Powershell-隐藏的Windows中的键盘按键 [英] Powershell - Keyboard keystroke in a Hidden Windows
问题描述
我正在实习,我必须编写一个powershell脚本来自动化telnet脚本连接.我使用cmd进行操作,我发送了击键来连接:
I'm in internship and I have to write a powershell script to automate a telnet script connection. I do it with cmd, I send keystroke to connect like this:
$ cmd ='C:\ Windows \ System32 \ cmd.exe'启动进程$ cmd -Verb runAs$ wshell =新对象-ComObject wscript.shell;$ wshell.AppActivate($ cmd)睡觉3$ wshell.SendKeys('telnet IP_ADDRESS')
工作正常!但是我有一个问题...
And it work fine! But I have a problem...
我必须以隐藏样式运行cmd窗口,所以我已经这样做了:
I have to run the cmd window in a hidden style, so I have done this:
Start-Process $cmd -Verb runAs -Windowstyle hidden
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($cmd)
Sleep 3
$wshell.SendKeys('telnet IP_ADDRESS')
窗口以隐藏样式启动,可以,但是键盘按键不在隐藏的cmd窗口中
The window launch on a hidden style, it's ok, but the keyboard keystroke don't go in the hidden cmd window
有人有解决方案吗?
推荐答案
问题是您使用的SendKeys实际上只是模拟键盘上的按键.如果没有窗口,则没有任何内容可将按键发送到.
The problem is you're using SendKeys which literally just simulates keypresses on the keyboard. If there is no window, there is nothing to send the keypresses to.
Start-Process $cmd -ArgumentList "/K telnet IP_ADDRESS" -Verb runAs -Windowstyle hidden
这只是告诉CMD在打开时运行指定的命令,应该可以解决问题.
This just tells CMD to run the specified command upon opening, should do the trick.
这篇关于Powershell-隐藏的Windows中的键盘按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!