Collector Discovery datasource

TheBackbone.SMP04.MG.Discovery.DS (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
TheBackbone.SMP04.MG.Discovery.DS DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Frequencyint$Config/Frequency$FrequencySpecifies the frequency to run the discovery
CollectorPathstring$Config/CollectorPath$Collector PathSpecifies the path of the collector
CollectorIntervalint$Config/CollectorInterval$Collector IntervalSpecifies the interval to process collected data
Debugbool$Config/Debug$DebugEnables the debug option in the discovery script

Source Code:

<DataSourceModuleType ID="TheBackbone.SMP04.MG.Discovery.DS" Accessibility="Public">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Frequency" minOccurs="1" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CollectorPath" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CollectorInterval" minOccurs="1" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Debug" minOccurs="0" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Frequency" Selector="$Config/Frequency$" ParameterType="int"/>
<OverrideableParameter ID="CollectorPath" Selector="$Config/CollectorPath$" ParameterType="string"/>
<OverrideableParameter ID="CollectorInterval" Selector="$Config/CollectorInterval$" ParameterType="int"/>
<OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="TheBackbone.SMP04.MG.Discovery.DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>$Config/Frequency$</IntervalSeconds>
<SyncTime/>
<ScriptName>TheBackbone.SMP04.MG.Discovery</ScriptName>
<ScriptBody><Script>param($Element, $TargetId, $ComputerName, $CollectorPath, $CollectorInterval, $Debug)

function Debug ($eventId, $Message){
if($Debug){
$oAPI.LogScriptEvent("TheBackbone.SMP04.MG.Discovery.ps1", $eventId, 0, $Message)
}
}

Debug 1000 "Script Started"

$MGName = $null
$oAPI = new-object -comObject "MOM.ScriptAPI"
$Discovery = $oAPI.CreateDiscoveryData(0, $Element, $TargetId)

$MGName = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Agent Management Groups" | ForEach-Object {Get-ItemProperty $_.pspath | select -expandproperty PSChildName}

if([string]::IsNullOrEmpty($MGName))
{
Debug 1001 "Not a agent health service, retrieve management group name from server regkey"
$MGName = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Server Management Groups" | ForEach-Object {Get-ItemProperty $_.pspath | select -expandproperty PSChildName}
}

Debug 1002 "Management group name: $MGName"

#Verify if the path ends with a directory seperator character
If(-not($CollectorPath.EndsWith([System.IO.Path]::DirectorySeparatorChar.ToString())))
{
$CollectorPath += [System.IO.Path]::DirectorySeparatorChar.ToString()
}

# Met een regex controleren of het collectorpath tekst tussen %% bevat
# Zo ja, voor elke match met (get-item env:$x).Value de daadwerkelijke waarde opvragen
# Vervolgens een replace uitvoeren om de enviroment variable aan te passen naar de effectieve waarde
# event 1003 genereren en script exit 1 uitvoeren
$Regex = [regex] '(?is)(?&lt;=%).*?(?=%)'
$RegexMatches = $Regex.Matches($CollectorPath);

if($RegexMatches.Success)
{
foreach($RegexMatch in $RegexMatches)
{
$EnvVar = (get-item env:$RegexMatch).Value

if([string]::IsNullOrEmpty($EnvVar))
{
$oAPI.LogScriptEvent("TheBackbone.SMP04.MG.Discovery.ps1", 1003, 2, "The environment variable %$RegexMatch% in the collector path could not be resolved. Collector instance is not created or changed")
exit 1
}
else
{
$CollectorPath = $CollectorPath.Replace("%$RegexMatch%", $EnvVar)
}
}
}

$instance = $Discovery.CreateClassInstance("$MPElement[Name='TSL!TheBackbone.SMP04.Collector.Class']$")
$instance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $ComputerName)
$instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$",$ComputerName)
$instance.AddProperty("$MPElement[Name='TSL!TheBackbone.SMP04.Collector.Class']/MGName$", $MGName)
$instance.AddProperty("$MPElement[Name='TSL!TheBackbone.SMP04.Collector.Class']/CollectorPath$", $CollectorPath)
$instance.AddProperty("$MPElement[Name='TSL!TheBackbone.SMP04.Collector.Class']/CollectorInterval$", $CollectorInterval)
$Discovery.AddInstance($instance)

$Discovery

Debug 1009 "Script Ended"


</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>Element</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>TargetId</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>ComputerName</Name>
<Value>$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</Parameter>
<Parameter>
<Name>CollectorPath</Name>
<Value>$Config/CollectorPath$</Value>
</Parameter>
<Parameter>
<Name>CollectorInterval</Name>
<Value>$Config/CollectorInterval$</Value>
</Parameter>
<Parameter>
<Name>Debug</Name>
<Value>$Config/Debug$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="TheBackbone.SMP04.MG.Discovery.DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>