Gateway DMZOMGW2 Assigned Computers Group Discovery

SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group.Discovery (Discovery)

This discovery rule populates a group of Windows Computer Objects from a PowerShell Script

Element properties:

TargetSCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group
EnabledTrue
Frequency86331
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:

Member Modules:

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

Source Code:

<Discovery ID="SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group.Discovery" Enabled="true" Target="SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group"/>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>86331</IntervalSeconds>
<SyncTime/>
<ScriptName>SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group.Discovery.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Group Population script based on PowerShell
#
# Kevin Holman
# v1.0
#=================================================================================
param($SourceID, $ManagedEntityID, [string]$GroupDisplayName)


# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $SourceId = '{00000000-0000-0000-0000-000000000000}'
# $ManagedEntityId = '{00000000-0000-0000-0000-000000000000}'
# [string]$GroupDisplayName = "Gateway DMZOMGW1 Assigned Computers Group"
#=================================================================================


# Constants section - modify stuff here:
#=================================================================================
#Get the Gateway Name
$GroupDisplayNameSplit = $GroupDisplayName.Split(" ")
[string]$GWNetBIOSName = $GroupDisplayNameSplit[1]

# Assign script name variable for use in event logging
$ScriptName = "SCOM.GatewayMaintenanceMode." + $GWNetBIOSName + "Computers.Group.Discovery.ps1"
$EventID = "1236"
#=================================================================================


# Starting Script section - All scripts get this
#=================================================================================
# Gather the start time of the script
$StartTime = Get-Date
# Load MOMScript API
$momapi = New-Object -comObject MOM.ScriptAPI
# Load SCOM Discovery module
$DiscoveryData = $momapi.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
#Set variables to be used in logging events
$whoami = whoami
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`n Script is starting. `n Running as ($whoami).")
#=================================================================================


# Connect to local SCOM Management Group Section
#=================================================================================
# Clear any previous errors
$Error.Clear()
# Import the OperationsManager module and connect to the management group
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
Import-module $SCOMModulePath
New-DefaultManagementGroupConnection "localhost"
IF ($Error)
{
$momapi.LogScriptEvent($ScriptName,$EventID,1,"`n FATAL ERROR: Failure loading OperationsManager module or unable to connect to the management server. `n Terminating script. `n Error is: ($Error).")
EXIT
}
#=================================================================================


# Begin MAIN script section
#=================================================================================
#Get the gateway object
$Gateway = Get-SCOMManagementServer | where {$_.Name &#x2013;match $GWNetBIOSName}
$Agents = Get-SCOMAgent -ManagementServer $Gateway

###Discovery Section
#Set the group instance ID
[string]$GroupInstanceID = "SCOM.GatewayMaintenanceMode." + $GWNetBIOSName + "Computers.Group"
#Set the Relationship instance ID
[string]$RelationshipInstanceID = "SCOM.GatewayMaintenanceMode." + $GWNetBIOSName + "Computers.Group.Contains.Windows.Computers"

#Set the group instance we will discover members of
$GroupInstance = $DiscoveryData.CreateClassInstance("$MPElement[Name='SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group']$")

# Loop through each Name and add a group membership containment relationship to the discovery data
$i=0;
FOREACH ($Agent in $Agents)
{
[string]$AgentName = $Agent.Name
$i = $i+1
$ServerInstance = $DiscoveryData.CreateClassInstance("$MPElement[Name='Windows!Microsoft.Windows.Computer']$")
$ServerInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $AgentName)
$RelationshipInstance = $DiscoveryData.CreateRelationshipInstance("$MPElement[Name='SCOM.GatewayMaintenanceMode.DMZOMGW2Computers.Group.Contains.Windows.Computers']$")
$RelationshipInstance.Source = $GroupInstance
$RelationshipInstance.Target = $ServerInstance
$DiscoveryData.AddInstance($RelationshipInstance)
}

IF ($i -ge 1)
{
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nSuccessfully found ($i) Computers to add to the group: ($GroupInstanceID).")
}
ELSE
{
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nERROR: No computers were found for Gateway: ($GWNetBIOSName). `nTerminating script.")
EXIT
}

# Return Discovery Items Normally
$DiscoveryData

# Return Discovery Bag to the command line for testing (does not work from ISE)
# $momapi.Return($DiscoveryData)
#=================================================================================
# End MAIN script section


# End of script section
#=================================================================================
#Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`n Script Ending. `n Script Runtime: ($ScriptTime) seconds.")
#=================================================================================
#End Script
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>sourceId</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>managedEntityId</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>GroupDisplayName</Name>
<Value>$Target/Property[Type="System!System.Entity"]/DisplayName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</Discovery>