web-dev-qa-db-fra.com

Arrêt / démarrage d'un service Windows distant et attente de son ouverture / fermeture

La meilleure réponse à cette question me dit comment arrêter/démarrer un service distant. Génial. Maintenant, tout ce dont j'ai besoin est d'attendre la fin de l'arrêt/démarrage. Donc, ce que je recherche, c'est une commande dos pour:

  1. Démarrer un service, doit revenir uniquement après le démarrage du service (ou après un délai d'expiration, augmentant le niveau d'erreur)
  2. Arrêter un service, ne revenir qu'après l'arrêt du service
66
ripper234

J'ai créé un ensemble de scripts batch qui utilisent sc.exe pour cela. Ils sont joints ci-dessous. Pour exécuter ces scripts, vous devez être un utilisateur avec des droits d'administration sur la machine cible et l'exécuter à partir d'un ordinateur membre du même domaine. Il est possible de le configurer pour pouvoir s'exécuter à l'extérieur du domaine (comme à partir d'un VPN), mais il existe de nombreuses couches de sécurité à utiliser via les pare-feu, DCOM et les informations d'identification de sécurité.

Un de ces jours, je vais trouver l'équivalent PowerShell, ce qui devrait être beaucoup plus facile.

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
117
Eric Falsken

Qu'en est-il de PowerShell et de WaitForStatus? Par exemple, le script ci-dessous redémarrerait SQL Server sur une machine distante:

$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 
10
andreister

Je n'ai jamais vu quelque chose qui le fasse spécifiquement, mais il serait assez facile de supprimer un tel utilitaire en C\C #\VB ou tout autre langage qui donne un accès facile à l'API de service. Voici un exemple de quelque chose en 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");
        }
    }
}

Le WaitForStatus est juste une boucle d'interrogation et pourrait être facilement remplacé dans n'importe quelle autre langue. Le reste n'est qu'OpenService et ControlService.

8
Stephen Martin

La solution d'Eric Falsken fonctionne parfaitement. +1.

Mais je voudrais ajouter que la commande timeout échouait parfois avec une erreur: "La redirection d'entrée n'est pas prise en charge, ce qui quitte le processus immédiatement"

Pour résoudre ce problème, j'ai dû remplacer la commande timeout:

timeout /t 2 /nobreak >NUL

avec ce qui suit:

ping -n 2 127.0.0.1  1>NUL
6
Edward Olamisan

Modifier le 20/10/2011 - mise à jour de mon code. Je l'ai posté avant de déboguer complètement. Un grand merci à Eric Falsken. Quelle excellente solution. J'ai modifié le code d'Eric (BTW recherche quelques erreurs typographiques si vous avez l'intention de l'utiliser). J'ai ajouté la journalisation et quelques vérifications d'erreurs supplémentaires pour certaines conditions dont Eric n'a pas tenu compte. Étant donné que je suis le plus intéressé par un redémarrage du service (pas seulement l'arrêt et/ou le démarrage), je n'ai construit que sur le code de redémarrage d'Eric. Quoi qu'il en soit, je poste ma version, j'espère qu'elle vous plaira!

@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.
5
George Perkins

Qu'en est-il de PowerShell et commandlet Restart-Service ? :)

Get-Service W3SVC -computer myserver | Restart-Service
4
Jan Remunda

Les scripts d'Eric Falsken sont fantastiques à cet effet. Mais notez qu'ils utilisent la commande timeout qui n'est disponible que dans Vista/Server2003 et plus récent. Pour une machine XP vous pouvez utiliser sleep.exe à partir du Kit de ressources NT à la place. (Cela devrait être un commentaire de la réponse d'Eric mais pas assez de représentant pour le faire).

2
James Holland

J'ai amélioré le script d'Eric Falsken et révisé par Gerorge Perkins.

