VBScript #02

This sounds familiar… 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!

This script is slightly different; it checks for the existence of particular services (defined in an array- aryServices(4) below) and- dependingon whether it finds the service exists (i.e. is installed, not simply stopped or started) either emails to say that the service exists or logs an event in the application log if it is installed. The really tricky part is finding out how WMI references a particular service; most services have at least 2 different "names" depending on how they’re referenced in WMI. this needs additional clarification, but to explain this script it checks for the existence of 5 key BackupExec services.

‘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’")

Dim aryServices(4)
aryServices(0) = "BackupExecAgentAccelerator"
aryServices(1) = "BackupExecAgentBrowser"
aryServices(2) = "BackupExecDeviceMediaService"
aryServices(3) = "BackupExecJobEngine"
aryServices(4) = "BackupExecRPCService"

For Each Service In aryServices
    Set chkService = _
        objWMIService.ExecQuery("Select * From Win32_Service Where Name = ‘" & Service & "’")
        If chkService.Count = 0 Then
            WScript.Echo "The following service – " & Service & "- is not installed on " & objEnv("COMPUTERNAME") & "."
            objEmail.From = "you_can_make_up_this_email_address@yourcompany.com"
            objEmail.To = "this_has_to_be_a_real_address@yourcompany.com"
            objEmail.Subject = "Critical service on " & objEnv("COMPUTERNAME") & "."
            objEmail.Textbody = "Server " & objEnv("COMPUTERNAME") & " does not have a critical backup service installed. Please correct 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
        Else
            WshShell.LogEvent 4, "The following service- " & Service & "- is already installed."
            GetObject("winmgmts:win32_Service.name=’" & Service & "’").StartService
        End If
Next

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