Gegevensbron voor roldetectie van Afdrukserver 2016 en 1709+

Microsoft.Windows.PrintServerRole.Discovery.DS (DataSourceModuleType)

Detectie van rol van Afdrukserver 2016 en 1709+

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterDiscoveryDataSource Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Frequencystring$Config/Frequency$Frequentie in secondenDetectie van frequentie in seconden
TimeoutSecondsint$Config/TimeoutSeconds$Time-out in seconden

Source Code:

<DataSourceModuleType ID="Microsoft.Windows.PrintServerRole.Discovery.DS" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="Frequency" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="SourceId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ManagedEntityId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TargetComputer" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TargetComputerId" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Frequency" Selector="$Config/Frequency$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterDiscoveryDataSource">
<IntervalSeconds>$Config/Frequency$</IntervalSeconds>
<ApplicationName>%SystemRoot%\system32\cmd.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>/c $file/Main.cmd$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>Main.cmd</Name>
<Contents><Script>

@echo off
set psscript=PrintServerRole.Discovery.ps1
set vbscript=PrintServerRole.Discovery.vbs
set cscript=%windir%\system32\cscript.exe
set ps=%windir%\system32\WindowsPowerShell\v1.0\powershell.exe
set params=

if Exist "%cscript%" goto vb
if Exist "%ps%" goto pshell
goto end
:vb
if not exist "%~dp0%vbscript%" goto end

if exist "%~dp0vbparams.txt" (
set /p params=&lt;"%~dp0vbparams.txt"
)

"%cscript%" /nologo "%~dp0%vbscript%" %params%
goto end
:pshell
if not exist "%~dp0%psscript%" goto end

if exist "%~dp0psparams.txt" (
set /p params=&lt;"%~dp0psparams.txt"
)
"%ps%" -nologo -EP bypass -command "&amp; '%~dp0%psscript%'" %params%
goto end

:end



</Script></Contents>
<Unicode>false</Unicode>
</File>
<File>
<Name>PrintServerRole.Discovery.ps1</Name>
<Contents><Script>param($SourceId, $ManagedEntityId, $TargetComputer, $TargetComputerId)

$ScriptName = "PrintServerRole.Discovery.ps1"
$EventId = 3001
$EventError = 1
$EventWarning = 2
$EventInformation = 3


Function Main()
{

$result = Init-ScomHelper

if ($false -eq $result)
{
return
}

$oDiscoveryData = Get-DiscoveryData

if ($null -eq $oDiscoveryData)
{
return
}

$ErrorActionPreference = "SilentlyContinue"

$SerializeDiscoveryData = [Scom.Helper.ConvertData]::GetDataItemFromOutput($oDiscoveryData)
Write-Host "$SerializeDiscoveryData"
}

Function DoDiscovery
{
param ([string]$sTargetComputer, [string]$sTargetComputerID, $oDisc)

$ErrorActionPreference = "SilentlyContinue"
$result = $false

$LogName = "Microsoft-Windows-PrintBRM/Admin"
$ClassId = "$MPElement[Name='Microsoft.Windows.Server.10.0.PrintServerRole']$"
$InstallationType = GetRegistryKeyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\" "InstallationType"

if ($InstallationType -eq "Server Core")
{
$LogName = "Application"
}
elseif($InstallationType -eq "Nano Server")
{
$result = $true
return $result
}

$Error.Clear()

$oInstance = $oDisc.CreateClassInstance($ClassId)
if (0 -ne $Error.Count)
{
return $result
}

$PrintRole = Get-PrinterRole
If (0 -ne $error.Count)
{
return $result
}

if ($null -eq $PrintRole)
{
$result = $true
return $result
}

$Error.Clear()

$DisplayName = "Windows Server 2016 Print Server ($sTargetComputer)"
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $sTargetComputerID)
$oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $DisplayName)
$oInstanceComputer.AddProperty("$MPElement[Name='Microsoft.Windows.Server.10.0.PrintServerRole']/EventLog$", $LogName)

if (0 -ne $Error.Count)
{
return $result
}


$oDisc.AddInstance($oInstance)

$result = 0 -eq $Error.Count

return $result

}


