Windows command to get service status?

后端 未结 12 762
渐次进展
渐次进展 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:24

    my intention was to create a script which switches services ON and OFF (in 1 script)

    net start NameOfSercive 2>nul
    if errorlevel 2 goto AlreadyRunning
    if errorlevel 1 goto Error
    

    ...

    Helped a lot!! TYVM z666

    but when e.g. service is disabled(also errorlevel =2?)it goes to "AlreadyRuning"and never comes to

    if errorlevel 1 goto Error  ?!!
    

    i wanted an output for that case ...

     :AlreadyRunning
     net stop NameOfSercive
     if errorlevel 1 goto Error
    
    
     :Error
     Echo ERROR!!1!
     Pause
    

    my 2 Cents, hope this helps

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

    Using pstools - in particular psservice and "query" - for example:

    psservice query "serviceName"
    
    0 讨论(0)
  • 2021-02-01 05:30

    look also hier:

    NET START | FIND "Service name" > nul IF errorlevel 1 ECHO The service is not running

    just copied from: http://ss64.com/nt/sc.html

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

    You can call net start "service name" on your service. If it's not started, it'll start it and return errorlevel=0, if it's already started it'll return errorlevel=2.

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

    according to this http://www.computerhope.com/nethlp.htm it should be NET START /LIST but i can't get it to work on by XP box. I'm sure there's some WMI that will give you the list.

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

    Well I'm not sure about whether you can email the results of that from a batch file. If I may make an alternate suggestion that would solve your problem vbscript. I am far from great with vbscript but you can use it to query the services running on the local machine. The script below will email you the status of all of the services running on the machine the script gets run on. You'll obviously want to replace the smtp server and the email address. If you're part of a domain and you run this script as a privileged user (they have to be an administrator on the remote machine) you can query remote machines as well by replacing localhost with the fqdn.

    Dim objComputer, objMessage
    Dim strEmail
    
    ' If there is an error getting the status of a service it will attempt to move on to the next one
    On Error Resume Next
    
    ' Email Setup
    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = "Service Status Report"
    objMessage.From = "service_report@noreply.net"
    objMessage.To = "youraddress@example.net"
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    
    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.net"
    
    'Server port (typically 25)
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    
    Set objComputer = GetObject("WinNT://localhost")
    objComputer.Filter = Array("Service")
    
    For Each aService In objComputer
    strEmail = strEmail &chr(10) & aService.Name & "=" & aService.Status
    Next
    
    objMessage.TextBody = strEmail
    objMessage.Configuration.Fields.Update
    objMessage.Send
    

    Hope this helps you! Enjoy!

    Edit: Ahh one more thing a service status of 4 means the service is running, a service status of 1 means it's not. I'm not sure what 2 or 3 means but I'm willing to bet they are stopping/starting.

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