如何在powershell中讀取sc.exe回傳的錯誤代碼?
我有以下代碼
sc.exe create backupService BinPath= $binPath
sc.exe start backupService
執行時,控制臺中顯示以下內容,因為服務已經在運行。我想知道如何在 powershell 中讀取 sc.exe 輸出的錯誤代碼,例如 1073 或 1056?
[SC] CreateService FAILED 1073:
The specified service already exists.
[SC] StartService FAILED 1056:
An instance of the service is already running.
uj5u.com熱心網友回復:
Powershell 設定了一個自動變數$LastExitCode,其中包含最后一個外部程式的退出代碼 - 請參閱https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell- 7.2#lastexitcode
您的代碼將是這樣的:
sc.exe create backupService BinPath= $binPath
if( $LastExitCode -ne 0 )
{
# handle error
}
sc.exe start backupService
if( $LastExitCode -ne 0 )
{
# handle error
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376424.html
標籤:电源外壳
下一篇:如何在回圈中驗證用戶輸入?
