1
我需要通过Powershell关闭具有按键“q”的打开的应用程序窗口。如何使用Powershell远程向应用程序发送按键
我发现这里的解决方案上的计算器(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的原因。本地你有不喜欢$ p =开始 - 进程记事本-PassThru; $ p.CloseMainWindow() –
会话是否以您传递给脚本的用户身份运行? – Matt
嗨,杰奎琳,记事本只是一个例子。我的主要目标是将密钥'q'发送给我的应用程序。上面的例子显示它在本地工作,但我不知道如何在远程机器上运行以下代码块:$ wshell = New-Object -ComObject wscript.shell; $ wshell.AppActivate('应用程序窗口的标题') Sleep 1 $ wshell.SendKeys('q') –