首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取从sc.exe命令返回的错误代码

如何读取从sc.exe命令返回的错误代码
EN

Stack Overflow用户
提问于 2021-12-07 07:08:16
回答 1查看 419关注 0票数 0

如何在powershell中读取从sc.exe返回的错误代码?

我有下面的代码

代码语言:javascript
复制
sc.exe create backupService BinPath= $binPath

sc.exe start backupService 

当执行时,下面显示在控制台中,这是预期的服务已经在运行。我想知道如何读取sc.exe输出的powershell中的错误代码,例如1073或1056?

代码语言:javascript
复制
[SC] CreateService FAILED 1073:

The specified service already exists.

[SC] StartService FAILED 1056:

An instance of the service is already running.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-07 07:40:36

Powershell设置一个名为$LastExitCode的自动变量,该变量包含最后一个外部程序的退出代码-请参阅变量?view=powershell-7.2#lastexitcode

您的代码应该如下所示:

代码语言:javascript
复制
sc.exe create backupService BinPath= $binPath
if( $LastExitCode -ne 0 )
{
    # handle error
}

sc.exe start backupService
if( $LastExitCode -ne 0 )
{
    # handle error
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70256121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档