问题
In Windows 10, I am using the exact same vbscript code once for "calc" and once for "taskmgr". Why does SendKeys (here alt+space) work for "calc" but not for "taskmgr"?
<package>
<job id="vbs">
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc"
WScript.Sleep 100
WshShell.SendKeys "% "
</script>
</job>
</package>
(working as expected, drop down menu opens)
<package>
<job id="vbs">
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "taskmgr"
WScript.Sleep 100
WshShell.SendKeys "% "
</script>
</job>
</package>
(not working, no drop down menu...)
回答1:
All right. Thank you all for your help in the comments. So, for sendKeys to work for taskmgr, the script has to be run as administrator. This can be done by openning a Command Prompt as Administrator and then
cscript myScript.wsf
回答2:
I think you missed a second keystroke. For me this works:
DIM shell
SET shell = WScript.CreateObject("WScript.Shell")
shell.Run "taskmgr.exe", 1, False
WScript.Sleep(100)
shell.SendKeys "% "
shell.SendKeys "n" '<- this is what you might have missed
but it only works if run by the same user (note that you cannot have two instances of the taskmgr running at the same time. When there is an "old" instance than this instance will be brought to foreground and no new instance will be created, even if the "old" instance is running under a different user. In this case you might need to close/kill the "old" instance with taskkill /im taskmgr.exe
来源:https://stackoverflow.com/questions/44180036/sendkeys-in-vbscript-not-working-for-taskmgr