如何在Powershell中执行击键? [英] How to perform keystroke inside powershell?

查看:176
本文介绍了如何在Powershell中执行击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ps1脚本来从vmware集群环境中获取一些信息.

I have ps1 script to grab some information from the vmware cluster environment.

在ps1脚本的某个地方,需要按 ENTER 按钮.

In some place of ps1 script requires the ENTER button keystroke.

那么,该怎么做?

-谢谢

推荐答案

如果我理解正确,您是否希望PowerShell将 ENTER 击键发送给某些交互式应用程序?

If I understand correctly, you want PowerShell to send the ENTER keystroke to some interactive application?

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys('~')

如果该交互式应用程序是PowerShell脚本,则只需将PowerShell窗口标题栏中的内容用作 AppActivate 的参数(默认情况下为powershell.exe的路径).为避免歧义,可以使用title 'new window title'命令使脚本重命名其自己的窗口.

If that interactive application is a PowerShell script, just use whatever is in the title bar of the PowerShell window as the argument to AppActivate (by default, the path to powershell.exe). To avoid ambiguity, you can have your script retitle its own window by using the title 'new window title' command.

一些注意事项:

  • 波浪号(〜)代表 ENTER 击键.您也可以使用{ENTER},尽管它们并不相同-这是键盘上的 ENTER 键.完整列表可在此处找到: http://msdn.microsoft.com/zh-CN/library/office/aa202943%28v=office.10%29.aspx .
  • Sleep 1语句的原因是等待1秒,因为激活窗口需要一些时间,如果立即调用 SendKeys ,它将把密钥发送到PowerShell窗口,或者到无处.
  • 请注意,如果在等待的第二秒钟内键入任何内容或单击鼠标,这可能会被触发,从而导致无法激活通过 AppActivate 激活的窗口.您可以尝试减少时间以找到在系统上可靠的最小时间(睡眠接受小数,因此您可以尝试0.5秒钟).我发现在我的2.6 GHz Core i7 Win7笔记本电脑上,任何少于.8秒的故障率都很高.为了安全起见,我用了1秒钟.
  • 重要警告: 如果您使用此方法发送密码,请格外小心,因为在调用 AppActivate 并调用 SendKeys 会将密码以纯文本格式发送到该不同的窗口!
  • The tilde (~) represents the ENTER keystroke. You can also use {ENTER}, though they're not identical - that's the keypad's ENTER key. A complete list is available here: http://msdn.microsoft.com/en-us/library/office/aa202943%28v=office.10%29.aspx.
  • The reason for the Sleep 1 statement is to wait 1 second because it takes a moment for the window to activate, and if you invoke SendKeys immediately, it'll send the keys to the PowerShell window, or to nowhere.
  • Be aware that this can be tripped up, if you type anything or click the mouse during the second that it's waiting, preventing to window you activate with AppActivate from being active. You can experiment with reducing the amount of time to find the minimum that's reliably sufficient on your system (Sleep accepts decimals, so you could try .5 for half a second). I find that on my 2.6 GHz Core i7 Win7 laptop, anything less than .8 seconds has a significant failure rate. I use 1 second to be safe.
  • IMPORTANT WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text!

有时 wscript.shell SendKeys 方法可能有点古怪,因此如果遇到问题,请用以下内容替换上面的第四行:

Sometimes wscript.shell's SendKeys method can be a little quirky, so if you run into problems, replace the fourth line above with this:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');

这篇关于如何在Powershell中执行击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