Agent.Management.Class.PowerShell.Properties.Discovery (Discovery)

Element properties:

TargetAgent.Management.Class
EnabledTrue
Frequency86493
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="Agent.Management.Class.PowerShell.Properties.Discovery" Enabled="true" Target="Agent.Management.Class" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Agent.Management.Class">
<Property PropertyID="ManagementGroups"/>
<Property PropertyID="PSVersion"/>
<Property PropertyID="PrimaryMS"/>
<Property PropertyID="FailoverList"/>
<Property PropertyID="ActionAccount"/>
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>86493</IntervalSeconds>
<SyncTime/>
<ScriptName>Agent.Management.Class.PowerShell.Properties.Discovery.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to gather agent properties via PowerShell
#=================================================================================
param($SourceId,$ManagedEntityId,$ComputerName,$MGName)

# For testing discovery manually in PowerShell:
# $SourceId = '{00000000-0000-0000-0000-000000000000}'
# $ManagedEntityId = '{00000000-0000-0000-0000-000000000000}'
# $Computername = 'server.domain.com'
# $MGName = 'SCOMA'

#=================================================================================
# Constants section - modify stuff here:

# Assign script name variable for use in event logging
$ScriptName = "Agent.Management.Class.PowerShell.Properties.Discovery.ps1"
#=================================================================================

# Gather script start time
$StartTime = Get-Date

# Gather who the script is running as
$whoami = whoami

#Load the MOMScript API and discovery propertybag
$momapi = New-Object -comObject "Mom.ScriptAPI"
$dbag = $momapi.CreateDiscoveryData(0, $sourceId, $managedEntityId)

#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,1006,0, "Starting script. Running as ($whoami)")

# Begin Main Script
#=================================================================================

#Check and see if OS is Nano server for use later where we must take different actions for Nano servers
$isNano = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels"
$isNano = $isNano.NanoServer
if($isNano -ne $null)
{
$isNano = $true
}
else
{
$isNano = $false
}

# Get Agent Management groups section
#=================================================================================
#Load SCOM Agent scripting module
$object=New-Object -ComObject "AgentConfigManager.MgmtSvcCfg"

#Get management groups
$MGs=$object.GetManagementGroups()

#Loop through each and create a comme seperated list
foreach ($MG in $MGs)
{
$MGList=$MGList + $MG.managementGroupName + ", "
}
$MGlist=$MGlist.TrimEnd(", ")
#=================================================================================

# Get PowerShell Version section
#=================================================================================
$PSVer = $PSVersionTable.PSVersion
[string]$PSMajor = $PSVer.Major
[string]$PSMinor = $PSVer.Minor
$PSVersion = $PSMajor + "." + $PSMinor
#=================================================================================

# Get PowerShell CLR Version section
#=================================================================================
$CLRVer = $PSVersionTable.CLRVersion
[string]$CLRMajor = $CLRVer.Major
[string]$CLRMinor = $CLRVer.Minor
$CLRVersion = $CLRMajor + "." + $CLRMinor
#=================================================================================

# Get Agent Assignments section
#=================================================================================
$SCOMRegKey = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup"
$SCOMAgentPath = (Get-ItemProperty $SCOMRegKey).InstallDirectory
$SCOMAgentPath = $SCOMAgentPath.TrimEnd("\")
$FilePath = "$SCOMAgentPath\Health Service State\Connector Configuration Cache\$MGName\OpsMgrConnector.Config.xml"

IF (Test-Path -Path $FilePath)
{
[xml]$ConfigFileXML = Get-Content -Path $FilePath

#Get Primary MS
$PrimaryArr = $ConfigFileXML.Message.State.Parents.Added.Item | Where-Object {$_.IsPrimary -eq "True"}
$PrimaryMS = $PrimaryArr.AuthenticationName

#Get list of Secondary MS
$SecondaryArr = $ConfigFileXML.Message.State.Parents.Added.Item | Where-Object {$_.IsPrimary -eq "False"}
[string]$SecondaryMSList = @()
FOREACH ($SecondaryXML in $SecondaryArr)
{
$SecondaryMS = $SecondaryXML.AuthenticationName
$SecondaryMSList = $SecondaryMSList + $SecondaryMS + ", "
}
$FailoverList = $SecondaryMSList.TrimEnd(", ")
}
ELSE
{
#Log script event that we cannot find config file
$momapi.LogScriptEvent($ScriptName,1006,2, "Cannot find config file at path ($FilePath)")
}
#=================================================================================

# Get Action Account section
#=================================================================================
# Get the action account this script is running under. We will assume that is the default agent action account
try
{
$user = ""
$domain = ""
if($isNano)
{
$user = $env:username
$domain = $env:userdnsdomain
}
else
{
$oNetwork = new-object -comobject "WScript.Network"
$user = $oNetwork.UserName
$domain = $oNetwork.UserDomain
}
If(($user.Length -eq 0) -or ($user -eq "SYSTEM"))
{
$ActionAccount = $user
}
Else
{
$ActionAccount = $domain + "\" + $user
}
}
catch
{
$oAPI.LogScriptEvent($ScriptName, 11, 1, "Failed to retrieve the User name and domain for the action account, error: ", $error.Description)
Exit -1;
}
#=================================================================================

# Get Remotely Manageable section
#=================================================================================
#=================================================================================

$instance = $dbag.CreateClassInstance("$MPElement[Name='Agent.Management.Class']$")
$instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $Computername)
$instance.AddProperty("$MPElement[Name='Agent.Management.Class']/ManagementGroups$", $MGList)
$instance.AddProperty("$MPElement[Name='Agent.Management.Class']/PSVersion$", $PSVersion)
$instance.AddProperty("$MPElement[Name='Agent.Management.Class']/CLRVersion$", $CLRVersion)
$instance.AddProperty("$MPElement[Name='Agent.Management.Class']/PrimaryMS$", $PrimaryMS)
$instance.AddProperty("$MPElement[Name='Agent.Management.Class']/FailoverList$", $FailoverList)
$instance.AddProperty("$MPElement[Name='Agent.Management.Class']/ActionAccount$", $ActionAccount)
$dbag.AddInstance($instance)

# Output Discovery Propertybag
$dbag

# Return Discovery Bag to the command line for testing (does not work from ISE):
# $momapi.Return($dbag)

# Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,1006,0,"`n Script has completed. `n Management Group list is ($MGList). `n PowerShell Version is ($PSVersion). `n Primary MS is ($PrimaryMS). `n Secondary MS Failover list is ($SecondaryMSList). `n Action Account: ($ActionAccount) `n Runtime was ($ScriptTime) seconds.")
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>SourceId</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>ManagedEntityId</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>ComputerName</Name>
<Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</Parameter>
<Parameter>
<Name>MGName</Name>
<Value>$Target/ManagementGroup/Name$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>120</TimeoutSeconds>
</DataSource>
</Discovery>