How to send the keystroke 'q' to an application remotely with Powershell

Hi guys,

how can I execute the following script on a remote machine:

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

I tried it with a function: $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 }

I also found this:

$scriptobjects = @() $scriptobjects += { $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate(‘Untitled - Notepad’) Sleep 2 $wshell.SendKeys('q') } $scriptobjects |foreach {& $_}

but I did not manage it to run on a remote machine, the result is always FALSE :-(

I would be happy if someone could help me with this!

Many thanks in advance

Paul


 

Parents
No Data
Reply
  • You don't understand how PowerShell remoting works. None of that will work in a remote session. You don't have access to any GUI elements that might be on the remote computer. All you have is a command line console so there's no way to interact with any windows.

Children
No Data