As it has been in the past let me start by saying that I did not write this script myself, at least not entirely. Why reinvent the wheel right? Instead I’ve taken multiple bits of code from various places, combined them and modified them to create the function I was after. This process can still take some work, and so I’m sharing my result with you here.
*The bulk of this script was borrowed from The Monitoring Guy (http://themonitoringguy.com/scripts-code/monitoring-windows-services-vbscript/)
Now to the point, what does it do exactly? It uses WMI to check the status of the service you’re concerned about and then performs an action based on if it’s running or not. In this case if the service is running it echo’s a small output to the cmd shell that the service is running and closes. If, however, the service is not running, the script will attempt to start the service. After ten seconds it will then send an email. The message alerts when (Date & Time) it ran and therefore when it found the service not running, the server it checked, and of course, if it was able to start the service back or not.
As always, the nice thing about scripts is that you can customize it to do anything you like.
I set this up to run as a scheduled task and I chose to call it from a .bat file using the below. The script name is followed by the name of the server and the name of the service to check.
CSCRIPT Monitor_Service.vbs server2 "My Service Name"
The vbs script is as shown below. Save to a .vbs file and run using the command above.
*You will need to change the, to: and from: email address and you will need to change the smarthost to the name or ip of your email server. Each of these fields, show up twice in the script below.
'Declare Variables
Dim objWMIService, objProcess, colProcess, Status,
strComputer, strService
'Assign Arguments
strComputer = WScript.Arguments(0)
strService = WScript.Arguments(1)
Status= false
'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
Wscript.echo
"No Arguments Entered - Exiting Script"
WScript.Quit
End If
'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"&
"{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("SELECT
DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" &
strService & "'")
'Check For Running Service
For Each objProcess in colProcess
If
InStr(objProcess.DisplayName,strService) > 0 And objProcess.State =
"Running" Then
Status
= true
End If
Next
If Status = true Then
Wscript.echo
"*****************************************************************"
Wscript.echo
" "
Wscript.echo
" Service: " &
strService & ", on " & strComputer & " - Running
"
Wscript.echo " "
Wscript.echo
"*****************************************************************"
Else
Set
objWMIService = GetObject("winmgmts:" _
&
"{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set
colRunningServices = objWMIService.ExecQuery _
("select
State from Win32_Service where Name = '" & strService &
"'")
For
Each objService in colRunningServices
If
objService.State <> "Running" Then
errReturn
= objService.StartService()
End If
Next
'Wait X Miliseconds
WScript.sleep 10000
'Recheck if service is running
Set objWMIService = GetObject("winmgmts:"&
"{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("SELECT
DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" &
strService & "'")
For Each objProcess in colProcess
If
InStr(objProcess.DisplayName,strService) > 0 And objProcess.State =
"Running" Then
Status
= true
End If
Next
If
Status = true ThenSet objEmail = CreateObject("CDO.Message")
objEmail.From = "Alert@company.org"
objEmail.To
= "engineers@company.org"
objEmail.Subject
= "Service Notice - " & strService & " - " &
strComputerobjEmail.HTMLBody = "The " & strService & " Service was detected not running on: " & Date & " at " & Time & "<BR><BR><B>On Host:</B> " & strComputer & "<BR><BR><B>Attmept to Start:</B> " & strService & " - Started Successfully"
objEmail.Configuration.Fields.Item
_
("http://schemas.microsoft.com/cdo/configuration/sendusing")
= 2
objEmail.Configuration.Fields.Item
_
("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= _
"smarthost"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
Else
'Email Alert if Service Failed to Start
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Alert@company.org"
objEmail.To = "engineers@company.org"
objEmail.Subject = "Service down - " & strService & " - " & strComputer
objEmail.HTMLBody = "The " & strService & " Service was detected not running on: " & Date & " at " & Time & "<BR><BR><B>On Host:</B> " & strComputer & "<BR><BR><B>Attmept to Start:</B> " & strService & " - Failed to Start"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"smarthost"
objEmail.Configuration.Fields.Item
_
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25objEmail.Configuration.Fields.Update
objEmail.Send
End If
End If
No comments:
Post a Comment