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


当前回答

使用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和net stop的返回代码似乎是最好的方法。试着看看这个:Net Start返回代码。

有时你会发现停不动。

我的SQlServer有时会这样做。使用下面的命令行杀死它。如果你真的真的需要你的剧本来消灭那些不停的东西。作为最后的手段,我会让它这么做

taskkill /pid [pid number] /f

语法总是让我....所以…

下面显式地介绍了如何向批处理文件中添加一行,如果您是两台机器上的管理员,以管理员身份运行.bat,并且两台机器位于同一个域中,则该批处理文件将终止远程服务(在另一台机器上)。机器名称遵循UNC格式\myserver

sc \\ip.ip.ip.ip stop p4_1

在这种情况下……当您在服务管理器中查看服务的属性时,p4_1是服务名称和显示名称。您必须使用服务名称。

对于你们的服务运营迷…一定要附上你的理由代码和评论!即。“4”等于“计划”和注释“停止服务器维护”

sc \\ip.ip.ip.ip stop p4_1 4 Stopping server for maintenance

我正在用c#写一个windows服务,停止/卸载/构建/安装/启动循环太累人了。写了一个小脚本,命名为reploy.bat,并放在我的Visual Studio输出目录中(其中包含构建的可执行服务)来自动化循环。

只要设置这3个变量

servicename:显示在Windows服务控制面板(services.msc)

Slndir:包含解决方案(.sln)文件的文件夹(不是完整路径)

Binpath:从构建到服务可执行文件的完整路径(不是文件夹路径)

注意:需要从Visual Studio开发人员命令行运行msbuild命令才能工作。

SET servicename="My Amazing Service"
SET slndir="C:dir\that\contains\sln\file"
SET binpath="C:path\to\service.exe"
SET currdir=%cd%

call net stop %servicename%
call sc delete %servicename%
cd %slndir%
call msbuild 
cd %bindir%
call sc create %servicename% binpath=%binpath%
call net start %servicename%
cd %currdir%

也许这能帮助到某些人:)

下面是Windows 10使用批量启动系统还原的命令:

sc config swprv start= Auto

你可能也会喜欢这些命令:

更改注册表值为自动启动系统恢复 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" /v DisableSR /t REG_DWORD /d 0 /f 创建系统还原点 Wmic.exe /Namespace:\root\default Path SystemRestore Call CreateRestorePoint "djibe saved your PC", 100,12 修改“系统恢复”磁盘使用率 vssadmin调整shadowstorage /for=C: /on=C: /maxsize=10%

享受