Function Get-DiscoveryData
{
$ErrorActionPreference = "SilentlyContinue"
$Error.Clear()

$momAPI = New-Object -ComObject MOM.ScriptAPI

If ($null -eq $momAPI -or 0 -ne $error.Count)
{
return $null
}


$oDiscoveryData = $momAPI.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
if (0 -ne $Error.Count)
{
$message = $Error[0].Exception.Message
$message = "Cannot create discovery data object. Details: " + $message
$momAPI.LogScriptEvent($ScriptName, $EventId, $EventError, $message)

return $null
}

If ([string]::IsNullOrEmpty($SourceId) -or [string]::IsNullOrEmpty($ManagedEntityId) -or [string]::IsNullOrEmpty($TargetComputerId))
{
$message = "Invalid number of arguments or empty arguments."
$momAPI.LogScriptEvent($ScriptName, $EventId, $EventInformation, $message)

$oDiscoveryData.IsSnapshot = $false
return $oDiscoveryData
}

$result = DoDiscovery $TargetComputer $TargetComputerID $oDiscoveryData
if ($false -eq $result)
{
$Error.Clear()
$oDiscoveryData = $momAPI.CreateDiscoveryData(0, $SourceID, $ManagedEntityId)
$oDiscoveryData.IsSnapshot = $false
}

return $oDiscoveryData

}

Function Load-Module ([string] $ModuleName)
{
if ([string]::IsNullOrEmpty($ModuleName) )
{
return $false
}

$ErrorActionPreference="SilentlyContinue"
$error.Clear()

$retval = $false
$cim = Get-Module -Name $ModuleName

########Check for powershell 1.0
if ($error.Count -ne 0)
{
$type = $error[0].Exception.GetType()
if ([System.Management.Automation.CommandNotFoundException] -eq $type)
{
$error.Clear()
return $retval
}

$error.Clear()
}

if ($null -eq $cim)
{
Import-Module $ModuleName
if ($error.Count -eq 0)
{
$retval = $true
}

$error.Clear()
}
else
{
$retval = $true
}

return $retval
}

Function Get-PrinterRole()
{

$WMI_ERROR_INVALIDCLASS = -2147217392
$WMI_ERROR_INVALIDNAMESPACE = -2147217394
$CIM_ERROR_INVALIDCLASS = 2147749902
$CIM_ERROR_INVALIDNAMESPACE = 2147749904

$ErrorActionPreference="SilentlyContinue"
$error.Clear()

$result = Load-Module -ModuleName "CimCmdLets"
$error.Clear()


If ($false -eq $result)
{
$cmd =Get-Command -Name "Get-WmiObject"

if ($null -eq $cmd -or $error.Count -ne 0)
{
return $null
}

$result = Get-WmiObject -Class "Win32_ServerFeature" -Filter "ID = 7"
if (0 -ne $error.Count)
{
$ErrorCode = $error[0].Exception.ErrorCode.value__
if ($WMI_ERROR_INVALIDCLASS -eq $ErrorCode -or $WMI_ERROR_INVALIDNAMESPACE -eq $ErrorCode)
{
$ErrorActionPreference.Clear()
$result = $null
}
}
}
else
{
$result = Get-CimInstance -ClassName "Win32_ServerFeature" -Filter "ID = 7"
if (0 -ne $error.Count)
{
$ErrorCode = $error[0].Exception.ErrorData.Error_code
if ($CIM_ERROR_INVALIDCLASS -eq $ErrorCode -or $CIM_ERROR_INVALIDNAMESPACE -eq $ErrorCode)
{
$ErrorActionPreference.Clear()
$result = $null
}
}
}
}

Function GetRegistryKeyValue
{
param ([string]$keyPath,
[string]$key)

$ErrorActionPreference="SilentlyContinue"
$error.Clear()

$strKeyValue = Get-ItemProperty -Path $keyPath -Name $key
if ($error.Count -gt 0)
{
$strKeyValue = $null
}

return $strKeyValue.$key
}

