我需要通过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 {& $_}
但我没有管理它在远程机器上运行,结果总是错误的: - (
如果有人可以帮助我,我会很高兴!
非常感谢提前 保罗
答案 0 :(得分:1)
$session = New-PSSession -ComputerName $server -Credential $credential
invoke-command -session $session -ScriptBlock {
$p = Start-Process notepad -PassThru
$p.CloseMainWindow()
}