【发布时间】:2017-12-08 11:07:38
【问题描述】:
我想从本地 powershell 脚本远程停止一个 winform 应用程序,但 CloseMainWindow 函数似乎不起作用
---这是连接远程服务器执行closeApp.ps1的本地命令,它存储在服务器上--
$remoteSession = New-PSSession -computerName 'xxx' -credential $cred
Invoke-Command -session $remoteSession -command {F:\AutoDeployment\closeApp.ps1}
Remove-pSSession -session $remoteSession
---这是closeApp.ps1的主要内容,它获取活动进程并尝试关闭它--
$app = Get-Process AppName -ErrorAction SilentlyContinue
if($app)
{
$app.MainWindowTitle
$app.CloseMainWindow()
}
我希望它应该像本地一样正确关闭应用程序。
但现在它为 mainWindowTitle 行返回 null,为 closeMainWindow 返回 False。
该脚本直接运行在服务器上运行良好,但通过invoke-command执行时,MainWindow似乎无效且不起作用。
我通过 jenkins 插件 powershell 调用这个命令
更新:
closeMainWindow 没有通过 invoke-command 的错误日志,但它总是返回“假”,预期为“真”。
此外,如果closeMainWindow 失败,我还有一个步骤将尝试发送关键操作以使该应用停止。我使用[System.Windows.Forms.SendKeys]::SendWait 来实现这一点。
它也可以在本地工作,但在带有日志的服务器上通过invoke-command 失败:
sendkeys 的错误日志:
Exception calling "SendWait" with "1" argument(s): "Access is denied"
+ CategoryInfo : NotSpecified: (Exception calli...cess is denied":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
【问题讨论】:
标签: powershell jenkins