Windows 如何远程执行需要active desktop的Powershell脚本

Windows 如何远程执行需要active desktop的Powershell脚本,windows,powershell,session,remoting,Windows,Powershell,Session,Remoting,我正在尝试在远程服务器上执行自动测试。为了运行测试,必须使用桌面应用程序对用户进行系统身份验证。基本上,启动桌面应用程序后,会显示一个GUI窗口,然后输入密码并按enter键。用户通过身份验证后,自动测试就可以开始了 我已经编写了一个Powershell脚本,当它运行时,它可以启动GUI应用程序,输入密码,按ok,然后用户获得身份验证。如果我通过活动的远程桌面连接登录到计算机,它运行时不会出现任何问题 $password = "password" & 'C:\Windows\GuiPas

我正在尝试在远程服务器上执行自动测试。为了运行测试,必须使用桌面应用程序对用户进行系统身份验证。基本上,启动桌面应用程序后,会显示一个GUI窗口,然后输入密码并按enter键。用户通过身份验证后,自动测试就可以开始了

我已经编写了一个Powershell脚本,当它运行时,它可以启动GUI应用程序,输入密码,按ok,然后用户获得身份验证。如果我通过活动的远程桌面连接登录到计算机,它运行时不会出现任何问题

$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` 
我知道这会运行,因为我看到它输入密码,然后在通过远程桌面连接登录时点击ok按钮。当它到达AppActivate调用时,它还会在Powershell ISE中显示“true”

现在,我尝试通过PS会话从另一台机器远程执行此脚本。远程处理已打开并正确配置。我看到脚本正在执行,但它找不到GUI窗口,因为我看到在执行脚本的远程机器的屏幕上显示“false”字符串。这是我在另一台机器上执行的powershell脚本。假设上述脚本另存为名为GUILogin.ps1的powershell脚本:

$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
我猜它找不到它,因为它是一个桌面应用程序,需要一个活动桌面。是这样吗?您能建议我可以做什么,以便通过PS会话找到它吗


谢谢你

简短答案的可能重复是,你不能。您可以使用
psexec
,也可以通过Powershell远程处理使用远程
任务调度程序
,但后者有黑客之嫌,而且非常挑剔。请参阅重复注释中的链接,了解如何使用
psexec
我尝试使用psexec-I,但一直收到以下错误消息:cmd在ServerName上退出,退出代码为-1073741502知道为什么会发生这种情况吗?c:\Sysinternals\psexec.exe-I\\ServerName-u USER-p PASSWORD/accepteula cmd/c powershell-非交互式-ExecutionPolicy Bypass-文件C:\PowershellScript.ps1是我使用的文件。简短答案的可能副本是,您不能。您可以使用
psexec
,也可以通过Powershell远程处理使用远程
任务调度程序
,但后者有黑客之嫌,而且非常挑剔。请参阅重复注释中的链接,了解如何使用
psexec
我尝试使用psexec-I,但一直收到以下错误消息:cmd在ServerName上退出,退出代码为-1073741502知道为什么会发生这种情况吗?c:\Sysinternals\psexec.exe-I\\ServerName-u USER-p PASSWORD/accepteula cmd/c powershell-非交互式-ExecutionPolicy Bypass-文件C:\PowershellScript.ps1是我使用的