Windows command to get service status?

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

提交回复
热议问题