Print_Server___Service_Discovery (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
Comment{A6AECF96-03B0-4A97-932B-88EDEA177944}

Member Modules:

ID Module Type TypeId RunAs 
RunScriptAction WriteAction System.Mom.BackwardCompatibility.ScriptResponse Default

Source Code:

<WriteActionModuleType ID="Print_Server___Service_Discovery" Comment="{A6AECF96-03B0-4A97-932B-88EDEA177944}" Accessibility="Internal" Batching="false">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>MomBackwardCompatibility!System.Mom.BackwardCompatibility.AlertGenerationSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element name="AlertGeneration" type="AlertGenerationType"/>
<xsd:element name="InvokerType" type="xsd:integer"/>
</Configuration>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="RunScriptAction" TypeID="MomBackwardCompatibility!System.Mom.BackwardCompatibility.ScriptResponse">
<AlertGeneration>$Config/AlertGeneration$</AlertGeneration>
<InvokerType>$Config/InvokerType$</InvokerType>
<Body><Script>
'-------------------------------------------------------------------
' &lt;company&gt;Microsoft Corporation&lt;/company&gt;
' &lt;copyright&gt;Copyright (c) Microsoft Corporation. All rights reserved.&lt;/copyright&gt;
' &lt;name&gt;
' Print Server - Service Discovery
' &lt;/name&gt;
' &lt;summary&gt;
' Discovers the printers and populates attributes for a print server
' &lt;/summary&gt;
'-------------------------------------------------------------------

Option Explicit

Const COMPUTER_CLASS_ID = "Computer"

Const COMPUTER_COMPUTER_NAME_ATTRIBUTE_ID = "ComputerName"
Const COMPUTER_TIME_ZONE_BIAS_ATTRIBUTE_ID = "Time Zone Bias"
Const COMPUTER_OPERATING_SYSTEM_VERSION_ATTRIBUTE_ID = "Operating System Version"
Const COMPUTER_IP_ADDRESS_ATTRIBUTE_ID = "IPAddress"
Const COMPUTER_FQDN_ATTRIBUTE_ID = "FQDN"
Const COMPUTER_VIRTUAL_SERVER_TYPE_ATTRIBUTE_ID = "Virtual Server Type"

Const PRINT_SERVER_CLASS_ID = "Print Server"

Const PRINT_SERVER_SERVER_NAME_ATTRIBUTE_ID = "Server Name"

Const PRINT_SERVER_PRINTERS_COMPONENT_ID = "Printers"
Const PRINT_SERVER_SERVICES_COMPONENT_ID = "Service"


Const PRINTER_CLASS_ID = "Printer"

Const PRINTER_NAME_ATTRIBUTE_ID = "Name"
Const PRINTER_DESCRIPTION_ATTRIBUTE_ID = "Description"
Const PRINTER_DETECTED_ERROR_STATE_ATTRIBUTE_ID = "Detected Error State"
Const PRINTER_DRIVER_NAME_ATTRIBUTE_ID = "Driver Name"
Const PRINTER_LOCATION_ATTRIBUTE_ID = "Location"
Const PRINTER_NETWORK_ATTRIBUTE_ID = "Network"
Const PRINTER_PORT_NAME_ATTRIBUTE_ID = "Port Name"
Const PRINTER_PRINTER_STATUS_ATTRIBUTE_ID = "Printer Status"
Const PRINTER_SHARE_NAME_ATTRIBUTE_ID = "Share Name"
Const PRINTER_SHARE_SERVER_ATTRIBUTE_ID = "Share Server"




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 _
ScriptContext.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 _
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) &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 _
ScriptContext.Quit()

GetWMIProperty = ""

End If


If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
ScriptContext.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; 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 " + '" &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
ScriptContext.Echo " + '" &amp; nErrorState &amp; "' converted to '" &amp; ErrorStateToString &amp; "'."
End Function
End Class


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
'
'

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('" &amp; sMessage &amp; "')"
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

Function IsValidObject(ByVal oObject)
IsValidObject = False

If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function

Sub Main()
ScriptContext.Echo "Print Server Service Discovery (" &amp; CStr(Time) &amp; ")"

