SendKeys in VBScript not working for taskmgr

夙愿已清 提交于 2019-12-11 02:05:06

问题


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

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