【发布时间】: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