# 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(", ")
#=================================================================================
# 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>