我正在嘗試從使用 Invoke-Command 在遠程計算機上運行腳本塊的 PowerShell 腳本中捕獲退出代碼。
首先是 BAT 檔案:BAT 檔案使用變數運行。腳本如下所示:
powershell.exe -noninteractive -noprofile -command "& {E:\Scripts\Check-Services_XXX.ps1 %1 }"
EXIT /B %errorlevel%
PowerShell 腳本如下所示:
param(
[string] $ip #IP address van server
)
$username = "DOMAIN\DOMAIN_USER"
$secpasswdfile = "E:\Location\DOMAINUSER_encrypted_password.txt"
$secpasswd = Get-Content $secpasswdfile | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$soptions = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
Invoke-Command -ComputerName $ip -UseSSL -SessionOption $soptions -Credential $credentials -ScriptBlock `
{
# Start services
Start-Service -InputObject (Get-Service -Name IAS)
# Check services status
$checkservice = (get-service -Name IAS -ErrorAction SilentlyContinue)
if($checkservice.status -ne "Running"){$host.SetShouldExit(1)}
exit
}
問題是 ExitCode 沒有被捕獲回來,所以當 BAT 檔案結束時,它以 0 結束。如果一切都在運行,就會出現這種情況。但是我故意將檢查服務部分中的服務名稱更改為肯定不存在的名稱,但 BAT 檔案仍然以 Exitcode 0 結尾
到目前為止已完成:嘗試了此解決方案: 使用“invoke-command”捕獲命令的回傳碼 - Powershell 2 但沒有奏效:出現以下錯誤“不等于打開,您無法在會話中運行命令。 session state is Closing" 顯然,當它退出錯誤時,會話已關閉,因此無法獲取退出代碼
也試過這個:Capture Write-Host output and exit code from Invoke-Command on a Remote System 但結果也一樣;沒有正確的退出代碼(在 BAT 檔案中應為 1 而不是 0)
解決方案!
感謝@js2010 和@mklement0;它現在就像一個魅力!
這是 BAT 檔案:
powershell.exe -noprofile -File "E:\Scripts\Check-Services_XXX.ps1" "%1" "%2"
EXIT /B %errorlevel%
這是最終為我解決的 PowerShell 代碼:
param(
[string] $ip, #IP address of checked server
[string] $service ) #Service name
$username = "DOMAIN\USER"
$secpasswdfile = "E:\Scripts\Credentials\DOMAIN-USER_encrypted_password.txt"
$secpasswd = Get-Content $secpasswdfile | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$soptions = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
$session = New-PSSession -ComputerName $ip -UseSSL -SessionOption $soptions -Credential $credentials
# Start services
Invoke-Command -Session $session -ScriptBlock { Start-Service -Name $using:service }
# Check services status
$checkservice = Invoke-Command -Session $session { Get-Service -name $using:service | where status -eq running }
if (! $checkservice) {
write-output ("Error 1, Service '" $service "' not running or not found.")
exit 1
}
我在將變數傳遞給遠程命令時遇到了一些問題,這個鏈接幫助了我(https://powershellexplained.com/2016-08-28-PowerShell-variables-to-remote-commands/)
uj5u.com熱心網友回復:
您必須在呼叫命令之外運行退出命令。
# check-service.ps1
$result = invoke-command localhost { get-service appxsvc |
where status -eq running }
if (! $result) {
exit 1
}
uj5u.com熱心網友回復:
更改您的呼叫powershell.exe以使用-File CLI引數:
powershell.exe -NoProfile -File "E:\Scripts\Check-Services_XXX.ps1" "%1"
EXIT /B %errorlevel%
這樣,.ps1腳本的退出代碼被正確地中繼為powershell.exe退出代碼。
此外,作為js2010 的回答說明,您需要使用腳本塊之外的$host.SetShouldExit(1)呼叫,因為后者是遠程執行的。由于下面解釋的原因,它是優選的。Invoke-Commandexit 1
通常來說,一般來說:
沒有理由使用
-Command(-c) CLI 引數"& { ... }"來呼叫代碼 - 只需"..."直接使用。舊版本的CLI 檔案錯誤地建議這& { ... }是必需的,但這已得到糾正。不僅沒有
"& { ... }"必要,而且總是將退出代碼重置為0.
至于您使用$host.SetShouldExit(1)退出代碼請求退出的使用1(撇開遠程呼叫無效):
- 它通常不被設計為從用戶代碼中呼叫,如本答案中所述。
有關 PowerShell 中退出代碼的一般資訊,請參閱此答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409803.html
標籤:
上一篇:在這種情況下,我如何將PowerShellSystem.Object[]轉換為字串值...?
下一篇:獲取內容-Powershell
