VBscript #01

I would love to say I wrote this, but this isn’t the case (let’s say I got a massive dolop of help from ed wilson + the Scripting Guys website… Hey, Scripting guys!

I desperately wanted to write something along this line as I was sick of particular services stopping; this script sites there… for ever if needs be… and monitors any service that’s running. If it stops, it emails the address of your choice + restarts the service. And then it carries on sitting there until another service stops.

‘Next 1 line enables the script to continue if it encounters errors
On Error Resume Next

‘Next 1 line indicates that the script should use the local computer (you could put a remote PC or server in here)
strComputer = "."

‘Next 1 line allows script to use networking
Set objNetwork = CreateObject("Wscript.Network")

‘Next 4 lines do something I don’t understand 🙂
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim objEnv
Set objEnv = WshShell.Environment("Process")
Set objEmail = CreateObject("CDO.Message")

‘Next 1 line connects to the WMI service
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colServices = objWMIService. _
    ExecNotificationQuery("Select * from __InstanceModificationEvent " _
        & "within 30 where TargetInstance isa ‘Win32_Service’")

i = 0
Do While i = 0
        i = 0
        Set objService = colServices.NextEvent
        If objService.TargetInstance.State = "Stopped" Then
            objEmail.From = "you_can_make_up_this_email_address@yourcompany.com"
            objEmail.To = "this_has_to_be_a_real_address@yourcompany.com"
            objEmail.Subject = "Service warning on " & objEnv("COMPUTERNAME") & "."
            objEmail.Textbody = "The following service- " & objService.TargetInstance.Caption & "- on " & objEnv("COMPUTERNAME") & " stopped unexpectedly but was restarted. Please investigate immediately. Generated on " & FormatDateTime(Now(), vbGeneralDate) & "."

            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                    "your_email_server.here.com"
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update

            objEmail.Send
                GetObject("winmgmts:win32_Service.name=’" & objService.TargetInstance.Name & "’").StartService
        End If
Loop

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s