M365SL.WatcherNode.POSH.Discovery.DS (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsM365SL.RunAs.Profile
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Default

Overrideable Parameters:

IDParameterTypeSelector
IntervalSecondsint$Config/IntervalSeconds$
PoshLibraryPathstring$Config/PoshLibraryPath$
ProbeActionTimeoutSecondsint$Config/ProbeActionTimeoutSeconds$
SyncTimestring$Config/SyncTime$
WriteToEventLogbool$Config/WriteToEventLog$

Source Code:

<DataSourceModuleType ID="M365SL.WatcherNode.POSH.Discovery.DS" Accessibility="Public" Batching="false" RunAs="M365SL.RunAs.Profile">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ComputerName" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" minOccurs="1" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MgmtGroupRegKey" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="PoshLibraryPath" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ProbeActionTimeoutSeconds" minOccurs="1" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SyncTime" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WorkflowName" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WriteToEventLog" minOccurs="1" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="PoshLibraryPath" Selector="$Config/PoshLibraryPath$" ParameterType="string"/>
<OverrideableParameter ID="ProbeActionTimeoutSeconds" Selector="$Config/ProbeActionTimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string"/>
<OverrideableParameter ID="WriteToEventLog" Selector="$Config/WriteToEventLog$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>$Config/SyncTime$</SyncTime>
<ScriptName>M365SL.WatcherNode.Discovery.ps1</ScriptName>
<ScriptBody><Script>&lt;#
Filename: M365SL.WatcherNode.Discovery.ps1
Author: Tyson Paul ( https://MonitoringGuys.com )

History
2020.11.17.2205 - Added API Url params
2020.11.04.1705 - Added IntervalSeconds
2020.09.28.1809 - Added accountname/accountpassword params to watchernode.
2020.09.23.1508 - Load-Library
2020.09.17.1435 - Added TLS discovery.
2020.09.02.1817 - Added client auth properties to discovery
2020.08.09.1513 - removed 'Enabled' from discovery. Added Testing output.
2020.05.22 V1
========================================================================
#&gt;
Param (

[string]$ManagedEntityID = "No ManagedEntityID defined",
[string]$MgmtGroupRegKey,
[string]$PoshLibraryPath, #comma-separated list of .ps1 files to load
[string]$PrincipalName = "No PrincipalName defined!",
[string]$SourceID = "No SourceID defined",
[string]$WorkflowName = '&lt;No Name Provided&gt;',
[string]$WriteToEventLog="false"
)
[bool]$WriteToEventLog = [System.Convert]::ToBoolean($WriteToEventLog)
$Testing=$false
[string]$ScriptName = 'M365SL.WatcherNode.Discovery.ps1'
$NameSpace = 'Library'
######################### FUNCTIONS ############################
################################################################
Function Load-Library {
Param (
[string]$PoshLibraryPath
)
$ErrorActionPreference = 'STOP'
If ($PoshLibraryPath ){
ForEach ($Path in $PoshLibraryPath.Split(',') ){
Try {
If (($Path.Length) -AND ($Path -notmatch '^-1$')) {
. $Path
}
} Catch {
Write-Host "Line [$($MyInvocation.ScriptLineNumber )]: Error loading PoshLibrary at path:[$($Path)]. This is likely to cause many other dependent functions to fail. `n`nError data: $($_)`n`n"
}
}
}
$ErrorActionPreference = 'CONTINUE'
}
################################################################

############## TESTING ##############
&lt;# #Run this as needed when testing
# powershell.exe -file C:\Test\M365SMP_Dev\library\M365SL.WatcherNode.Discovery.ps1 &gt; C:\Test\M365SMP_Dev\DiscoveryData.xml

Function Testing {
$Testing = $true
Get-Item Alias:\_LINE_ -ErrorAction Ignore | Remove-Item -ErrorAction Ignore
$testParamsFile = (Join-path $TestFolder ("PARAMS_$($ScriptName)"))
#Write-Host "$(Test-Path $testParamsFile):$($testParamsFile)" -F Yellow -B Green
. $testParamsFile
. Load-Library -PoshLibraryPath $PoshLibraryPath

$error.Clear()
}

$TestFolder = "C:\Test\M365SMP_Dev\$($NameSpace)\TestSetup"
If (Test-Path -Path $TestFolder) {
. Testing
}

#&gt;
############## TESTING ##############

. Load-Library -PoshLibraryPath $PoshLibraryPath

LogIt -EventID 9990 -Type $info -msg "Script Begin." -Proceed $WriteToEventLog -Line $(_LINE_); $Error.Clear();

$MgmtGroupRegKeyPath = $MgmtGroupRegKey.Replace("HKLM\",'HKLM:\')

$api = New-Object -comObject 'MOM.ScriptAPI'
$discoveryData = $api.CreateDiscoveryData(0, $SourceID, $ManagedEntityID)

Try {
$null = Test-Path $MgmtGroupRegKeyPath -ErrorAction Stop
} Catch {
# Return empty discovery data. This will undiscover entities if they were previously discovered.
$discoveryData
LogIt -EventID 9991 -Type $info -Msg "RegKey does not exist:[$($MgmtGroupRegKeyPath)] . Exiting Discovery workflow: [$WorkflowName]. " -Proceed $WriteToEventLog -LINE $(_LINE_); $Error.Clear()
Exit
}

# Force this to be an array for consistency
$Tenants = @(Get-ChildItem -Path $MgmtGroupRegKeyPath | Select-Object Name -ExpandProperty Name)

ForEach ($Tenant in $Tenants){
LogIt -EventID 9991 -Type $info -Msg "Proceed to discovery type: [M365SL.WatcherNode] for tenant: [$($TenantPath)]..." -Proceed $WriteToEventLog -LINE $(_LINE_); $Error.Clear()
Try{
$instance = $discoveryData.CreateClassInstance('$MPElement[Name="M365SL.WatcherNode"]$')

$TenantName = Split-Path $Tenant -Leaf
$TenantPath = (Join-Path $MgmtGroupRegKeyPath $TenantName)
$TenantProperties = Get-ItemProperty -Path $TenantPath

$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/ApiURL$", [string]($TenantProperties.ApiURL) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/ApiTokenURL$", [string]($TenantProperties.ApiTokenURL) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/ApiTokenScopeURL$", [string]($TenantProperties.ApiTokenScopeURL) )

$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/M365_AccountName$", [string]($TenantProperties.M365_AccountName) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/M365_AccountPassword$", [string]($TenantProperties.M365_AccountPassword) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/M365_ClientID$", [string]($TenantProperties.M365_ClientID) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/M365_ClientSecret$", [string]($TenantProperties.M365_ClientSecret) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/IntervalSeconds$", [string]($TenantProperties.IntervalSeconds) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/Location$", [string]($TenantProperties.Location) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/MgmtGroupRegKey$", [string]($MgmtGroupRegKey) )
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/TLSVersion$", [string]($TenantProperties.TLSVersion) )

#Key
$instance.AddProperty("$MPElement[Name='M365SL.WatcherNode']/TenantName$", [string]($TenantName) )

# Standard Props
$instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $PrincipalName)
$instance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $TenantName)
$discoveryData.AddInstance($instance)
LogIt -EventID 9992 -Type $info -Msg "Discovery object for tenant: [$($TenantPath)] added to batch!" -Proceed $WriteToEventLog -LINE $(_LINE_); $Error.Clear()
} Catch {
$msg = "There was a critical failure in discovery for tenant at path: [$($Tenant)]"
LogIt -EventID 9995 -Type $Critical -Msg $msg -Proceed $true -LINE $(_LINE_); $Error.Clear()
}
}#End ForEach Tenant

If ($Testing) {
$api.Return($discoveryData)
}
Else {
$discoveryData
LogIt -EventID 9992 -Type $info -Msg "Returned discovery data for tenant: [$($TenantPath)] " -Proceed $WriteToEventLog -LINE $(_LINE_); $Error.Clear()
}
$TotalSeconds = "{0:N4}" -f $ScriptTimer.Elapsed.TotalSeconds
LogIt -EventID 9991 -Type $info -Msg "Discovery finished in [$($TotalSeconds)] seconds. " -Proceed $WriteToEventLog -LINE $(_LINE_); $Error.Clear()</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>ManagedEntityId</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>MgmtGroupRegKey</Name>
<Value>$Config/MgmtGroupRegKey$</Value>
</Parameter>
<Parameter>
<Name>PoshLibraryPath</Name>
<Value>$FileResource[Name='Res.M365SL.M365Library.ps1.Resource']/Path$,$Config/PoshLibraryPath$</Value>
</Parameter>
<Parameter>
<Name>PrincipalName</Name>
<Value>$Config/ComputerName$</Value>
</Parameter>
<Parameter>
<Name>SourceId</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>WorkflowName</Name>
<Value>$Config/WorkflowName$</Value>
</Parameter>
<Parameter>
<Name>WriteToEventLog</Name>
<Value>$Config/WriteToEventLog$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/ProbeActionTimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>