Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated Send Keys Failing When No RDP

I have a VM that I wish to run an automated task on (i.e. the automated task runs on the actual VM).

The VM is a Windows 2008 server.

I have added the task into the task scheduler, and when I'm logged into the machine via RDP I can run the task by right clicking, Run.

However, when my RDP session is turned off, (but the user on the VM is still logged in) the task trys to run, but only opens notepad.exe, but does not write the text.

The vbs script is as follows (simplified for our use here...);

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Notepad.exe"
Do Until Success = True
Success = objShell.AppActivate("Notepad")
Wscript.Sleep 1000
Loop
objShell.SendKeys "This is a test."

I have has this task working perfectly on a Windows XP machine and I just cant understand what is causing the issue.

It seems as if there is no keyboard found on the VM when not connected via RDP. When connected via RDP MSTSC sends the VM my keyboard and can then run the task correctly.

Thanks,

David

like image 600
David Johnson Avatar asked Apr 19 '13 10:04

David Johnson


People also ask

What to do if remote desktop is not working?

Check firewalls, security certificates and more if a remote desktop is not working. When the remote desktop connection between a Windows desktop and its host fails, it's time to do some remote desktop troubleshooting by checking firewalls, security certificates and more.

Why do I keep losing my remote desktop connection?

This problem may be caused by the session time limit setting. Policy settings in this node control time limits for Remote Desktop Services sessions on a Remote Desktop Session Host server. To stop Remote Desktop connection from disconnecting, you can change Policy settings.

Can ping but not RDP?

If you can ping the remote IP address but can't RDP, it means that the RDP failure isn't related to the network. And take a look at other reasons that cause the problem "cannot connect to the remote computer using RDP": Remote Desktop isn't enabled on or seated up a specific user.


1 Answers

OK,

So think i figured it out...

The problem is that once you log out with remote desktop, then the server is locked and no scripts can run. I have tried various options, including ControlSend and ControlClick commands, but without any success.

The solution is as follows: Create a batch file with the following commands and save it to the desktop of the workstation you want to keep unlocked: I have named my file Logoff.bat

START C:\Windows\System32\tscon.exe 0 /dest:console
START C:\Windows\System32\tscon.exe 1 /dest:console
START C:\Windows\System32\tscon.exe 2 /dest:console
START C:\Windows\System32\tscon.exe 2 /dest:console
START C:\Windows\System32\tscon.exe 3 /dest:console
START C:\Windows\System32\tscon.exe 4 /dest:console
START C:\Windows\System32\tscon.exe 5 /dest:console

Tscon.exe comes standard with your windows installation and is specifically created to leave a Previously-Locked Console Unlocked. See this link: http://support.microsoft.com/kb/302801

The next time you log in to the workstation with remote desktop, do not log out the normal way, but run you batch file - in my case "Logoff.bat" This will terminate your Remote Desktop connection and log you out, but will then continue in an unlocked state for any scripts to run as if someone is actually logged in.

like image 177
David Johnson Avatar answered Oct 20 '22 15:10

David Johnson