Changements:

  • maintenant ce n'est plus seulement un script de redémarrage. Le script peut démarrer, arrêter et redémarrer un service local ou distant;
  • suppression de la journalisation (si vous le souhaitez, vous pouvez l'utiliser simplement en lançant SCRIPT_NAME.bat> logfile.txt);
  • optimisations clairsemées.

    @ECHO off 
     :: Ce script a été écrit à l'origine par Eric Falsken http://stackoverflow.com/
     :: Révisé par George Perkins le 20/10/2011 
     :: Révisé par Armando Contestabile 23/02/2015 
     IF "% 1" == "" Utilisation GOTO 
     IF "% 2" == "" Utilisation GOTO 
     
     SET ACTION =% 1 
     SET SERVICENAME =% 2 
     
     IF "% 3" == "" (
     SET SYSTEMNAME =% COMPUTERNAME% 
     ) AUTRE (
     SET SYSTEMNAME =% 3 
    ) 
     
     IF "% ACTION%" == "stop" (
     SET ACTION = STOP 
    ) ELSE SI "% ACTION%" == "STOP" (
     SET ACTION = STOP 
    ) ELSE SI "% ACTION%" == "start" (
     SET ACTION = START 
    ) ELSE IF "% ACTION%" == "START" (
     SET ACTION = START 
    ) ELSE IF "% ACTION%" == " redémarrer "(
     SET ACTION = RESTART 
    ) ELSE IF"% ACTION% "==" RESTART "(
     SET ACTION = RESTART 
    ) ELSE GOTO Utilisation 
     
     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 Tentative de% ACTION% service% SERVICENAME% sur l'ordinateur% SYSTEMNAME%. 
     
     PING -n 1% SYSTEMNAME% | FIND "TTL ="> 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! Le service% SERVICENAME% n'est pas valide! 
     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% n'est pas accessible ou est hors ligne! 
     EXIT/B 3 
    ) 
     
    : Envoi de 
     Jetons FOR/f "= *" %% i IN ('SC \\% SYSTEMNAME% requête% 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 L'état actuel du service est% CURRENT_STATUS% 
     
     SI NON "% CURRENT_STATUS%" == "RUNNING" IF NOT "% CURRENT_STATUS%" == "STOPPED" IF NOT "% CURRENT_STATUS%" == "PAUSE" (
     IF "% SLEEP_COUNT % "=="% MAX_WAIT_PERIODS% "(
     L'état du service ECHO ne changera pas. L'exécution du script est annulée. 
     EXIT/B 4 
    ) 
     Service ECHO L'état change, en attendant% DEFAULT_DELAY% secondes ... 
     SLEEP% DEFAULT_DELAY% 
     SET/A SLEEP_COUNT + = 1 
     GOTO Dispatch 
    ) 
     
     IF "% ACTION%" == "START" (
     IF "% CURRENT_STATUS%" == "RUNNING" (
     Le service ECHO% SERVICENAME% est en cours d'exécution. 
     GOTO EndExit 
    ) ELSE (
     GOTO StartService 
    ) 
    ) ELSE IF "% ACTION%" == "RESTART" (
     IF "% CURRENT_STATUS%" == "RUNNING" (
     IF% RESTARTED% == 1 (
     ECHO Servi ce% SERVICENAME% a redémarré. 
     GOTO EndExit 
    ) 
     SET/A SLEEP_COUNT = 0 
     GOTO StopService 
    ) ELSE (
     SET/A RESTARTED = 1 
     GOTO StartService 
    ) 
    ) AUTRE SI "% ACTION%" == "STOP" (
     SI "% CURRENT_STATUS%" = = = "STOPPED" (
     Le service ECHO% SERVICENAME% est arrêté. 
     GOTO EndExit 
    ) ELSE (
     GOTO StopService 
    ) 
    ) 
     
    : StartService 
     ECHO Démarrage de% SERVICENAME% sur \\% SYSTEMNAME% 
     SC \\% SYSTEMNAME% start% SERVICENAME%> nul 2> & 1 
     SET SLEEP_COUNT = 0 
     Envoi GOTO 
     
    : StopService 
     ECHO Arrêt de% SERVICENAME% sur \\% SYSTEMNAME% 
     SC \\% SYSTEMNAME% stop% SERVICENAME%> nul 2> & 1 
     SET SLEEP_COUNT = 0 
     GOTO Dispatch 
     
    : Usage 
     ECHO Ce script peut démarrer/arrêter/redémarrer un service local ou distant, en attendant que le service s'arrête/démarre ^ (si nécessaire ^) . 
     ECHO. 
     ECHO Utilisation: 
     ECHO% 0 ^ <start ^ | stop ^ | restart ^> ^ <SERVICE ^> [SYSTEM] 
     ECHO. 
     ECHO Si aucun SYSTEM n'est fourni, le script tente de s'exécuter sur le système local. 
     EXIT/B 5 
     
    : EndExit 
     ECHO. 
     SORTIE/B 0 
    
2
Armando Contestabile

NET START et NET STOP ne doivent pas revenir tant que le service n'a pas indiqué que le service a démarré ou s'est arrêté avec succès.

0
Joe