Automated Send Keys Failing When No RDP

家住魔仙堡 提交于 2019-12-10 15:23:43

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/16102789/automated-send-keys-failing-when-no-rdp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!