Microsoft Windows Server 2003 Print Server Role Discovery Module

Microsoft.Windows.Server.PrintServer.2003.Discovery.ModuleType (DataSourceModuleType)

This module provides the timed script that conducts the Print Server Role discovery on Microsoft Windows Server 2003 systems.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterDiscoveryDataSource Default

Overrideable Parameters:

IDParameterTypeSelector
IntervalSecondsint$Config/IntervalSeconds$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<DataSourceModuleType ID="Microsoft.Windows.Server.PrintServer.2003.Discovery.ModuleType" Accessibility="Internal">
<Configuration>
<xsd:element name="ComputerFQDN" type="xsd:string"/>
<xsd:element name="ComputerNetBIOS" type="xsd:string"/>
<xsd:element name="IntervalSeconds" type="xsd:integer"/>
<xsd:element name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="SystemLibrary!System.CommandExecuterDiscoveryDataSource">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<ApplicationName>%windir%\System32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>/nologo $file/PrintServer2003Discovery.vbs$ $MPElement$ $Target/Id$ $Config/ComputerFQDN$ $Config/ComputerNetBIOS$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>PrintServer2003Discovery.vbs</Name>
<Contents><Script>
'-------------------------------------------------------------------
' &lt;company&gt;Microsoft Corporation&lt;/company&gt;
' &lt;copyright&gt;Copyright (c) Microsoft Corporation. All rights reserved.&lt;/copyright&gt;
'-------------------------------------------------------------------

Option Explicit

Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4
Const EVENT_TYPE_AUDITSUCCESS = 8
Const EVENT_TYPE_AUDITFAILURE = 16

Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'
'

WScript.StdErr.WriteLine "Error " &amp; oErr.Number &amp; ": " &amp; oErr.Description
WScript.StdErr.WriteLine " " &amp; 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 = &amp;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 &lt;&gt; 0 Then
ThrowScriptErrorNoAbort "Unable to open WMI Namespace '" &amp; sNamespace &amp; "'. 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 &lt;&gt; 0) And (e.Number &lt;&gt; wbemErrFailed) Then
ThrowScriptErrorNoAbort "The class name '" &amp; sClassName &amp; "' could not be found. Please check to see if this is a valid WMI class name.", e
Exit Function
End If
End Function



Const wbemCimtypeUseDefault = 0 'Use Default Type CIM type - Custom
Const wbemCimtypeSint16 = 2 'Signed 16-bit integer
Const wbemCimtypeSint32 = 3 'Signed 32-bit integer
Const wbemCimtypeReal32 = 4 '32-bit real number
Const wbemCimtypeReal64 = 5 '64-bit real number
Const wbemCimtypeString = 8 'String
Const wbemCimtypeBoolean = 11 'Boolean value
Const wbemCimtypeObject = 13 'CIM object
Const wbemCimtypeSint8 = 16 'Signed 8-bit integer
Const wbemCimtypeUint8 = 17 'Unsigned 8-bit integer
Const wbemCimtypeUint16 = 18 'Unsigned 16-bit integer
Const wbemCimtypeUint32 = 19 'Unsigned 32-bit integer
Const wbemCimtypeSint64 = 20 'Signed 64-bit integer
Const wbemCimtypeUint64 = 21 'Unsigned 64-bit integer
Const wbemCimtypeDatetime = 101 'Date/time value
Const wbemCimtypeReference = 102 'Reference to a CIM object
Const wbemCimtypeChar16 = 103 '16-bit character

Const ErrAction_None = 0
Const ErrAction_Trace = 1
Const ErrAction_ThrowError = 16
Const ErrAction_Abort = 32
Const ErrAction_ThrowErrorAndAbort = 48

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 &lt;&gt; 0 Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" &amp; sPropName &amp; "'.", 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) &amp; "/" &amp; _
Mid(sValue, 7, 2) &amp; "/" &amp; _
Left(sValue, 4) &amp; " " &amp; _
Mid (sValue, 9, 2) &amp; ":" &amp; _
Mid(sValue, 11, 2) &amp; ":" &amp; _
Mid(sValue, 13, 2)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else

'
' Second, attempt just to convert the YYYYMMDD
'
sTmpStrDate = Mid(sValue, 5, 2) &amp; "/" &amp; _
Mid(sValue, 7, 2) &amp; "/" &amp; _
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: '" &amp; sPropName &amp; "'.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
WScript.Quit()

GetWMIProperty = ""

End If


If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
WScript.Echo " + " &amp; sPropName &amp; " :: '" &amp; GetWMIProperty &amp; "'"

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 = "\\" &amp; 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 " + '" &amp; CStr(nStatusCode) &amp; "' converted to '" &amp; StatusCodeToString &amp; "'."
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 " + '" &amp; nErrorState &amp; "' converted to '" &amp; ErrorStateToString &amp; "'."
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:\\" &amp; TargetComputerFQDN &amp; "\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 &lt; 3 Then
ThrowScriptError "Invalid number of args (" &amp; Wscript.Arguments.Count &amp; ")", Err
End If

Dim SourceId
Dim ManagedEntityId
Dim TargetComputerFQDN
Dim TargetComputerNetBIOS

SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
TargetComputerFQDN = oArgs(2)
TargetComputerNetBIOS = oArgs(3)

' 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

</Script></Contents>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>SystemLibrary!System.Discovery.Data</OutputType>
</DataSourceModuleType>