簡體   English   中英

在遠程計算機上啟動應用程序並使其運行,powershell腳本不應等待進程完成

[英]Start Application on Remote machine and keep it running and powershell script should not wait for process to finish

遠程計算機上有一個名為Vesper.exe的應用程序。 我現在正在做的是使用Powershell會話遠程登錄到計算機。

要求 :需要在遠程計算機上啟動應用程序,並且在powershell腳本完成執行后,應用程序應保持運行

我嘗試了兩種方法,以下是摘要:

  • 調用命令:如果我使用此cmdlet啟動應用程序,則腳本將卡住並等待應用程序最終停止
  • 啟動過程:如果我使用此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

如何將二進制作為服務運行? 然后,Powershell將僅用於啟動/停止服務,這非常簡單。

您可以使用nssm輕松地將任何可執行文件轉換為服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM