停止/启动远程Windows服务并等待其打开/关闭


68

这个问题的最高答案告诉我如何停止/启动远程服务。大。现在,我需要等待实际的停止/启动完成。所以,我要寻找的是一个dos命令:

  1. 启动服务,应仅在服务启动后(或在超时后提高错误级别)返回
  2. 停止服务,仅在服务停止后返回

我很想知道这一点。我不相信使用SC命令是可能的,因为它本质上是异步的。当命令从“停止”变为“停止”以及从“开始”变为“开始”时,您可能不得不将该命令绑定到服务状态的ping上。
David Andres 2009年

Answers:


119

我创建了一组使用sc.exe来执行此操作的批处理脚本。它们附在下面。要运行这些脚本,您应该是在目标计算机上具有管理权限的用户,并应从属于同一域成员的计算机上运行该脚本。可以将其设置为可以从域外部运行(例如从VPN),但是要涉及防火墙,DCOM和安全凭证,需要进行很多层安全工作。

这些天之一,我要弄清楚PowerShell等效项,它应该容易得多。

safeServiceStart.bat

@echo off
:: This script originally authored by Eric Falsken

IF [%1]==[] GOTO usage
IF [%2]==[] GOTO usage

ping -n 1 %1 | FIND "TTL=" >NUL
IF errorlevel 1 GOTO SystemOffline
SC \\%1 query %2 | FIND "STATE" >NUL
IF errorlevel 1 GOTO SystemOffline

:ResolveInitialState
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO StartService
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO StartedService
SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
echo Service State is changing, waiting for service to resolve its state before making changes
sc \\%1 query %2 | Find "STATE"
timeout /t 2 /nobreak >NUL
GOTO ResolveInitialState

:StartService
echo Starting %2 on \\%1
sc \\%1 start %2 >NUL

GOTO StartingService
:StartingServiceDelay
echo Waiting for %2 to start
timeout /t 2 /nobreak >NUL
:StartingService
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 1 GOTO StartingServiceDelay

:StartedService
echo %2 on \\%1 is started
GOTO:eof

:SystemOffline
echo Server \\%1 is not accessible or is offline
GOTO:eof

:usage
echo %0 [system name] [service name]
echo Example: %0 server1 MyService
echo.
GOTO:eof

safeServiceStop.bat

@echo off
:: This script originally authored by Eric Falsken

IF [%1]==[] GOTO usage
IF [%2]==[] GOTO usage

ping -n 1 %1 | FIND "TTL=" >NUL
IF errorlevel 1 GOTO SystemOffline
SC \\%1 query %2 | FIND "STATE" >NUL
IF errorlevel 1 GOTO SystemOffline

:ResolveInitialState
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO StopedService
SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
echo Service State is changing, waiting for service to resolve its state before making changes
sc \\%1 query %2 | Find "STATE"
timeout /t 2 /nobreak >NUL
GOTO ResolveInitialState

:StopService
echo Stopping %2 on \\%1
sc \\%1 stop %2 %3 >NUL

GOTO StopingService
:StopingServiceDelay
echo Waiting for %2 to stop
timeout /t 2 /nobreak >NUL
:StopingService
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
IF errorlevel 1 GOTO StopingServiceDelay

:StopedService
echo %2 on \\%1 is stopped
GOTO:eof

:SystemOffline
echo Server \\%1 or service %2 is not accessible or is offline
GOTO:eof

:usage
echo Will cause a remote service to STOP (if not already stopped).
echo This script will waiting for the service to enter the stopped state if necessary.
echo.
echo %0 [system name] [service name] {reason}
echo Example: %0 server1 MyService
echo.
echo For reason codes, run "sc stop"
GOTO:eof

safeServiceRestart.bat

@echo off
:: This script originally authored by Eric Falsken

if [%1]==[] GOTO usage
if [%2]==[] GOTO usage

ping -n 1 %1 | FIND "TTL=" >NUL
IF errorlevel 1 GOTO SystemOffline
SC \\%1 query %2 | FIND "STATE" >NUL
IF errorlevel 1 GOTO SystemOffline

:ResolveInitialState
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO StartService
SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
echo Service State is changing, waiting for service to resolve its state before making changes
sc \\%1 query %2 | Find "STATE"
timeout /t 2 /nobreak >NUL
GOTO ResolveInitialState

:StopService
echo Stopping %2 on \\%1
sc \\%1 stop %2 %3 >NUL

GOTO StopingService
:StopingServiceDelay
echo Waiting for %2 to stop
timeout /t 2 /nobreak >NUL
:StopingService
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
IF errorlevel 1 GOTO StopingServiceDelay

:StopedService
echo %2 on \\%1 is stopped
GOTO StartService

:StartService
echo Starting %2 on \\%1
sc \\%1 start %2 >NUL

GOTO StartingService
:StartingServiceDelay
echo Waiting for %2 to start
timeout /t 2 /nobreak >NUL
:StartingService
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 1 GOTO StartingServiceDelay

:StartedService
echo %2 on \\%1 is started
GOTO:eof

:SystemOffline
echo Server \\%1 or service %2 is not accessible or is offline
GOTO:eof

:usage
echo Will restart a remote service, waiting for the service to stop/start (if necessary)
echo.
echo %0 [system name] [service name] {reason}
echo Example: %0 server1 MyService
echo.
echo For reason codes, run "sc stop"
GOTO:eof

请注意,timeout命令仅在Vista和Server 2003(及更高版本)中可用。在Windows XP上,使用NT资源工具包中的SLEEP命令。(感谢詹姆斯)
埃里克·法尔肯

3
还要注意,用于检查服务器可用性的ping命令假定服务器位于IPv4上。包括“ -4”开关以强制ping使用IPv4可能会更好。
本杰明·韦格曼

1
我希望我有一个以上的支持。太好了
安东

2
@EricFalsken-您的脚本(否则很棒)中的“系统处于脱机状态”检测位与ipv6地址有关,因为ping不会为ipv6地址打印出TTL。添加-4强制ipv4 ping,但这并非总是可行的
radai

1
很棒的脚本!我发现我确实需要ping -4并按以下建议用ping替换超时。另外,我认为最好删除sc start / stop命令的> NULL,以便您实际上可以看到任何异常消息。我还在那里添加了“ IF errorlevel 1 EXIT 1”,以便脚本在诸如访问被拒绝之类的错误上停止而不是永远等待。
Henno Vermeulen 2014年

10

那么powershell和WaitForStatus呢?例如,以下脚本将在远程计算机上重新启动SQL Server:

$computer = "COMPUTER_NAME"
$me = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\user", (convertto-securestring "password" -asplaintext -force)
$restartSqlServer = { 
    $sqlServer = get-service mssqlserver
    $waitInterval = new-timespan -seconds 5
    if (-not ($sqlServer.Status -eq "Stopped")) {
        $sqlServer.Stop()
        $sqlServer.WaitForStatus('Stopped', $waitInterval) 
    }
    $sqlServer.Start()
    $sqlServer.WaitForStatus('Running', $waitInterval) 
}     
icm -ComputerName $computer -ScriptBlock $restartSqlServer -Credential $me 

你怎么称呼它?我忘记了有关Powershell的东西,比我记得的要多。
JustEngland 2011年

1
@JustEngland-只需将代码放入扩展名为“ .ps1”的文件中,然后从Poweshell控制台或ISE运行它就可以了。
andreister 2011年

8

我从未真正看到过专门执行此操作的工具,但是用C \ C#\ VB或任何其他可以轻松访问Service API的语言来敲除这样的实用程序将非常容易。这是C#中的示例。

using System;
using System.ComponentModel;
using System.ServiceProcess;

namespace SCSync
{
    class Program
    {
        private const int ERROR_SUCCESS = 0;

        private const int ERROR_INVALID_COMMAND_LINE = 1;
        private const int ERROR_NO_ACCESS = 2;
        private const int ERROR_COMMAND_TIMEOUT = 3;
        private const int ERROR_NO_SERVICE = 4;
        private const int ERROR_NO_SERVER = 5;
        private const int ERROR_INVALID_STATE = 6;
        private const int ERROR_UNSPECIFIED = 7;

        static int Main(string[] args)
        {

            if (args.Length < 2 || args.Length > 4)
            {
                ShowUsage();
                return ERROR_INVALID_COMMAND_LINE;
            }

            string serviceName = args[0];
            string command = args[1].ToUpper();
            string serverName = ".";
            string timeoutString = "30";
            int timeout;

            if (args.Length > 2)
            {
                if (args[2].StartsWith(@"\\"))
                {
                    serverName = args[2].Substring(2);
                    if (args.Length > 3)
                    {
                        timeoutString = args[3];
                    }
                }
                else
                {
                    timeoutString = args[2];
                }
            }

            if (!int.TryParse(timeoutString, out timeout))
            {
                Console.WriteLine("Invalid timeout value.\n");
                ShowUsage();
                return ERROR_INVALID_COMMAND_LINE;
            }

            try
            {
                ServiceController sc = new ServiceController(serviceName, serverName);
                switch (command)
                {
                    case "START":
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 0, timeout));
                        break;
                    case "STOP":
                        sc.Stop();
                        sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 0, timeout));
                        break;
                    case "PAUSE":
                        sc.Pause();
                        sc.WaitForStatus(ServiceControllerStatus.Paused, new TimeSpan(0, 0, 0, timeout));
                        break;
                    case "CONTINUE":
                        sc.Continue();
                        sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 0, timeout));
                        break;
                    default:
                        Console.WriteLine("Invalid command value.\n");
                        ShowUsage();
                        return ERROR_INVALID_COMMAND_LINE;
                }
            }
            catch (System.ServiceProcess.TimeoutException)
            {
                Console.WriteLine("Operation timed out.\n");
                return ERROR_COMMAND_TIMEOUT;
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("You are not authorized to perform this action.\n");
                return ERROR_NO_ACCESS;
            }
            catch (InvalidOperationException opEx)
            {
                Win32Exception winEx = opEx.InnerException as Win32Exception;
                if (winEx != null)
                {
                    switch (winEx.NativeErrorCode)
                    {
                        case 5: //ERROR_ACCESS_DENIED
                            Console.WriteLine("You are not authorized to perform this action.\n");
                            return ERROR_NO_ACCESS;
                        case 1722: //RPC_S_SERVER_UNAVAILABLE
                            Console.WriteLine("The server is unavailable or does not exist.\n");
                            return ERROR_NO_SERVER;
                        case 1060: //ERROR_SERVICE_DOES_NOT_EXIST
                            Console.WriteLine("The service does not exist.\n");
                            return ERROR_NO_SERVICE;
                        case 1056: //ERROR_SERVICE_ALREADY_RUNNING
                            Console.WriteLine("The service is already running.\n");
                            return ERROR_INVALID_STATE;
                        case 1062: //ERROR_SERVICE_NOT_ACTIVE
                            Console.WriteLine("The service is not running.\n");
                            return ERROR_INVALID_STATE;
                        default:
                            break;
                    }
                }
                Console.WriteLine(opEx.ToString());
                return ERROR_UNSPECIFIED;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return ERROR_UNSPECIFIED;
            }

            return ERROR_SUCCESS;
        }

        private static void ShowUsage()
        {
            Console.WriteLine("SCSync usage:\n");
            Console.WriteLine("SCSync.exe service command <server> <timeout>\n");
            Console.WriteLine("    service   The name of the service upon which the command will act. (Required)");
            Console.WriteLine("    command   The command to execute - one of: start|stop|pause|continue. (Required)");
            Console.WriteLine("    server    The name of the server on which the target service runs. This must start with \\. (Optional)");
            Console.WriteLine("    timeout   The timeout period in seconds in which the command should finish. The default is 30 seconds. (Optional)");
            Console.WriteLine("\n");
        }
    }
}

WaitForStatus只是一个轮询循环,可以用任何其他语言轻松替换。其余的只是OpenService和ControlService。


6

埃里克·法尔斯肯(Eric Falsken)的解决方案运行完美。+1。

但我想补充一点,超时命令有时会因错误而失败:“不支持输入重定向,请立即退出进程”

为了解决这个问题,我不得不替换了timeout命令:

timeout /t 2 /nobreak >NUL

具有以下内容:

ping -n 2 127.0.0.1  1>NUL

5

在2011年10月20日编辑-更新了我的代码。我在完全调试之前发布了它。非常感谢Eric Falsken。多么好的解决方案。我调整了Eric的代码(顺便说一句,如果您打算使用它,则会发现一些印刷错误)。我添加了日志记录和一些额外的错误检查功能,以解决Eric未解决的某些情况。由于我对服务重启最感兴趣(而不仅仅是停止和/或启动),因此我仅以Eric的重启代码为基础。无论如何,我正在发布我的版本,希望您喜欢!

@ECHO off
:: This script originally authored by Eric Falsken http://stackoverflow.com/
:: Revised for by George Perkins 10/20/2011
IF [%1]==[] GOTO Usage
IF [%2]==[] GOTO Usage

:SetLocalVariables
SET /A MyDelay=0 
SET MyHours=%time:~0,2%
IF %MyHours%==0 SET MyHours=00
IF %MyHours%==1 SET MyHours=01
IF %MyHours%==2 SET MyHours=02
IF %MyHours%==3 SET MyHours=03
IF %MyHours%==4 SET MyHours=04
IF %MyHours%==5 SET MyHours=05
IF %MyHours%==6 SET MyHours=06
IF %MyHours%==7 SET MyHours=07
IF %MyHours%==8 SET MyHours=08
IF %MyHours%==9 SET MyHours=09
SET MyMinutes=%time:~3,2%
SET MySeconds=%time:~6,2%
SET MyHundredths=%time:~9,2%
SET MyMonth=%date:~4,2%
SET MyDay=%date:~-7,2%
SET MyCentury=%date:~-4,4%
SET MyTimeStamp=%MyCentury%%MyMonth%%MyDay%%MyHours%%MyMinutes%%MySeconds%
IF "%3" == "" (
         SET MyLog=C:\Temp
   ) ELSE (
         SET MyLog=%3
   ) 
SET MyLogFile=%MyLog%\ServiceRestart%MyTimeStamp%.log
ECHO.
ECHO. >> %MyLogFile%
ECHO ------------- ------------- %MyHours%:%MyMinutes%:%MySeconds%.%MyHundredths% %MyMonth%/%MyDay%/%MyCentury% ------------- ------------- 
ECHO ------------- ------------- %MyHours%:%MyMinutes%:%MySeconds%.%MyHundredths% %MyMonth%/%MyDay%/%MyCentury% ------------- ------------- >> %MyLogFile% 
ECHO Begin batch program %0. 
ECHO Begin batch program %0. >> %MyLogFile%
ECHO Logging to file %MyLogFile%. 
ECHO Logging to file %MyLogFile%. >> %MyLogFile% 
ECHO Attempting to restart service %2 on computer %1.
ECHO Attempting to restart service %2 on computer %1. >> %MyLogFile%

PING -n 1 %1 | FIND "TTL=" >> %MyLogFile%
IF errorlevel 1 IF NOT errorlevel 2 GOTO SystemOffline
SC \\%1 query %2 | FIND "FAILED 1060" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO InvalidServiceName
SC \\%1 query %2 | FIND "STATE" >> %MyLogFile%
IF errorlevel 1 IF NOT errorlevel 2 GOTO SystemOffline

:ResolveInitialState
SET /A MyDelay+=1
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO StartService
SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
ECHO Service State is changing, waiting %MyDelay% seconds for service to resolve its state before making changes.
ECHO Service State is changing, waiting %MyDelay% seconds for service to resolve its state before making changes. >> %MyLogFile%
TIMEOUT /t %MyDelay% /nobreak >> %MyLogFile%
GOTO ResolveInitialState

:StopService
SET /A MyDelay=0
ECHO Stopping %2 on \\%1.
ECHO Stopping %2 on \\%1. >> %MyLogFile%
SC \\%1 stop %2 | FIND "FAILED" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO Unstoppable

:StoppingServiceDelay
SET /A MyDelay+=1
IF %MyDelay%==21 GOTO MaybeUnStoppable
ECHO Waiting %MyDelay% seconds for %2 to stop.
ECHO Waiting %MyDelay% seconds for %2 to stop. >> %MyLogFile%
TIMEOUT /t %MyDelay% /nobreak >> %MyLogFile%
:StoppingService
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO StoppedService
SC \\%1 query %2 | FIND "STATE" | FIND "STOP_PENDING" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO StoppingServiceDelay
GOTO StoppingServiceDelay

:MaybeUnStoppable
:: If we got here we waited approximately 3 mintues and the service has not stopped.
SC \\%1 query %2 | FIND "NOT_STOPPABLE" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO OneLastChance
GOTO Unstoppable 

:OneLastChance
SC \\%1 stop %2 >> %MyLogFile%
SET /A MyDelay+=1
ECHO Waiting %MyDelay% seconds for %2 to stop.
ECHO Waiting %MyDelay% seconds for %2 to stop. >> %MyLogFile%
TIMEOUT /t %MyDelay% /nobreak >> %MyLogFile%
SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO StoppedService
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >> %MyLogFile%
IF errorlevel 1 IF NOT errorlevel 2 GOTO UnknownState
SC \\%1 query %2 | FIND "NOT_STOPPABLE" >> %MyLogFile%
IF errorlevel 0 IF NOT errorlevel 1 GOTO Unstoppable
GOTO StoppingServiceDelay

:StoppedService
ECHO %2 on \\%1 is stopped.
ECHO %2 on \\%1 is stopped. >> %MyLogFile%
GOTO StartService

:StartService
SET /A MyDelay=0 
ECHO Starting %2 on \\%1.
ECHO Starting %2 on \\%1. >> %MyLogFile%
SC \\%1 start %2 >> %MyLogFile%

GOTO StartingService
:StartingServiceDelay
SET /A MyDelay+=1
ECHO Waiting %MyDelay% seconds for %2 to start.
ECHO Waiting %MyDelay% seconds for %2 to start. >> %MyLogFile%
TIMEOUT /t %MyDelay% /nobreak >> %MyLogFile%
:StartingService
SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >> %MyLogFile%
IF errorlevel 1 IF NOT errorlevel 2 GOTO StartingServiceDelay

:StartedService
ECHO %2 on \\%1 is started.
ECHO %2 on \\%1 is started. >> %MyLogFile%
GOTO EndExit

:SystemOffline
ECHO Failure! Server \\%1 or service %2 is not accessible or is offline!
ECHO Failure! Server \\%1 or service %2 is not accessible or is offline! >> %MyLogFile%
ECHO See log file %MyLogFile% for details!
GOTO EndExit

:InvalidServiceName
ECHO Failure! Service %2 is not valid!
ECHO Failure! Service %2 is not valid! >> %MyLogFile%
ECHO See log file %MyLogFile% for details!
GOTO EndExit

:UnknownState
ECHO Failure! Service %2 in an unknown state and cannot be stopped!
ECHO Failure! Service %2 in an unknown state and cannot be stopped! >> %MyLogFile%
ECHO See log file %MyLogFile% for details!
GOTO EndExit

:UnStoppable
ECHO Failure! Service %2 cannot be stopped! Check dependencies or system state.
ECHO Failure! Service %2 cannot be stopped! Check dependencies or system state. >> %MyLogFile%
ECHO See log file %MyLogFile% for details!
GOTO EndExit

:Usage
ECHO Will restart a remote service, waiting for the service to stop/start (if necessary).
ECHO.
ECHO Usage:
ECHO %0 [system name] [service name] [logfile path]
ECHO Example: %0 server1 MyService C:\Temp\Log
ECHO.
GOTO EndExit

:EndExit
ECHO.
ECHO %0 Ended.
ECHO.

4

那么PowerShell和Restart-Service命令集又如何呢?:)

Get-Service W3SVC -computer myserver | Restart-Service

真好 记住要从启用了admin的shell运行(不是很清晰的错误消息)。
马特·威尔基

对于PowerShell 2.0中,你必须求助于Get-Service W3SVC -computer myserver | Set-Service -Status Stopped; Get-Service W3SVC -computer myserver | Set-Service -Status Running
sferencik 2015年

2

Eric Falsken的脚本非常适合此目的。但是请注意,它们使用timeout命令,该命令仅在Vista / Server2003及更高版本中可用。对于XP计算机,您可以改用NT资源工具包中的sleep.exe。(这应该是对Eric的回答的一种表扬,但没有足够的代表来做到这一点)。


2

我改进了Eric Falsken的脚本,并由Gerorge Perkins进行了修订。

变化:

  • 现在,它不仅是重启脚本。该脚本可以启动,停止和重新启动本地或远程服务。
  • 删除了日志记录(如果需要,可以通过启动SCRIPT_NAME.bat> logfile.txt来使用它);
  • 稀疏优化。

    @ECHO关闭
    ::此脚本最初由Eric Falsken创作,http://stackoverflow.com/
    ::乔治·珀金斯(George Perkins)修订,2011年10月20日
    ::由Armando Contestabile修订02/23/2015
    IF“%1” ==“”转到用法
    IF“%2” ==“”转到用法
    
    SET ACTION =%1
    SET SERVICENAME =%2
    
    如果“%3” ==“”(
        SET SYSTEMNAME =%COMPUTERNAME%
    )ELSE(
        SET SYSTEMNAME =%3
    )
    
    如果“%ACTION%” ==“停止”(
        SET ACTION =停止
    )ELSE IF“%ACTION%” ==“ STOP”(
        SET ACTION =停止
    )ELSE IF“%ACTION%” ==“开始”(
        SET ACTION =开始
    )ELSE IF“%ACTION%” ==“ START”(
        SET ACTION =开始
    )ELSE IF“%ACTION%” ==“重新启动”(
        设置动作=重新启动
    )ELSE IF“%ACTION%” ==“重新启动”(
        设置动作=重新启动
    )ELSE GOTO用法
    
    SET STATE =
    SET CURRENT_STATUS =
    设置/ A DEFAULT_DELAY = 5
    SET / A SLEEP_COUNT = 0
    设置/ A重新启动= 0
    设置/ A MAX_WAIT_PERIODS = 5
    
    回声。
    尝试在计算机%SYSTEMNAME%上为%ACTION%服务%SERVICENAME%的ECHO。
    
    PING -n 1%SYSTEMNAME%| 查找“ TTL =”> nul 2>&1
    如果错误级别1如果错误级别2(
        回声失败!服务器\\%SYSTEMNAME%或服务%SERVICENAME%无法访问或离线!
        出口/ B 1
    )
    SC \\%SYSTEMNAME%查询%SERVICENAME%| 查找“ FAILED 1060”> nul 2>&1
    如果错误级别0如果错误级别1(
        回声失败!服务%SERVICENAME%无效!
        出口/ B 2
    )
    SC \\%SYSTEMNAME%查询%SERVICENAME%| 查找“状态”> nul 2>&1
    如果错误级别1如果错误级别2(
        回声失败!服务器\\%SYSTEMNAME%或服务%SERVICENAME%无法访问或离线!
        出口/ B 3
    )
    
    :调度
    FOR / f“令牌= *” %% i IN('SC \\%SYSTEMNAME%查询%SERVICENAME%^ |查找“ STATE”')请设置STATE = %% i
    
    回声%STATE%| FINDSTR / C:“ 1”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS =已停止
    回声%STATE%| FINDSTR / C:“ 2”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS = START_PENDING
    回声%STATE%| FINDSTR / C:“ 3”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS = STOP_PENDING
    回声%STATE%| FINDSTR / C:“ 4”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS = RUNNING
    回声%STATE%| FINDSTR / C:“ 5”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS = CONTINUE_PENDING
    回声%STATE%| FINDSTR / C:“ 6”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS = PAUSE_PENDING
    回声%STATE%| FINDSTR / C:“ 7”>空
    如果%ERRORLEVEL%== 0 SET CURRENT_STATUS = PAUSED
    
    ECHO当前服务状态为%CURRENT_STATUS%
    
    如果不是“%CURRENT_STATUS%” ==“正在运行”如果不是“%CURRENT_STATUS%” ==“已停止”如果不是“%CURRENT_STATUS%” ==“已暂停”(
        IF“%SLEEP_COUNT%” ==“%MAX_WAIT_PERIODS%”(
            ECHO服务状态不会改变。脚本执行被取消。
            出口/ B 4
        )
        ECHO服务状态正在更改,正在等待%DEFAULT_DELAY%秒...
        睡眠%DEFAULT_DELAY%
        设置/ A SLEEP_COUNT + = 1
        转到调度
    )
    
    如果“%ACTION%” ==“ START”(
        如果“%CURRENT_STATUS%” ==“ RUNNING”(
            ECHO服务%SERVICENAME%正在运行。
            转到EndExit
        )ELSE(
            转到StartService
        )
    )ELSE IF“%ACTION%” ==“重新启动”(
        如果“%CURRENT_STATUS%” ==“ RUNNING”(
            如果%RESTARTED%== 1(
                ECHO服务%SERVICENAME%重新启动。
                转到EndExit
            )
            SET / A SLEEP_COUNT = 0
            转到StopService
        )ELSE(
            设置/ A重新启动= 1
            转到StartService
        )
    )ELSE IF“%ACTION%” ==“停止”(
        如果“%CURRENT_STATUS%” ==“ STOPPED”(
            ECHO服务%SERVICENAME%已停止。
            转到EndExit
        )ELSE(
            转到StopService
        )
    )
    
    :StartService
    ECHO在\\%SYSTEMNAME%上以%SERVICENAME%开头
    SC \\%SYSTEMNAME%开始%SERVICENAME%> nul 2>&1
    SET SLEEP_COUNT = 0
    转到调度
    
    :StopService
    ECHO正在\\%SYSTEMNAME%上停止%SERVICENAME%
    SC \\%SYSTEMNAME%停止%SERVICENAME%> nul 2>&1
    SET SLEEP_COUNT = 0
    转到调度
    
    :用法
    ECHO此脚本可以启动/停止/重新启动本地或远程服务,等待服务停止/启动^(如有必要,^)。
    回声。
    回声用法:
    回声%0 ^ <开始^ |停止^ |重新启动^> ^ <服务^> [系统]
    回声。
    回显如果未提供SYSTEM,则脚本将尝试在本地系统上执行。
    出口/ B 5
    
    :结束退出
    回声。
    退出/ B 0
    

我收到“无法将'SLEEP'识别为内部或外部命令”
kofifus

您正在使用哪个Windows版本?
Armando竞赛

0

在服务指示服务已成功启动或停止之前,不应返回NET START和NET STOP。


1
我可以确认远程终止服务时,它们确实会服务停止之前返回。我每天要这样做几次。
杰森·杰克逊

有趣。我以为他们一直等到服务已向SCM指示SERVICE_STARTED或SERVICE_STOPPED代码。

让我澄清一下先前的评论。以我的经验,Windows可能认为该服务已停止,但实际上它仍在关闭。我不知道这是因为服务异常还是这是正常情况。我不止一次遇到它。我不会指望该服务在停止后立即被拆除,并且肯定不会指望该服务在开始后立即营业。
杰森·杰克逊2009年

该服务可以告诉Windows任何需要的信息。它可以在告知Windows已“启动”后延迟执行,而在告知Windows已“停止”后可以进行其他拆卸。糟糕的恕我直言,但完全有可能。

1
一项更正:根据文档,将状态设置为停止后,SCM可以随时终止服务。以我的经验,这通常会立即发生,即对SetServiceStatus的调用永远不会返回。因此,将状态设置为停止之前,需要进行任何必要的拆卸。(当多个服务共享一个进程时,这可能不适用,尽管我怀疑在最后一个服务停止时也是如此。)
哈里·约翰斯顿

0

我对该脚本进行了较小的更改,以使其可以在Windows 10或类似版本下运行。命令“ sleep”已替换为命令“ timeout”。

@ECHO off
:: This script originally authored by Eric Falsken http://stackoverflow.com/
:: Revised by George Perkins 10/20/2011
:: Revised by Armando Contestabile 02/23/2015
:: Revised by Sascha Jelinek 11/13/2020
IF "%1"=="" GOTO Usage
IF "%2"=="" GOTO Usage

SET ACTION=%1
SET SERVICENAME=%2

IF "%3"=="" (
    SET SYSTEMNAME=%COMPUTERNAME%
) ELSE (
    SET SYSTEMNAME=%3
)

IF "%ACTION%" == "stop" (
    SET ACTION=STOP
) ELSE IF "%ACTION%" == "STOP" (
    SET ACTION=STOP
) ELSE IF "%ACTION%" == "start" (
    SET ACTION=START
) ELSE IF "%ACTION%" == "START" (
    SET ACTION=START
) ELSE IF "%ACTION%" == "restart" (
    SET ACTION=RESTART
) ELSE IF "%ACTION%" == "RESTART" (
    SET ACTION=RESTART
) ELSE GOTO Usage

SET STATE=
SET CURRENT_STATUS=
SET /A DEFAULT_DELAY=5
SET /A SLEEP_COUNT=0
SET /A RESTARTED=0
SET /A MAX_WAIT_PERIODS=5

ECHO.
ECHO Attempting to %ACTION% service %SERVICENAME% on computer %SYSTEMNAME%.

PING -n 1 %SYSTEMNAME% | FIND "Antwort von" >nul 2>&1
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 (
    ECHO Failure!! Server \\%SYSTEMNAME% or service %SERVICENAME% is not accessible or is offline!
    EXIT /B 1
)
SC \\%SYSTEMNAME% query %SERVICENAME% | FIND "FAILED 1060" >nul 2>&1
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 (
    ECHO Failure! Service %SERVICENAME% is not valid!
    EXIT /B 2
)
SC \\%SYSTEMNAME% query %SERVICENAME% | FIND "STATE" >nul 2>&1
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 (
    ECHO Failure! Server \\%SYSTEMNAME% or service %SERVICENAME% is not accessible or is offline!
    EXIT /B 3
)

