13

I checked another thread (Automatically run a script when I log on to Windows), but it did not solve my exact problem.

I need to run a script on when a connection is made across my network using Windows Remote Desktop Connection.

The thread listed above works for the initial login, however, if I don't log out (which is necessary for some processes running on my network), then it won't run the script again the next time someone connects to the system using remote desktop connection. Previously we were using pcAnywhere to achieve this, however after running into some graphical issues with pcAnywhere, we have decided to move away from it to RDC.

Background: We need to have an email sent out anytime a connection is made to particular machines. The login name will always be the same for those systems and we do not log off when closing the connection.

4 Answers4

9

You can make a new scheduled task and define the trigger to "At the moment of a new connection to a user session"

Laurent F
  • 91
  • 1
  • 2
7

Use trigger "On connection to user session"

Note: I only noticed that the question is tagged "XP"/"Server2008". What's here may only work on Vista/2008R2 onwards.

Windows task scheduler has a predefined trigger for that. It is called On connection to user session.

It also allows you to distinguish between users and between local or remote connections.

The default when creating this trigger: Any user and Connection from remote computer. This seems to be what you want, so you don't have to change that.

Screenshot here: Screenshot of trigger

Source: Microsoft TechNet: Task Scheduler | Triggers (Archived here.)

Further reading

When you export this task and then look inside the file, you will see that this a trigger of type SessionStateChangeTrigger: Trigger detail There is in depth documentation on MSDN:

3

To my knowledge Windows does not provide a built-in method for running a program or script on connection, only log on. You'll need to find a program that registers with Windows (using WTSRegisterSessionNotification) and listens for the WM_WTSSESSION_CHANGE message.

A program that does the above will receive notifications from Windows about user logons, logoffs, connections, disconnections, and all of the events needed to launch a script at connection as well as log on.

Off the top of my head I don't know of any applications (other than pcAnywhere) that can do this.

Joshua
  • 4,574
0

If you're familiar with programming in .net, you may want to look at this thread over at Stack Overflow.

According to Steven A. Lowe:

allegedly,

System.Windows.Forms.SystemInformation.TerminalServerSession will be true for a remote desktop session (or VNC session)

You may also be able to monitor for a connection on the RDP, which by default is TCP 3389.

Jared Harley
  • 13,012