【问题标题】:Start Application on Remote machine and keep it running and powershell script should not wait for process to finish在远程机器上启动应用程序并保持运行,powershell 脚本不应等待进程完成
【发布时间】:2016-04-19 15:27:29
【问题描述】:

远程机器上有一个应用程序,比如 Vesper.exe。我现在正在做的是使用 powershell 会话远程登录机器。

要求:需要在远程机器上启动应用程序,并且在powershell脚本执行完成后应用程序应该继续运行

我试过两种方法,总结如下:

  • Invoke-Command:如果我使用此 cmdlet 启动应用程序,那么我的脚本会卡住并等待应用程序最终停止
  • Start-Process:如果我使用此 cmdlet,应用程序会开始运行,但会立即退出。我发现这个的原因是我使用了 cmdlet Remove-PSSession,然后我发现我应该使用 Exit-PSSession 来保持远程机器上的进程处于活动状态。但是这样做的问题是,一旦构建完成执行(或者如果我关闭 powershell 窗口),远程计算机上的应用程序就会自行停止。我不知道在这里做什么。

以下是powershell代码:

# Create a new session on the remote machine with the above credentials
try {
    $newsession = New-PSSession -ComputerName $loadvm -Credential $loadvm_credentials
    Write-Host "Connected successfully..."
} catch [Exception] {
    echo "Error while connecting to the remote machine", $_.Exception.GetType().FullName, $_.Exception.Message
    exit 1
}
try {
    Invoke-Command -Session $newsession -Scriptblock {
      Write-Host "Cd'ing and starting Vesper on"
      cd C:\vesper_cpt
      Start-Process -file Vesper.exe -ArgumentList "-auto"
    } -ErrorAction Stop
} catch [Exception] {
    echo "Error while running the remote command", $_.Exception.GetType().FullName, $_.Exception.Message
    Remove-PSSession $newsession
    exit 1
}

Exit-PSSession

【问题讨论】:

    标签: windows powershell invoke-command start-process remote-process


    【解决方案1】:

    如何将二进制文件作为服务运行?然后 Powershell 将仅用于启动/停止服务,这非常简单。

    您可以使用nssm 轻松将任何可执行文件转换为服务。

    【讨论】:

      猜你喜欢
      • 2016-09-08
      • 2011-12-16
      • 2010-10-07
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多