Dim oPrintDiscData
Set oPrintDiscData = ScriptContext.CreateDiscoveryData()
oPrintDiscData.ScopeID = "{914CDD7F-9862-4608-BD12-18F43AB5BD90}"

DoPrinterDiscovery oPrintDiscData

ScriptContext.Submit oPrintDiscData

ScriptContext.Echo "Submitted Discovery Data (" &amp; CStr(Time) &amp; ")"

End Sub


Sub DoPrinterDiscovery(oDiscData)
Dim oPrinterCollection
Set oPrinterCollection = oDiscData.CreateCollection()
With oPrinterCollection
.ClassID = PRINTER_CLASS_ID

.AddScopeFilter COMPUTER_COMPUTER_NAME_ATTRIBUTE_ID, ScriptContext.TargetComputerIdentity
.AddScopeFilter PRINT_SERVER_SERVER_NAME_ATTRIBUTE_ID, ScriptContext.TargetNetbiosComputer
.AddScopeProperty PRINTER_DESCRIPTION_ATTRIBUTE_ID
.AddScopeProperty PRINTER_DETECTED_ERROR_STATE_ATTRIBUTE_ID
.AddScopeProperty PRINTER_DRIVER_NAME_ATTRIBUTE_ID
.AddScopeProperty PRINTER_LOCATION_ATTRIBUTE_ID
.AddScopeProperty PRINTER_NETWORK_ATTRIBUTE_ID
.AddScopeProperty PRINTER_PORT_NAME_ATTRIBUTE_ID
.AddScopeProperty PRINTER_SHARE_NAME_ATTRIBUTE_ID
.AddScopeProperty PRINTER_SHARE_SERVER_ATTRIBUTE_ID
.AddScopeProperty PRINTER_PRINTER_STATUS_ATTRIBUTE_ID
End With

Dim oPrinters, oWMIPrinter
Dim oPrinter
Set oPrinter = new Printer
Set oPrinters = WMIGetInstances("winmgmts:\\" &amp; ScriptContext.TargetComputer &amp; "\root\cimv2", "Win32_Printer")
If IsValidObject(oPrinters) Then
For Each oWMIPrinter In oPrinters
oPrinter.SetPrinter oWMIPrinter
If oPrinter.IsShared Then
Dim oPrinterInstance
Set oPrinterInstance = oPrinterCollection.CreateInstance()

With oPrinterInstance
.AddKeyProperty PRINTER_NAME_ATTRIBUTE_ID, oPrinter.Name

.AddProperty PRINTER_DESCRIPTION_ATTRIBUTE_ID, oPrinter.Description
.AddProperty PRINTER_DETECTED_ERROR_STATE_ATTRIBUTE_ID, oPrinter.ErrorStateToString(oPrinter.DetectedErrorState)
.AddProperty PRINTER_DRIVER_NAME_ATTRIBUTE_ID, oPrinter.DriverName
.AddProperty PRINTER_LOCATION_ATTRIBUTE_ID, oPrinter.Location
.AddProperty PRINTER_NETWORK_ATTRIBUTE_ID, oPrinter.Network
.AddProperty PRINTER_PORT_NAME_ATTRIBUTE_ID, oPrinter.PortName
.AddProperty PRINTER_SHARE_NAME_ATTRIBUTE_ID, oPrinter.ShareName
.AddProperty PRINTER_SHARE_SERVER_ATTRIBUTE_ID, oPrinter.ShareServer
.AddProperty PRINTER_PRINTER_STATUS_ATTRIBUTE_ID, oPrinter.StatusCodeToString(oPrinter.StatusCode)
End With

oPrinterCollection.AddInstance oPrinterInstance
Else
ScriptContext.Echo " + Not Shared: '" &amp; oPrinter.Name &amp; "'"
End If

ScriptContext.Echo ""
Next
End If

oDiscData.AddCollection oPrinterCollection
End Sub</Script></Body>
<Language>VBScript</Language>
<Name>Print Server - Service Discovery</Name>
<Parameters/>
<ManagementPackId>[Microsoft.Windows.Server.PrintServer,,1.0.0.1]</ManagementPackId>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RunScriptAction"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>SystemLibrary!System.BaseData</InputType>
</WriteActionModuleType>