如何在powershell中读取从sc.exe返回的错误代码?
我有下面的代码
sc.exe create backupService BinPath= $binPath
sc.exe start backupService 当执行时,下面显示在控制台中,这是预期的服务已经在运行。我想知道如何读取sc.exe输出的powershell中的错误代码,例如1073或1056?
[SC] CreateService FAILED 1073:
The specified service already exists.
[SC] StartService FAILED 1056:
An instance of the service is already running.发布于 2021-12-07 07:40:36
Powershell设置一个名为$LastExitCode的自动变量,该变量包含最后一个外部程序的退出代码-请参阅变量?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://stackoverflow.com/questions/70256121
复制相似问题