Function Init-ScomHelper
{
$DiscHelper = @'
namespace SCOM.Helper
{
using System;
using System.Runtime.InteropServices;
public class ConvertData
{

public static string GetDataItemFromOutput(
Object oData)
{
NativeMethods.ISerialize discoverySerializer = null;
discoverySerializer = oData as NativeMethods.ISerialize;
string xmlString = null;
if (null != discoverySerializer)
{
int hr = discoverySerializer.SaveToString(out xmlString);
Marshal.ThrowExceptionForHR(hr);
}

return xmlString;
}

public static class NativeMethods
{
[Guid("A4E79E8A-9494-47A4-A280-8C7D35C88A2F"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISerialize
{
int SaveToString([MarshalAs(UnmanagedType.BStr)] out string output);
int LoadFromString([MarshalAs(UnmanagedType.BStr)] string input);
}
}
}
}

'@

$ErrorActionPreference = "SilentlyContinue"
$Error.Clear()
Add-Type $DiscHelper

$result = 0 -eq $Error.Count

return $result
}


Main

</Script></Contents>
<Unicode>true</Unicode>
</File>
<File>
<Name>PrintServerRole.Discovery.vbs</Name>
<Contents><Script>
'Copyright (c) Microsoft Corporation. All rights reserved.

' Parameters that should be passed to this script
' 0 MPElement ID
' 1 Target Id for ME this rule is running against
' 2 Computer (FQDN) that the OS will be hosted on
' 3 Computer ID (Key) that the OS will be hosted on

Const EventId = 3001
Const EventError = 1
Const EventWarning = 2
Const EventInformation = 3
Const ScriptName = "PrintServerRole.Discovery.vbs"
Const CoreLogName = "Application"
Const FullLogName = "Microsoft-Windows-PrintBRM/Admin"

Const WMI_ERROR_INVALIDCLASS = -2147217392
Const WMI_ERROR_INVALIDNAMESPACE = -2147217394

Const PrinterNameSpace = "winmgmts:\\.\root\cimv2"
Const PrinterRoleQuery = "SELECT Name FROM Win32_ServerFeature WHERE ID = 7"

Call Main()

Sub Main()
On Error Resume Next

Dim SourceId, ManagedEntityId, TargetComputer, TargetComputerId
Dim oAPI, oDiscoveryData
Dim result
Dim message
Dim IsExitWithSnapshot

IsExitWithSnapshot = False
Set oError = New Error

Err.Clear
Set oAPI = CreateObject("MOM.ScriptAPI")
If (0 &lt;&gt; Err.Number) Then
WScript.Quit()
End If

result = GetArguments(SourceId,ManagedEntityId,TargetComputer,TargetComputerId)

If (False = result) Then
message = "Invalid number of arguments or empty arguments."
Call oAPI.LogScriptEvent(ScriptName, EventId, EventInformation, message)
IsExitWithSnapshot = True
Err.Clear
End If


Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
If (0 &lt;&gt; Err.Number) Then
message = "Cannot create discovery data object."
Call oAPI.LogScriptEvent(ScriptName, EventId, EventError, message)
WScript.Quit()
End If

If (True = IsExitWithSnapshot) Then
oDiscoveryData.IsSnapShot = False
Else
If ( False = DoDiscovery(TargetComputer, TargetComputerId, oDiscoveryData) ) Then
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
oDiscoveryData.IsSnapShot = False
End If
End If

Call oAPI.Return(oDiscoveryData)

End Sub


Function DoDiscovery(ByVal sTargetComputer, ByVal sTargetComputerID, ByVal oDisc)
Dim oInstance, InstallationType, WmiPrintRole
Dim LogName, nCount,DisplayName

DoDiscovery = False
On Error Resume Next
LogName = FullLogName
InstallationType = GetRegistryKeyValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "InstallationType")
InstallationType = LCase(InstallationType)

If (InstallationType = "server core") Then
LogName = CoreLogName
Else
If (InstallationType = "nano server") Then
DoDiscovery = True
Exit Function
End If
End If

Err.Clear

Set WmiPrintRole = WMIQuery(PrinterNameSpace,PrinterRoleQuery)

If (0 &lt;&gt; Err.number) Then
If ( WMI_ERROR_INVALIDCLASS = Err.number) Or (WMI_ERROR_INVALIDNAMESPACE = Err.number) Then
DoDiscovery = True
Set WmiPrintRole = Nothing
Err.Clear
Exit Function
Else
Set WmiPrintRole = Nothing
Exit Function
End If
End If

If (False = HasValue(WmiPrintRole)) Then
DoDiscovery = True
Set WmiPrintRole = Nothing
Err.Clear
Exit Function
End If

nCount = WmiPrintRole.Count
If (0 &lt;&gt; Err.number) Then
If ( WMI_ERROR_INVALIDCLASS = Err.number) Or (WMI_ERROR_INVALIDNAMESPACE = Err.number) Then
DoDiscovery = True
Set WmiPrintRole = Nothing
Err.Clear
Exit Function
Else
Set WmiPrintRole = Nothing
Exit Function
End If
End If

If (0 = nCount) Then
DoDiscovery = True
Set WmiPrintRole = Nothing
Err.Clear
Exit Function

End If

Set WmiPrintRole = Nothing
Set oInstance = oDisc.CreateClassInstance("$MPElement[Name='Microsoft.Windows.Server.10.0.PrintServerRole']$")

If Err.Number &lt;&gt; 0 Then
Set oInstance = Nothing
Exit Function
End If

DisplayName = "Windows Server 2016 Print Server (" &amp; sTargetComputer &amp; ")"
With oInstance
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", sTargetComputerID
.AddProperty "$MPElement[Name='Microsoft.Windows.Server.10.0.PrintServerRole']/EventLog$", LogName
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", DisplayName
End With

If Err.Number &lt;&gt; 0 Then
Exit Function
End If

Call oDisc.AddInstance(oInstance)

If 0 = Err.Number Then
DoDiscovery = True
End If

On Error Goto 0

End Function

Function GetArguments(ByRef SourceId,ByRef ManagedEntityId,ByRef sTargetComputer, ByRef sTargetComputerID)
On Error Resume Next
Err.Clear

GetArguments = False
Set objArguments = WScript.Arguments

If (Err.number &lt;&gt; 0) Then
Exit Function
End If

If (objArguments.Count &lt; 4) Then
Exit Function
End If

Dim objArguments

SourceId = Replace(objArguments(0), Chr(34), "")
ManagedEntityId = Replace(objArguments(1), Chr(34), "")
sTargetComputer = Replace(objArguments(2), Chr(34), "")
sTargetComputerID = Replace(objArguments(3), Chr(34), "")

If ( ( 0 &lt;&gt; Len(SourceId)) And (0 &lt;&gt; Len(sTargetComputerID)) And (0 &lt;&gt; Len(sTargetComputer)) And (0 &lt;&gt; Len(ManagedEntityId)) ) Then
GetArguments = True
End If
On Error Goto 0
End Function

Function GetRegistryKeyValue(ByVal keyPath, ByVal key)
Dim oReg, strKeyValue

strKeyValue = ""
GetRegistryKeyValue = strKeyValue
On Error Resume Next
Err.Clear
Set oReg = CreateObject("WScript.Shell")
If Err.Number &lt;&gt; 0 Then
Err.Clear
Exit Function
End If
strKeyValue = oReg.RegRead(keyPath &amp; key)
If Err.Number &lt;&gt; 0 Then
Err.Clear
Exit Function
End If

GetRegistryKeyValue = strKeyValue

' resume error
On Error Goto 0

End Function

Function WMIQuery(ByVal sNamespace, ByVal sQuery)
Dim oWMI, oInstance, nInstanceCount

On Error Resume Next
Set WMIQuery = Nothing
Err.Clear
Set oWMI = GetObject(sNamespace)

If (Err.Number &lt;&gt; 0 ) Then
Set oWMI = Nothing
Exit Function
End If

If (False = HasValue(oWMI)) Then
Set oWMI = Nothing
Exit Function
End If

Set oInstance = oWMI.ExecQuery(sQuery)
If (Err.Number &lt;&gt; 0 ) Then
Set oWMI = Nothing
Set oInstance = Nothing
Exit Function
End If

If (True = HasValue(oInstance)) Then
'Determine if we queried a valid WMI class - Count will return 0 or empty
nInstanceCount = oInstance.Count
If Err.Number = 0 Then
Set WMIQuery = oInstance
On Error Goto 0
Exit Function
End If
End If

End Function

Function HasValue(Value)

Dim bNothing

bNothing = false

IF ( IsObject(Value) ) THEN
IF (Nothing is Value) THEN
bNothing = true
END IF
END IF

HasValue = Not ( IsEmpty(Value) or bNothing or IsNull(Value) )
End Function


</Script></Contents>
<Unicode>false</Unicode>
</File>
<File>
<Name>vbparams.txt</Name>
<Contents><Script>"$Config/SourceId$" "$Config/ManagedEntityId$" "$Config/TargetComputer$" "$Config/TargetComputerId$"</Script></Contents>
<Unicode>false</Unicode>
</File>
<File>
<Name>psparams.txt</Name>
<Contents><Script>'$Config/SourceId$' '$Config/ManagedEntityId$' '$Config/TargetComputer$' '$Config/TargetComputerId$'</Script></Contents>
<Unicode>false</Unicode>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>