'Event ID Constants
Const EVENTID_SCRIPT_ERROR = 1000
Const SCRIPT_NAME = "AD_ADWS_Service.vbs"
Dim col, nCount, colServices, oService, state
Dim oAPI, oBag, oWMI
Sub Main()
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
Err.Clear
On Error Resume Next
' Connect to WMI
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
If Err <> 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " & SCRIPT_NAME & _
" failed to connect to WMI. The error was: " & GetErrorString(Err)
Else
' Attempt to get the service instance
Set colServices = oWMI.ExecQuery("Select Name,State from Win32_Service where name='adws'")
If Err <> 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " & SCRIPT_NAME & _
" failed query WMI. The error was: " & GetErrorString(Err)
Else
nCount = 0
For Each oService in colServices
nCount = nCount + 1
state = oService.State
Next
If Err <> 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " & SCRIPT_NAME & _
" failed retrieve the service state. The error was: " & GetErrorString(Err)
Else
' If the serice is not on the machine, return success
If nCount <> 0 Then
Call oBag.AddValue("Status", CStr(state))
Else
Call oBag.AddValue("Status", "Not Installed")
End If
Call oAPI.Return(oBag)
End If
End If
End If
End Sub
'******************************************************************************
Sub CreateEvent(lEventID, lEventType, strMessage)
'
' Purpose: Create a new event.
'
' Parameters: lEventID - The numerical ID of the event to create
' lEventType - The numerical ID of the event type
' strMessage - The message to include in the event
'
oAPI.LogScriptEvent "ADWS Service Status", lEventID, lEventType, strMessage
End Sub
'******************************************************************************
Function GetErrorString(oErr)
'
' Purpose: Attempts to find the description for an error if an error with
' no description is passed in.
'
' Parameters: oErr, the error object
'
' Return: String, the description for the error. (Includes the error code.)
'
Dim lErr, strErr
lErr = oErr
strErr = oErr.Description
On Error Resume Next
If 0 >= Len(strErr) Then
' If we don't have an error description, then check to see if the error
' is a 0x8007xxxx error. If it is, then look it up.
Const ErrorMask = &HFFFF0000
Const HiWord8007 = &H80070000
Const LoWordMask = 65535 ' This is equivalent to 0x0000FFFF
If (lErr And ErrorMask) = HiWord8007 Then
' Attempt to use 'net helpmsg' to get a description for the error.
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If Err = 0 Then
Dim oExec
Set oExec = oShell.Exec("net helpmsg " & (lErr And LoWordMask))
Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i < 5)
strErr = strMessage
End If
End If
End If
GetErrorString = vbCrLf & "The error returned was: '" & strErr & "' (0x" & Hex(lErr) & ")"
End Function