Function GetWMIProperty(oWmi, sPropName, nCIMType, ErrAction)
Dim sValue, oWmiProp
If Not IsValidObject(oWmi) Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "Accessing property on invalid WMI object.", Err
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
ScriptContext.Quit()
GetWMIProperty = ""
Exit Function
End If
On Error Resume Next
Set oWmiProp = oWmi.Properties_.Item(sPropName)
If Err.Number <> 0 Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" & sPropName & "'.", Err
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
ScriptContext.Quit()
End If
On Error Goto 0
If IsValidObject(oWmiProp) Then
sValue = oWmiProp.Value
If IsNull(sValue) Then
'
' If value is null, return blank to avoid any issues
'
GetWMIProperty = ""
Else
Select Case (oWmiProp.CIMType)
Case wbemCimtypeString, wbemCimtypeSint16, wbemCimtypeSint32, wbemCimtypeReal32, wbemCimtypeReal64, wbemCimtypeSint8, wbemCimtypeUint8, wbemCimtypeUint16, wbemCimtypeUint32, wbemCimtypeSint64, wbemCimtypeUint64:
GetWMIProperty = Trim(CStr(sValue))
Case wbemCimtypeBoolean:
If sValue = 1 Or UCase(sValue) = "TRUE" Then
GetWMIProperty = "True"
Else
GetWMIProperty = "False"
End If
Case wbemCimtypeDatetime:
Dim sTmpStrDate
'
' First attempt to convert the whole wmi date string
'
sTmpStrDate = Mid(sValue, 5, 2) & "/" & _
Mid(sValue, 7, 2) & "/" & _
Left(sValue, 4) & " " & _
Mid (sValue, 9, 2) & ":" & _
Mid(sValue, 11, 2) & ":" & _
Mid(sValue, 13, 2)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else
'
' Second, attempt just to convert the YYYYMMDD
'
sTmpStrDate = Mid(sValue, 5, 2) & "/" & _
Mid(sValue, 7, 2) & "/" & _
Left(sValue, 4)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else
'
' Nothing works - return passed in string
'
GetWMIProperty = sValue
End If
End If
Case Else:
GetWMIProperty = ""
End Select
End If
Else
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" & sPropName & "'.", Err
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
ScriptContext.Quit()
GetWMIProperty = ""
End If
If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
ScriptContext.Echo " + " & sPropName & " :: '" & GetWMIProperty & "'"
End Function
Class Printer
Private m_oPrinter
Public Sub SetPrinter(ByVal oWMIPrinter)
Set m_oPrinter = oWMIPrinter
End Sub
Public Property Get IsShared()
Const ATTR_SHARED = 8
IsShared = False
If IsNull(m_oPrinter.ServerName) Then
If (m_oPrinter.Attributes And ATTR_SHARED) = ATTR_SHARED Then
IsShared = True
End If
End If
End Property
Public Property Get Name()
Name = GetWMIProperty(m_oPrinter, "Name", wbemCimtypeUseDefault, ErrAction_None)
End Property
Public Property Get DetectedErrorState()
DetectedErrorState = GetWMIProperty(m_oPrinter, "DetectedErrorState", wbemCimtypeUseDefault, ErrAction_None)
End Property
Public Property Get Description()
Dim sDescription
sDescription = GetWMIProperty(m_oPrinter, "Description", wbemCimtypeUseDefault, ErrAction_Trace)
If sDescription = "" Then sDescription = GetWMIProperty(m_oPrinter, "Comment", wbemCimtypeUseDefault, ErrAction_Trace)
Description = sDescription
End Property
Public Property Get DriverName()
DriverName = GetWMIProperty(m_oPrinter, "DriverName", wbemCimtypeUseDefault, ErrAction_Trace)
End Property
Public Property Get Location()
Location = GetWMIProperty(m_oPrinter, "Location", wbemCimtypeUseDefault, ErrAction_Trace)
End Property
Public Property Get Network()
Network = GetWMIProperty(m_oPrinter, "Network", wbemCimtypeUseDefault, ErrAction_Trace)
End Property
Public Property Get PortName()
PortName = GetWMIProperty(m_oPrinter, "PortName", wbemCimtypeUseDefault, ErrAction_Trace)
End Property
Public Property Get ShareName()
ShareName = GetWMIProperty(m_oPrinter, "ShareName", wbemCimtypeUseDefault, ErrAction_Trace)
End Property
Public Property Get ShareServer()
If IsNull(m_oPrinter.ServerName) Then
ShareServer = "\\" & ScriptContext.TargetComputer
Else
ShareServer = GetWMIProperty(m_oPrinter, "ServerName", wbemCimtypeUseDefault, ErrAction_Trace)
End If
End Property
Public Property Get StatusCode()
StatusCode = GetWMIProperty(m_oPrinter, "Availability", wbemCimtypeUseDefault, ErrAction_Trace)
End Property
Public Function StatusCodeToString(ByVal nStatusCode)
If IsNull(nStatusCode) Or Len(nStatusCode) = 0 Then
StatusCodeToString = "Unknown"
Else
Select Case (nStatusCode)
Case 1: StatusCodeToString = "Other"
Case 2: StatusCodeToString = "Unknown"
Case 3: StatusCodeToString = "Idle"
Case 4: StatusCodeToString = "Printing"
Case 5: StatusCodeToString = "Warmup"
Case 6: StatusCodeToString = "Stopped printing"
Case 7: StatusCodeToString = "Offline"
End Select
End If
ScriptContext.Echo " + '" & CStr(nStatusCode) & "' converted to '" & StatusCodeToString & "'."
End Function
Public Function ErrorStateToString(ByVal nErrorState)
If IsNull(nErrorState) Then
ErrorStateToString = "Unknown"
Else
Select Case (nErrorState)
Case 1: ErrorStateToString = "Unknown"
Case 2: ErrorStateToString = "Other"
Case 3: ErrorStateToString = "No Error"
Case 4: ErrorStateToString = "Low Paper"
Case 5: ErrorStateToString = "No Paper"
Case 6: ErrorStateToString = "Low Toner"
Case 7: ErrorStateToString = "No Toner"
Case 8: ErrorStateToString = "Door Open"
Case 9: ErrorStateToString = "Jammed"
Case 10: ErrorStateToString = "Offline"
Case 11: ErrorStateToString = "Service Requested"
Case 12: ErrorStateToString = "Output Bin Full"
Case Else: ErrorStateToString = "No Error"
End Select
End If
ScriptContext.Echo " + '" & nErrorState & "' converted to '" & ErrorStateToString & "'."
End Function
End Class
Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
Function WMIGetInstances(ByVal sNamespace, ByVal sClassName)
'
' WMIGetInstances :: Returns a WMI Instance collection.
'
'
Set WMIGetInstances = Nothing
Const wbemFlagReturnWhenComplete = 0
Const wbemErrFailed = &H80041001
Dim oWMI
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptErrorNoAbort "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
Exit Function
End If
On Error Resume Next
Set WMIGetInstances = oWMI.InstancesOf(sClassName, wbemFlagReturnWhenComplete)
e.Save
On Error Goto 0
If (e.Number <> 0) And (e.Number <> wbemErrFailed) Then
ThrowScriptErrorNoAbort "The class name '" & sClassName & "' could not be found. Please check to see if this is a valid WMI class name.", e
Exit Function
End If
End Function
Function CaseInsensitiveStringEquals(ByVal sString1, ByVal sString2)
CaseInsensitiveStringEquals = (StrComp(sString1, sString2, vbTextCompare) = 0)
End Function
''******************************************************************************
' Name: CreateAlert
'
' Purpose: Creates, but does not submit, an alert
'
' Parameters:
' iSeverity - The severity of the alert.
' sName - The name of the alert.
' sDescription - The alert description.
' sRole - The server role
' sInstance - The server role instance for which to issue a state alert.
' sComponent - The component affected
' iProblemState - The problem state
'
Function CreateAlert(ByVal iSeverity, ByVal sName, ByVal sDescription, ByVal sRole, ByVal sInstance, ByVal sComponent, ByVal iProblemState)
Dim oAlert
Set oAlert = ScriptContext.CreateAlert()
oAlert.Name = sName
oAlert.Description = sDescription
oAlert.AlertSource = "Print Server - State Monitoring"
oAlert.AlertLevel = iSeverity
'If there is a role, instance and component specified then this is a state alert
If sRole <> "" And sInstance <> "" And sComponent <> "" Then
oAlert.ProblemState = iProblemState
oAlert.ServerRole = sRole
oAlert.ServerRoleInstance = sInstance
oAlert.Component = sComponent
oAlert.ComponentInstance = ""
End If
Set CreateAlert = oAlert
End Function
''******************************************************************************
' Name: CreateComponentInstanceAlert
'
' Purpose: Creates, but does not submit, an alert
'
' Parameters:
' iSeverity - The severity of the alert.
' sName - The name of the alert.
' sDescription - The alert description.
' sRole - The server role
' sInstance - The server role instance for which to issue a state alert.
' sComponent - The component affected
' sComponentInstance - The component instance affected
' iProblemState - The problem state
'
Function CreateComponentInstanceAlert(ByVal iSeverity, _
ByVal sName, _
ByVal sDescription, _
ByVal sRole, _
ByVal sInstance, _
ByVal sComponent, _
ByVal sComponentInstance, _
ByVal iProblemState)
Dim oAlert
Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'
'
Dim sErrDescription, sErrNumber
sErrDescription = oErr.Description
sErrNumber = oErr.Number
On Error Resume Next
Dim oScriptErrorEvent
Set oScriptErrorEvent = ScriptContext.CreateEvent()
With oScriptErrorEvent
.EventNumber = 40000
.EventType = EVENT_TYPE_ERROR
.Message = sMessage
.SetEventParameter """Microsoft Windows Print Server"""
.SetEventParameter sMessage
.SetEventParameter sErrDescription
.SetEventParameter sErrNumber
End With
ScriptContext.Submit oScriptErrorEvent
ScriptContext.Echo "ThrowScriptError('" & sMessage & "')"
End Function
Function IsValidObject(ByVal oObject)
IsValidObject = False
If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function
Dim aInfo
For Each aInfo In aAlertData
If CaseInsensitiveStringEquals(sAlertLevelName, aInfo(ALERT_LEVEL_NAME_INDEX)) Then
lAlertLevel = aInfo(ALERT_LEVEL_VALUE_INDEX)
lProblemState = aInfo(PROBLEM_STATE_INDEX)
GetAlertInfo = True
Exit Function
End If
Next
End Function
If lStatusCode = 0 Then
GetAlertLevelName = True
sAlertLevelName = ALERT_NONE_NAME
ElseIf (lStatusCode > 0) And (lStatusCode <= UBound(STATUS_CODE_TO_PARAMETER_MAP)) Then
GetAlertLevelName = True
sAlertLevelName = ScriptContext.Parameters.Get(STATUS_CODE_TO_PARAMETER_MAP(lStatusCode))
End If
End Function
Sub Main()
ScriptContext.Echo "Print Server State Monitoring (" & CStr(Time) & ")"
ScriptContext.Echo "Obtaining State for Printers"
DoPrinterMonitoring()
ScriptContext.Echo "Complete :: Print Server State Monitoring (" & CStr(Time) & ")"
End Sub
Sub DoPrinterMonitoring()
Dim oPrinters, oWMIPrinter
Dim oPrinter
Set oPrinter = New Printer
Set oPrinters = WMIGetInstances("winmgmts:\\" & ScriptContext.TargetComputer & "\root\cimv2", "Win32_Printer")
If Not IsValidObject(oPrinters) Then Exit Sub
For Each oWMIPrinter In oPrinters
oPrinter.SetPrinter oWMIPrinter
If oPrinter.IsShared Then
Dim sPrinterName, nErrorState, sErrorStr
sPrinterName = oPrinter.Name
ScriptContext.Echo " + Shared: '" & sPrinterName & "'"
nErrorState = oPrinter.DetectedErrorState
If nErrorState = 0 Then
For nErrorState = 1 To UBound(STATUS_CODE_TO_PARAMETER_MAP)
sErrorStr = oPrinter.ErrorStateToString(nErrorState)
SubmitPrinterAlert ALERT_SUCCESS, sPrinterName, sErrorStr, PROBLEMSTATE_GREEN
Next
Else
sErrorStr = oPrinter.ErrorStateToString(nErrorState)
Dim lAlertLevel
Dim lProblemState
Dim sAlertLevelName
If GetAlertLevelName(nErrorState, sAlertLevelName) Then
If GetAlertInfo(sAlertLevelName, lAlertLevel, lProblemState) Then
SubmitPrinterAlert lAlertLevel, sPrinterName, sErrorStr, lProblemState
Else
ThrowScriptErrorNoAbort """" & sAlertLevelName & """ is not a valid alert level name", Err
End If
Else
ThrowScriptErrorNoAbort "There is no alert level associated with printer status code " & nErrorState, Err
End If
ScriptContext.Echo " > Detected Error State : '" & sErrorStr & "'"
End If
Else
ScriptContext.Echo " + Not Shared: '" & oPrinter.Name & "'"
End If
Next
End Sub