:Dispatch
FOR /f "tokens=*" %%i IN ('SC \\%SYSTEMNAME% query %SERVICENAME% ^| FIND "STATE"') DO SET STATE=%%i

ECHO %STATE% | FINDSTR /C:"1" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=STOPPED
ECHO %STATE% | FINDSTR /C:"2" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=START_PENDING
ECHO %STATE% | FINDSTR /C:"3" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=STOP_PENDING
ECHO %STATE% | FINDSTR /C:"4" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=RUNNING
ECHO %STATE% | FINDSTR /C:"5" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=CONTINUE_PENDING
ECHO %STATE% | FINDSTR /C:"6" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=PAUSE_PENDING
ECHO %STATE% | FINDSTR /C:"7" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=PAUSED

ECHO Current status of service is %CURRENT_STATUS%

IF NOT "%CURRENT_STATUS%"=="RUNNING" IF NOT "%CURRENT_STATUS%"=="STOPPED" IF NOT "%CURRENT_STATUS%"=="PAUSED" (
    IF "%SLEEP_COUNT%"=="%MAX_WAIT_PERIODS%" (
        ECHO Service state won't change. Script exececution is canceled.
        EXIT /B 4
    )
    ECHO Service State is changing, waiting %DEFAULT_DELAY% seconds...
    TIMEOUT /t %DEFAULT_DELAY% /NOBREAK
    SET /A SLEEP_COUNT+=1
    GOTO Dispatch
)

IF "%ACTION%"=="START" (
    IF "%CURRENT_STATUS%"=="RUNNING" (
        ECHO Service %SERVICENAME% is running.
        GOTO EndExit
    ) ELSE (
        GOTO StartService
    )
) ELSE IF "%ACTION%"=="RESTART" (
    IF "%CURRENT_STATUS%"=="RUNNING" (
        IF %RESTARTED%==1 (
            ECHO Service %SERVICENAME% restarted.
            GOTO EndExit
        )
        SET /A SLEEP_COUNT=0
        GOTO StopService
    ) ELSE (
        SET /A RESTARTED=1
        GOTO StartService
    )
) ELSE IF "%ACTION%"=="STOP" (
    IF "%CURRENT_STATUS%"=="STOPPED"  (
        ECHO Service %SERVICENAME% is stopped.
        GOTO EndExit
    ) ELSE (
        GOTO StopService
    )
)

:StartService
ECHO Starting %SERVICENAME% on \\%SYSTEMNAME%
SC \\%SYSTEMNAME% start %SERVICENAME% >nul 2>&1
SET SLEEP_COUNT=0
GOTO Dispatch

:StopService
ECHO Stopping %SERVICENAME% on \\%SYSTEMNAME%
SC \\%SYSTEMNAME% stop %SERVICENAME% >nul 2>&1
SET SLEEP_COUNT=0
GOTO Dispatch

:Usage
ECHO This script can start/stop/restart a local or remote service, waiting for the service to stop/start ^(if necessary^).
ECHO.
ECHO Usage:
ECHO %0 ^<start^|stop^|restart^> ^<SERVICE^> [SYSTEM]
ECHO.
ECHO If no SYSTEM is provided, the script attempts to execute on the local system.
EXIT /B 5

:EndExit
ECHO.
EXIT /B 0

-1

我不相信您可以使用直接的dos命令执行此操作。您可以检查Code Project或其他类似的站点,以查看是否已经有针对此的自定义解决方案。

如果没有,您可以编写一个辅助Windows服务,以通过WCF端点公开启动/停止功能来为您执行此操作。要远程访问该辅助服务,您可以编写一个简单的控制台应用程序,连接到该服务以启动/停止有问题的Windows服务。由于它是一个控制台应用程序,它将模仿从命令行工作并在完成(或发生错误)之前不返回的预期行为。这不是您要寻找的简单,简单的解决方案,但我将其扔出去考虑。


对于使用简单的蝙蝠脚本即可解决的任务而言,这太过残酷了……记住KISS原则
Oscar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.