Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'
'
WScript.StdErr.WriteLine "Error " & oErr.Number & ": " & oErr.Description
WScript.StdErr.WriteLine " " & sMessage
End Function
Function ThrowScriptError(ByVal sMessage, ByVal oErr)
ThrowScriptErrorNoAbort sMessage, oErr
WScript.Quit -1
End Function
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 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 _
WScript.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 _
WScript.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 _
WScript.Quit()
GetWMIProperty = ""
End If
If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
WScript.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 = "\\" & TargetComputerFQDN
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
WScript.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
WScript.Echo " + '" & nErrorState & "' converted to '" & ErrorStateToString & "'."
End Function
End Class
Function IsValidObject(ByVal oObject)
IsValidObject = False
If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function
Function ComputerHasSharedPrinters()
ComputerHasSharedPrinters = False
Dim oPrinters, oWMIPrinter
Dim oPrinter
Set oPrinter = new Printer
Set oPrinters = WMIGetInstances("winmgmts:\\" & TargetComputerFQDN & "\root\cimv2", "Win32_Printer")
If Not IsValidObject(oPrinters) Then Exit Function
For Each oWMIPrinter In oPrinters
oPrinter.SetPrinter oWMIPrinter
If oPrinter.IsShared Then
ComputerHasSharedPrinters = True
Exit Function
End If
Next
End Function
'--------------------
' Main
'--------------------
Dim oArgs
Set oArgs = WScript.Arguments
if oArgs.Count < 3 Then
ThrowScriptError "Invalid number of args (" & Wscript.Arguments.Count & ")", Err
End If
Dim SourceId
Dim ManagedEntityId
Dim TargetComputerFQDN
Dim TargetComputerNetBIOS
' Create discovery
Dim oMOMScriptAPI
Set oMOMScriptAPI = CreateObject("MOM.ScriptAPI")
Dim oDiscoveryData
Set oDiscoveryData = oMOMScriptAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
If ComputerHasSharedPrinters() Then
Dim oInst
set oInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.Server.PrintServer.Microsoft_Windows_2003_Print_Servers_Installation']$")
With oInst
.AddProperty "$MPElement[Name='WindowsLibrary!Microsoft.Windows.Computer']/PrincipalName$", TargetComputerFQDN
.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", "Windows Server 2003 Print Server (" + TargetComputerNetBIOS + ")"
End With
call oDiscoveryData.AddInstance(oInst)
End If
' Return the snapshot
oMOMScriptAPI.Return oDiscoveryData