【发布时间】:2015-12-13 20:51:01
【问题描述】:
我需要通过 Powershell 使用按键“q”关闭打开的应用程序窗口。
我在 stackoverflow (How to perform keystroke inside powershell?) 上找到了一个解决方案,它工作得非常好,但只能在本地机器上运行:
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys('q')
问题是,我需要关闭远程机器上的那个窗口。我用一个函数试了一下:
$a = {
function Close_window {
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate(‘name of the window’)
Sleep 2
$wshell.SendKeys('q')
}
Close_window($args)
}
$user = 'user'
$pw = 'password'
$password = ConvertTo-SecureString $pw -asplaintext -force
$credential = New-Object System.Management.Automation.PSCredential $user, $password
$servers = Get-content D:\test.txt
foreach ($server in $servers)
{
$session = New-PSSession -ComputerName $server -Credential $credential
invoke-command -session $session -ScriptBlock $a
}
我还发现了这个:
$scriptobjects = @()
$scriptobjects += {
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate(‘Untitled - Notepad’)
Sleep 2
$wshell.SendKeys('q')
}
$scriptobjects |foreach {& $_}
但我没有管理它在远程机器上运行,结果总是 FALSE :-(
如果有人可以帮助我,我会很高兴!
提前非常感谢
保罗
【问题讨论】:
-
当你有 powershell 时看不到使用 wscript 的理由。 localy youd 有 smth like $p = Start-Process notepad -PassThru; $p.CloseMainWindow()
-
会话是否以您传递给脚本的用户身份运行?
-
嗨,杰奎琳,记事本只是一个例子。我的主要目标是将密钥“q”发送到我的应用程序。上面的例子表明它在本地工作,但我不知道如何在远程机器上运行以下代码块: $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate('应用程序窗口的标题') Sleep 1 $wshell.SendKeys('q')
标签: powershell remote-access keystroke wsh