我怎么能脚本一个蝙蝠或cmd停止和启动一个服务可靠的错误检查(或让我知道它不成功的原因)?


当前回答

SC 净停止/启动 PsService WMIC Powershell还有易于使用的选项

SC和NET已经作为答案给出了。PsService增加了一些简洁的功能,但需要从微软下载。

但我最喜欢的方式是WMIC,因为WQL语法提供了一种强大的方式,可以用一行管理多个服务(WMI对象也可以通过powershell/vbscript/jscript/c#使用)。

最简单的使用方法:

wmic service MyService call StartService
wmic service MyService  call StopService

以及WQL的例子

wmic service where "name like '%%32Time%%' and ErrorControl='Normal'" call StartService

这将启动名称包含32Time并具有正常错误控制的所有服务。

这里有一些你可以使用的方法。

:

wmic service get /FORMAT:VALUE

您可以查看有关服务的可用信息。

其他回答

或者您可以使用以下CMD启动远程服务:sc \\<computer> start <service>

使用SC(服务控制)命令,它为您提供了更多的选项,而不仅仅是启动和停止。

  DESCRIPTION:
          SC is a command line program used for communicating with the
          NT Service Controller and services.
  USAGE:
      sc <server> [command] [service name]  ...

      The option <server> has the form "\\ServerName"
      Further help on commands can be obtained by typing: "sc [command]"
      Commands:
        query-----------Queries the status for a service, or
                        enumerates the status for types of services.
        queryex---------Queries the extended status for a service, or
                        enumerates the status for types of services.
        start-----------Starts a service.
        pause-----------Sends a PAUSE control request to a service.
        interrogate-----Sends an INTERROGATE control request to a service.
        continue--------Sends a CONTINUE control request to a service.
        stop------------Sends a STOP request to a service.
        config----------Changes the configuration of a service (persistant).
        description-----Changes the description of a service.
        failure---------Changes the actions taken by a service upon failure.
        qc--------------Queries the configuration information for a service.
        qdescription----Queries the description for a service.
        qfailure--------Queries the actions taken by a service upon failure.
        delete----------Deletes a service (from the registry).
        create----------Creates a service. (adds it to the registry).
        control---------Sends a control to a service.
        sdshow----------Displays a service's security descriptor.
        sdset-----------Sets a service's security descriptor.
        GetDisplayName--Gets the DisplayName for a service.
        GetKeyName------Gets the ServiceKeyName for a service.
        EnumDepend------Enumerates Service Dependencies.

      The following commands don't require a service name:
      sc <server> <command> <option>
        boot------------(ok | bad) Indicates whether the last boot should
                        be saved as the last-known-good boot configuration
        Lock------------Locks the Service Database
        QueryLock-------Queries the LockStatus for the SCManager Database
  EXAMPLE:
          sc start MyService
net start [serviceName]

and

net stop [serviceName]

清楚地告诉你他们是成功了还是失败了。例如

U:\>net stop alerter
The Alerter service is not started.

More help is available by typing NET HELPMSG 3521.

如果从批处理文件运行,则可以访问返回代码的ERRORLEVEL。0表示成功。高于这个数值意味着失败。

作为bat文件,error.bat:

@echo off
net stop alerter
if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem
pause

输出如下所示:

U:\>error.bat
The Alerter service is not started.

More help is available by typing NET HELPMSG 3521.

There was a problem
Press any key to continue . . .

返回代码

 - 0 = Success
 - 1 = Not Supported
 - 2 = Access Denied
 - 3 = Dependent Services Running
 - 4 = Invalid Service Control
 - 5 = Service Cannot Accept Control
 - 6 = Service Not Active
 - 7 = Service Request Timeout
 - 8 = Unknown Failure
 - 9 = Path Not Found
 - 10 = Service Already Running
 - 11 = Service Database Locked
 - 12 = Service Dependency Deleted
 - 13 = Service Dependency Failure
 - 14 = Service Disabled
 - 15 = Service Logon Failure
 - 16 = Service Marked For Deletion
 - 17 = Service No Thread
 - 18 = Status Circular Dependency
 - 19 = Status Duplicate Name
 - 20 = Status Invalid Name
 - 21 = Status Invalid Parameter 
 - 22 = Status Invalid Service Account
 - 23 = Status Service Exists
 - 24 = Service Already Paused

编辑20.04.2015

返回代码:

NET命令不会返回文档中的Win32_Service类返回码(服务未激活,服务请求超时等),对于许多错误将简单地返回Errorlevel 2。

看这里:http://ss64.com/nt/net_service.html

我只是使用上面Jonas的例子,创建了一个完整的0到24个错误级别的列表。另一篇文章是正确的,网络启动和网络停止只使用errorlevel 0表示成功,2表示失败。

但这对我来说很管用:

net stop postgresql-9.1
if %errorlevel% == 2 echo Access Denied - Could not stop service
if %errorlevel% == 0 echo Service stopped successfully
echo Errorlevel: %errorlevel%

将停止改为开始并反向工作。

手动重启服务是ok -服务。msc有“重新启动”按钮,但在命令行中,sc和net命令都没有“重新启动”开关,如果在cmd/bat文件中计划重新启动,服务将立即停止和启动,有时它会得到一个错误,因为服务还没有停止,它需要一段时间来关闭东西。

这可能会产生一个错误: sc停止 sc开始

插入超时是个好主意,我使用ping(它每1秒ping一次): sc停止 Ping localhost -n 60 sc开始