【问题标题】:Powershell Remote Invoke-Command Start-Process App Immediately Closes After LaunchPowershell Remote Invoke-Command Start-Process App 启动后立即关闭
【发布时间】:2012-02-11 01:16:29
【问题描述】:

我正在编写一个脚本来远程终止两个进程,删除一些文件夹,并将服务作为应用程序启动。最终这将被部署为在多台服务器上运行,但我目前正在对一台服务器进行测试。一切都很好,除了最后一步是启动远程服务(由于作为服务运行的一些兼容性问题,它使用 -cl 参数作为应用程序运行)。应用程序通过脚本启动,但在脚本进入下一步时立即关闭。我是一个完全的菜鸟,所以我一直在挖掘很多,但没有找到解决方案的运气。看起来我最终可能不得不在新的运行空间中启动该进程,但我也没有找到一个好的新手指南来做这件事。

此外,当本地化并在机器上运行时,脚本的执行方式也完全一样。

这就是我所拥有的

$a = Get-ChildItem "\\TestMachine\c$\DataFiles"

$pass = cat "C:\cred.txt" | convertto-securestring 

$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "username",$pass

if ($a.Count -gt 10)
{
Invoke-Command -ComputerName TestMachine -cred $mycred -scriptBlock {stop-process -name TestProcess -force -EA SilentlyContinue}

Invoke-Command -ComputerName TestMachine -cred $mycred -scriptBlock {stop-process -name Winword -force -EA SilentlyContinue}

Get-ChildItem -Path \\TestMachine\c$\WordDocs -Recurse -EA SilentlyContinue | Where-Object {$_.PsIsContainer} | Remove-Item -Recurse -Force -EA SilentlyContinue

Invoke-Command -ComputerName TestMachine -cred $mycred -scriptBlock {Start-Process "C:\Program Files (x86)\Test\TestUploadManager\TestUploadManager.exe" -Verb Runas -ArgumentList -cl}

echo "Success: TestMachine Was successfully reset"
}

else

{
echo "Failed: Reset was not necessary"
}

exit

【问题讨论】:

标签: powershell powershell-remoting invoke-command start-process


【解决方案1】:

您可以尝试使用 Enter-PSSession 打开远程会话,然后使用 cmdlet Start-Process 而不使用 Invoke-Command

你也可以像这样& "C:\Program Files (x86)\Test\TestUploadManager\TestUploadManager.exe" -cl(在远程会话中)运行程序

最后你应该用Exit-PSSession退出会话

另一种可能性是“本地”运行脚本。编写脚本,就像在本地机器上运行它一样。将其复制到可访问的共享中。然后使用远程方法在机器上运行脚本。

【讨论】:

    【解决方案2】:

    Tom 建议的解决方案对我有用。以下是我使用的命令:

      New-PSSession -computername TestMachine
      Start-Process yourservice.exe
      Exit-PSSession
    

    确保使用Exit-PSSession 而不是Remove-PSSession,以便进程yourservice.exe 在退出会话后保持活动状态。

    【讨论】:

    • start-process 部分只会在您的机器上运行 exe。不是远程计算机,因为您没有使用会话来运行它。
    【解决方案3】:

    这个 WMI 解决方案最适合我: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/ead19612-5e7b-4012-8466-0d650232c7a5/invokecommand-and-startprocess?forum=ITCG – Adam Driscoll(指向同一个链接)

    用途:

    ([WMICLASS]"\\localhost\ROOT\CIMV2:win32_process").Create("something.exe argument1Here")
    

    代替:

    Start-Process
    

    【讨论】:

      【解决方案4】:

      在 Start-Process 调用中尝试使用 -Wait 参数。

      【讨论】:

      • 感谢您的建议。 -wait 参数确实使线程按我的需要运行,但程序需要能够在脚本继续通过 start-process 命令时运行。使用 -wait 在程序关闭之前它不会继续运行。
      【解决方案5】:

      我遇到了完全相同的问题。通过 [WMICLASS] 's create()Start-Process 的组合,它可以干净地工作。

      查看我的答案here

      【讨论】:

        猜你喜欢
        • 2022-11-25
        • 1970-01-01
        • 2012-05-13
        • 1970-01-01
        • 1970-01-01
        • 2015-08-22
        • 2014-08-04
        • 2019-12-15
        • 1970-01-01
        相关资源
        最近更新 更多