# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $SourceId = '{00000000-0000-0000-0000-000000000000}'
# $ManagedEntityId = '{00000000-0000-0000-0000-000000000000}'
# $Computername = 'server.domain.com'
# $MGName = 'SCOMA'
# $IP = '123.123.123.123'
#=================================================================================
# Constants section - modify stuff here:
#=================================================================================
# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.Agent.Class.PowerShell.Properties.Discovery.ps1"
$EventID = "1006"
#SCOM Management Servers or Gateways that we wish to test the port availability to using FQDN seperated by a comma such as "scom1.opsmgr.net","scom2.opsmgr.net","scom3.opsmgr.net"
[array]$Parents = ""
#=================================================================================
# Starting Script section - All scripts get this
#=================================================================================
# Gather the start time of the script
$StartTime = Get-Date
#Set variable to be used in logging events
$whoami = whoami
# Load MOMScript API
$momapi = New-Object -comObject MOM.ScriptAPI
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nScript is starting. `nManagement Group: ($MGName). `nRunning as ($whoami).")
#=================================================================================
# Begin MAIN script section
#=================================================================================
#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
}
# If nothing else found then default to version number
default {$SCOMAgentVersion}
}
#=======================================================================
# Get Agent Management groups section
#=======================================================================
#Get management groups
$MGs=$AgentCfg.GetManagementGroups()
#Loop through each and create a comma 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
[string]$OMSList=''
# Agent might not support OMS
$AgentSupportsOMS = $AgentCfg | Get-Member -Name 'GetCloudWorkspaces'
IF (!$AgentSupportsOMS)
{
#This agent version does not support Cloud Workspaces.
}
ELSE
{
$OMSWorkSpaces = $AgentCfg.GetCloudWorkspaces()
FOREACH ($OMSWorkSpace in $OMSWorkSpaces)
{
$OMSList = $OMSList + $OMSWorkspace.workspaceId + ", "
}
IF ($OMSList)
{
$OMSList = $OMSList.TrimEnd(", ")
}
# Get Agent AD Integration Setting
#=======================================================================
# This section depends on AgentConfigManager.MgmtSvcCfg object in previous section
# Check Agent version for 2012 or a later version as these use different commands
IF ($MajorSCOMAgentVersion -lt 8)
{
# Assume SCOM 2012 agent
$ADIntEnabled = $AgentCfg.GetActiveDirectoryIntegrationEnabled()
}
Else
{
# Assume SCOM 2016 agent or later
$ADIntEnabled = $AgentCfg.ActiveDirectoryIntegrationEnabled
}
#=======================================================================
# 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
}
#=======================================================================
# Build IP List from Windows Computer Property
#=======================================================================
#We want to remove Link local IP
[string]$IPList = ""
$IPSplit = $IP.Split(",")
FOREACH ($IPAddr in $IPSplit)
{
[string]$IPAddr = $IPAddr.Trim()
write-host $IPAddr
IF (!($IPAddr.StartsWith("fe80") -or $IPAddr.StartsWith("169.254")))
{
$IPList = $IPList + $IPAddr + ","
}
}
$IPList = $IPList.TrimEnd(",")
#=======================================================================
# Get port connection availability to an array of parents
#=======================================================================
[string]$PortList = ""
IF ($Parents)
{
FOREACH ($Parent in $Parents)
{
[string]$PortAvail = ""
$ip=([System.Net.Dns]::GetHostAddresses($Parent)).IPAddressToString;
$tcp=New-Object net.sockets.tcpclient;$tcp.Connect($Parent,5723);
$out=$tcp.Connected;
# write-host "`nPort 5723 test result for ($Parent) on IP ($ip) : ($out)"
# Return Discovery Items Normally
$DiscoveryData
# Return Discovery Bag to the command line for testing (does not work from ISE)
# $momapi.Return($DiscoveryData)
#=================================================================================