Task Scheduler trigger "on remote connection to" not firing on new RDP session - remote-desktop

I'm trying to use Task Scheduler to send an email every time someone logs on via remote desktop. Now there's a specific trigger in Task Scheduler called "on connection to user session" with settings that can be set to "Connection from remote computer". I use this to perform the action of running a PS script that sends the email. This works.
However, the trigger only fires when someone reconnects to a previously disconnected RDP session. Performing a fresh logon via RDP does not trigger the task. What's worse, I would swear that this was not a problem yesterday, before I pat myself on the back on a job well done and shut down the server before going to bed. I'm sure the task fired both on fresh logons as well as reconnects, and tested this multiple times.
So I guess I have two questions; what could have caused the task to change it's behavior from a reboot, and how can I get it working again? I tried creating a new identical task, but that didn't help. Are there other options/solutions I should pursue to achieve my goal?
I'm doing this on a Win 10 Pro system.
Best regards

Solved.
It appears the trigger "at log on" fires on both local and remote logons (fresh, not reconnected), while "on connection to user session" fires when (re)connecting to an existing session.

Related

Offline and online execution problem with multiples queries incoming

I have a SQL Server service and multiple Windows Service doing some backgrounds work on same server.
One of them (I'm calling it "A") have a routine executing "single_user/offline" and "online/multi_user" to active databases to do some backup operations at midnight. The another ones executes multiple queries over that databases (I'm calling it "B").
The problem is the following:
1.- Windows Service "A" executes SET ONLINE.
2.- Windows Service "B" executes a random SELECT.
3.- Windows Service "A" tries to execute SET MULTI_USER. This execution is dropped because there is an active connection made from Windows Service "B".
I've tried executing SET ONLINE and SET MULTI_USER on same CommandText of the SqlCommand, but this doesn't denies the incoming query from Windows Service "B", breaking my process and keeping the database locked (because the SINGLE_USER).
How can I make an ONLINE and MULTI_USER commands at same time on the Windows Service "A" to make Windows Service "B" being cancelled or wait the process finished? (It's not a problem that Windows Service "B" being cancelled)
Could be sp_dettach_db or sp_attach_db useful?
It sounds like Service A is closing it's connection after bringing the database into Single User mode. This frees up Service B to become the single user, at which point Service A can no longer change the mode until it can grab the single connection back, which it won't be able to do as long as Service B, or any other client for that matter, has it.
I can think of a couple of things you could do here:
Once your offline operations are complete, begin polling until Service A can become the single user again
Find the SPID of the connection from Service B and kill it.
See the limitations and restrictions section about single user mode at this link for more info.
Thanks to all, but I reanalyzed the problem I was having from the beginning, and concluded that the action "SET SINGLE_USER" was not necessary, since the process does not require actions in that mode, so finally with the action "SET OFFLINE" and "SET ONLINE" could prevent intermediate connection problems.

Kill Job Activity in SQL Server

There is job on our SQL Server. It was started and stopped after some time. However, the job still appears to be running even if it is not.
I can see it "is running" in Job Activity Monitor and also sysjobactivity (= there is no stop_execution_date field filled, but in fact nothing happens. I do not see it running in Activity Monitor.
How to effectively kill the job without need to restart whole server? Stopping the job via SSMS GUI does not work.

Cannot log in to LocalDB

I accidentally deleted my domain logon from SQL Server LocalDB. It was the only logon on the server.
How can I get access back to my instance? Every time I try connecting I get the message Logon failed for domain\user.
I tried reinstalling and repairing with no luck.
Did you have any special configuration of your instance that would be hard to recreate? Or maybe user data in masterDB (please don't do this ever again if so)?
If not then you can just delete the instance and recreate it. All user databases will be preserved, although you will need to attach them again.
Assuming we're talking about the automatic instance (v11.0), just do:
SqlLocalDB delete v11.0
It will be recreated the next time you try connecting to it. It can take 10 seconds or more, so the first connection could timeout.
After that use sp_attach_db() stored proc to attach your user databases again.

How to determine the most restrictive SQL server security permissions a program can use and still function?

PROBLEM BACKGROUND
Sorry if this is a bit tedious to read, but please bear with me.
I have been tasked to determine the most restrictive security permissions...or rather investigate if more restrictive security settings can be configured for the SQL server login our program uses, yet still function as normal.
Currently the program runs as a Windows service configured to log on using a Windows user account that has been configured in SQL server with trusted auth. The login used has been assigned a db_owner role and the service works fine like that.
So to narrow the permissions for this user I removed the db_owner rights and assigned it to the db_datareader and db_datawriter roles. Unfortunately this causes a problem and when I start up the service I get an error dialogue displaying:
Error 1053: the service did not respond to the start or control request in a timely fashion.
and in the event viewer under the System events are logged:
event 7009 (timeout waiting for..to connenct)
event 7000 (the service did not respond to the start or control )
My problem is the code base is really large and I'm not sure what exactly to look for that would require db_owner permissions (it sets permissions maybe?).
QUESTION
What should I be looking for in a program that executes SQL that would cause it to require db_owner permissions?
In case the first question is too general: is there an easy way/any tools I can use to figure out what a Windows service is trying to do during start-up 'SQL wise' if I get system error events logged:
event 7009 (Timeout (30000 milliseconds) waiting for the ... service to connect)
event 7000 (The service did not respond to the start or control request in a timely fashion).
BTW I tried running profiler with all audit events selected, but still get nothing logged when starting the service.
This is such a broad question without knowing the architecture of your service and how it communicates with SQL Server. Are you using in-line SQL? Stored Procedures?
I think you'd best tackle this issue by starting from the service's code and tracing the execution path from the start and see what is being executed on/against SQL Server.
Alternatively, if you are using stored procedures, you may want to script them all out into a file and search on some common T-SQL commands limited to a db_owner, such as CREATE, DROP, ALTER.

How to remotely Start/Stop SQLServer services kicking off existing connections?

I know there is already a question about this but my issue is more oriented to remote scenarios.
With net start/stop you can specify a /y parameter to bounce users off current sessions but you cannot start/stop remotely.
With sc you can start/stop remotely but if the SQLServer instance is being used it won't let you stop the service (with a "[SC] ControlService FAILED 1051: A stop control has been sent to a service that other running services are dependent on." message)
So my question is: what's the best way of kicking out users stopping the SqlServer service remotely?
I think the /y just answers Yes to the "Are you sure?" prompt. I'd think that sc could be used as well, though it may time out waiting for the service to stop if there's a lot of inflight transactions. Does it give you any specifics of why it can't stop?
Here's a couple of other methods to stop a remote SQL instance. Except for SHUTDOWN WITH NOWAIT, I'd recommend any of them.
psexec will let you run net stop remotely.
There's also the SQL SHUTDOWN command - you can issue that WITH NOWAIT to avoid waiting for current transactions to finish and checkpointing, which will make shutdown faster (but subsequent startup slower, and could lead to lost data).
Or you could use either Configuration Manager or Management Studio to stop a remote instance.
Edit: The error is pretty self explanatory. It means you must stop dependent services first. Sql Agent is probably at least one of them. Checking Admin Tools->Services will show you the rest.
Using SSMS is the best make sure you have the proper privileges to shutdown and check to see the status of replications and ALWAYS ON and Log Shipping.

Resources