1

I am trying to execute an automated test on a remote server. In order for the test to run, the user has to be authenticated into a system using a desktop application. Basically after you start the desktop application, a GUI window is displayed and then you enter a password and hit enter. After the user is authenticated, then the automated test can start.

I have written a Powershell script then when it runs, it can start the GUI application, enter the password, press ok, and the user gets authenticated. It runs without any problems if I am logged into the machine via an active remote desktop connection.

$password = "password"
& 'C:\Windows\GuiPassword.exe'
Start-Sleep -Seconds 60
$wshell = New-Object -ComObject wscript.shell; 
write-output "Focusing on the window"
$wshell.AppActivate('Gui Password') 
Sleep 2 
write-output "Entering the password string"
write-output "$wshell.SendKeys('$password')"
$wshell.SendKeys("$password")
$wshell.AppActivate('Gui Password') 
Sleep 2 
write-output "Entering Enter key"
$wshell.SendKeys('{ENTER}')
Sleep 10` 

I know this runs because I see it entering the password and then hitting the ok button while being logged into via Remote Desktop Connection. Also it displays "true" in Powershell ISE when it gets to the AppActivate calls

Now I have tried to execute this script remotely via a PS Session from another machine. Remoting is turned on and configured correctly. I see the script executing but it is unable to find the GUI window because I see "false" strings being displayed on the screen on the remote machine where I am executing it from. This is the powershell script that I am executing from a different machine. Assume that above script is saved as a powershell script called GUILogin.ps1 :

$tpassword = "password"
$secPasswd = ConvertTo-SecureString -AsPlainText -Force -String $tpassword
$myCreds = New-Object System.Management.Automation.PSCredential -argumentlist "REMOTESYSTEMUSERID", $secPasswd
$ps_Opt = New-PSSessionOption -IdleTimeout 43199999
$TargetSession = New-PSSession -ComputerName RemoteSystemname -Credential $myCreds -ConfigurationName RemoteExecution -SessionOption $ps_Opt
Write-Output "Session created"
Invoke-Command -Session $TargetSession  -Command {D:\Temp\GUILogin.ps1}    
Remove-PSSession $TargetSession

I am guessing it can't find it because it is a desktop application and requires an active desktop. Is that right? Can you please suggest what I can do so it can be found via the PS Session ?

Thank you

  • Possible duplicate of [Running remote GUI app in Powershell](https://stackoverflow.com/questions/3570517/running-remote-gui-app-in-powershell) – codewario Nov 21 '19 at 20:54
  • The short answer is, you can't. You can potentially make use of `psexec`, or make use of the remote `Task Scheduler` through Powershell Remoting, but the latter is hacky and very finicky to get right. See the link in the duplicate comment for how you can make use of `psexec` – codewario Nov 21 '19 at 20:56
  • I tried to use psexec -i but i keep getting the following error message: cmd exited on *ServerName* with exit code -1073741502 any idea why this is happening ? – My Display Name Nov 26 '19 at 17:28
  • c:\Sysinternals\PsExec.exe -i \\SERVERNAME -u USER -p PASSWORD /accepteula cmd /c powershell -noninteractive -ExecutionPolicy Bypass -file C:\PowershellScript.ps1 is what i used – My Display Name Nov 26 '19 at 17:30

0 Answers0