Windows command to get service status?

后端 未结 12 761
渐次进展
渐次进展 2021-02-01 04:48

I need to know the status of a service at the end of my batch script which restarts services using \"net stop thingie\" and \"net start thingie\".

In my most favorite id

相关标签:
12条回答
  • 2021-02-01 05:11

    Maybe this could be the best way to start a service and check the result

    Of course from inside a Batch like File.BAT put something like this example but just replace "NameOfSercive" with the service name you want and replace the REM lines with your own code:

    @ECHO OFF
    
    REM Put whatever your Batch may do before trying to start the service
    
    net start NameOfSercive 2>nul
    if errorlevel 2 goto AlreadyRunning
    if errorlevel 1 goto Error
    
    REM Put Whatever you want in case Service was not running and start correctly
    
    GOTO ContinueWithBatch
    
    :AlreadyRunning
    REM Put Whatever you want in case Service was already running
    GOTO ContinueWithBatch
    
    :Error
    REM Put Whatever you want in case Service fail to start
    GOTO ContinueWithBatch
    
    :ContinueWithBatch
    
    REM Put whatever else your Batch may do
    

    Another thing is to check for its state without changing it, for that there is a much more simple way to do it, just run:

    net start
    

    As that, without parameters it will show a list with all services that are started...

    So a simple grep or find after it on a pipe would fit...

    Of course from inside a Batch like File.BAT put something like this example but just replace "NameOfSercive" with the service name you want and replace the REM lines with your own code:

    @ECHO OFF
    
    REM Put here any code to be run before check for Service
    
    SET TemporalFile=TemporalFile.TXT
    NET START | FIND /N "NameOfSercive" > %TemporalFile%
    SET CountLines=0
    FOR /F %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
    IF 0==%CountLines% GOTO ServiceIsNotRunning
    
    REM Put here any code to be run if Service Is Running
    
    GOTO ContinueWithBatch
    
    :ServiceIsNotRunning
    
    REM Put here any code to be run if Service Is Not Running
    
    GOTO ContinueWithBatch
    :ContinueWithBatch
    DEL -P %TemporalFile% 2>nul
    SET TemporalFile=
    
    REM Put here any code to be run after check for Service
    

    Hope this can help!! It is what i normally use.

    0 讨论(0)
  • 2021-02-01 05:13

    Ros the code i post also is for knowing how many services are running...

    Imagine you want to know how many services are like Oracle* then you put Oracle instead of NameOfSercive... and you get the number of services like Oracle* running on the variable %CountLines% and if you want to do something if there are only 4 you can do something like this:

    IF 4==%CountLines% GOTO FourServicesAreRunning

    That is much more powerfull... and your code does not let you to know if desired service is running ... if there is another srecive starting with same name... imagine: -ServiceOne -ServiceOnePersonal

    If you search for ServiceOne, but it is only running ServiceOnePersonal your code will tell ServiceOne is running...

    My code can be easly changed, since it reads all lines of the file and read line by line it can also do whatever you want to each service... see this:

    @ECHO OFF
    REM Put here any code to be run before check for Services
    
    SET TemporalFile=TemporalFile.TXT
    NET START > %TemporalFile%
    SET CountLines=0
    FOR /F "delims=" %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
    SETLOCAL EnableDelayedExpansion
    SET CountLine=0
    FOR /F "delims=" %%X IN (%TemporalFile%) DO @(
     SET /A CountLine=1+CountLine
    
     REM Do whatever you want to each line here, remember first and last are special not service names
    
     IF 1==!CountLine! (
    
       REM Do whatever you want with special first line, not a service.
    
     ) ELSE IF %CountLines%==!CountLine! (
    
       REM Do whatever you want with special last line, not a service.
    
     ) ELSE (
    
       REM Do whatever you want with rest lines, for each service.
       REM    For example echo its position number and name:
    
       echo !CountLine! - %%X
    
       REM    Or filter by exact name (do not forget to not remove the three spaces at begining):
       IF "   NameOfService"=="%%X" (
    
         REM Do whatever you want with Service filtered.
    
       )
     )
    
     REM Do whatever more you want to all lines here, remember two first are special as last one
    
    )
    
    DEL -P %TemporalFile% 2>nul
    SET TemporalFile=
    
    REM Put here any code to be run after check for Services
    

    Of course it only list running services, i do not know any way net can list not running services...

    Hope this helps!!!

    0 讨论(0)
  • 2021-02-01 05:13

    Have you tried sc.exe?

    C:\> for /f "tokens=2*" %a in ('sc query audiosrv ^| findstr STATE') do echo %b
    4  RUNNING
    
    C:\> for /f "tokens=2*" %a in ('sc query sharedaccess ^| findstr STATE') do echo %b
    1  STOPPED
    

    Note that inside a batch file you'd double each percent sign.

    0 讨论(0)
  • 2021-02-01 05:14

    Well i see "Nick Kavadias" telling this:

    "according to this http://www.computerhope.com/nethlp.htm it should be NET START /LIST ..."

    If you type in Windows XP this:

    NET START /LIST
    

    you will get an error, just type instead

    NET START
    

    The /LIST is only for Windows 2000... If you fully read such web you would see the /LIST is only on Windows 2000 section.

    Hope this helps!!!

    0 讨论(0)
  • 2021-02-01 05:17

    Using Windows Script:

    Set ComputerObj = GetObject("WinNT://MYCOMPUTER")    
    ComputerObj.Filter = Array("Service")    
    For Each Service in ComputerObj    
        WScript.Echo "Service display name = " & Service.DisplayName    
        WScript.Echo "Service account name = " & Service.ServiceAccountName    
        WScript.Echo "Service executable   = " & Service.Path    
        WScript.Echo "Current status       = " & Service.Status    
    Next
    

    You can easily filter the above for the specific service you want.

    0 讨论(0)
  • 2021-02-01 05:17

    If PowerShell is available to you...

    Get-Service -DisplayName *Network* | ForEach-Object{Write-Host $_.Status : $_.Name}
    

    Will give you...

    Stopped : napagent
    Stopped : NetDDE
    Stopped : NetDDEdsdm
    Running : Netman
    Running : Nla
    Stopped : WMPNetworkSvc
    Stopped : xmlprov
    

    You can replace the ****Network**** with a specific service name if you just need to check one service.

    0 讨论(0)
提交回复
热议问题