# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.Agent.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
#=================================================================================
#Get management groups
$MGs=$AgentCfg.GetManagementGroups()
#Loop through each and create a comme seperated list
foreach ($MG in $MGs)
{
$MGList=$MGList + $MG.managementGroupName + ", "
}
$MGlist=$MGlist.TrimEnd(", ")
#=================================================================================
# Get Agent OMS Workspaces section
#=================================================================================
# This section depends on AgentConfigManager.MgmtSvcCfg object in previous section
# Agent might not support OMS
$AgentSupportsOMS = $AgentCfg | Get-Member -Name 'GetCloudWorkspaces'
IF (!$AgentSupportsOMS)
{
#This agent version does not support Cloud Workspaces.
$OMSList=''
}
ELSE
{
$OMSWorkSpaces = $AgentCfg.GetCloudWorkspaces()
FOREACH ($OMSWorkSpace in $OMSWorkSpaces)
{
$OMSList = $OMSList + $OMSWorkspace.workspaceId + ", "
}
$OMSList = $OMSList.TrimEnd(", ")
# If Windows 10 or WS2016 use new reg keys else use old keys
IF ($OSCurrentCurrentMajorVersionNumber)
{
[string]$OSVersion = $OSCurrentCurrentMajorVersionNumber + "." + $OSCurrentCurrentMinorVersionNumber + "." + $OSCurrentBuildNumber
}
ELSE
{
[string]$OSVersion = $OSCurrentVersion + "." + $OSCurrentBuildNumber
}
#=================================================================================
# Get Remotely Manageable section
#=================================================================================
#=================================================================================
# 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 ADIntEnabled is ($ADIntEnabled). `n Management Group list is ($MGList). `n OMS Workspace list is ($OMSList). `n ProxyURL is ($ProxyURL). `n PowerShell Version is ($PSVersion). `n CLR Version is ($CLRVersion). `n Primary MS is ($PrimaryMS). `n Secondary MS Failover list is ($FailoverList). `n Action Account: ($ActionAccount). `n OSVersion is ($OSVersion). `n SCOMAgentPath is ($SCOMAgentPath). `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>