Get SCOM Agents Greyed out, Uninitialized or Monitored State using Powershell

This Powershell Script gets All the agents for SCOM Instance, Display the results and Export to CSV if required.

Requirements: System Center Operations Manager
Tested Environment: System Center 2012 R2 Operations Manager, System Center Operations Manager 2007 & 2007 R2, Powershell Version 3.0 & 4.0
NOTE: Script is designed to run only on SCOM Management Server. 
Scripts is provided as ASIS and no personal or official guarantee is taken.
No Error catching is provided.

This script is designed to Get the SCOM Agent states, which can be Available or Not Available (Un-Monitored or Greyed out). Lot of people seems to be interested in Greyed out State Agents.

Specially Greyed out agents can be challenging, because agent is still communicating with Management Server. Under normal circumstances an Alert should be raised as “Health Service Heartbeat Failure. Problem with Greyed out agent is agent is not communicating properly and needs to be solved quickly. If not fixed quickly then may miss critical failures or alerts.

To fix Grey agents, generally you have to flush the cache agent on agent machine.
Tim Culham has great instructions on How to Flush agent successfully – http://www.culham.net/powershell/flushing-a-scom-agent-cache-using-powershell/

Script has certain configurations which can be set to true or false.

  1. Export to CSV – $blnExportToCSV  True or False
    1. Sngle CSV for All Agent regardless of state – $blnSingleCSV as Strue
    2. Individual CSV for each State type – $blnSingleCSV as Sfalse
  2. Display Results when script is executed – $blnShowResults  as True or False

 

 

#*******************************************************************
# Get-SCOMAgentsState.ps1
# This script is designed to Get the SCOM Agent states.
# State can be Available and Not Available (Un-Monitored or Grey)
# Script is designed to run from one of the SCOM Management Server
# Script is tested with System Center 2007 and 2012 R2 versions
# Author: Harmik Singh Batth
# Modified On: 10-OCT-2016
# Modifed By: Harmik Singh Batth
# Version: 1.0
# Change History:
#
#
#*******************************************************************

#*********************************CONFIGURATIONS*******************
#Variables
$ArrAllAgentsState = New-Object System.Collections.ArrayList
$ArrAgentsinHealthyState = New-Object System.Collections.ArrayList
$ArrAgentsinUnMonitoredState = New-Object System.Collections.ArrayList
$ArrAgentsinGreyState = New-Object System.Collections.ArrayList

#Determine whether you would like to generate CSV file
$blnExportToCSV = $true

#Determine whether you would like to have single CSV for each Health State or Single combined CSV
$blnSingleCSV = $false

#Get Current Directory where script is running from
$strBaseDir = (Split-Path $MyInvocation.MyCommand.Definition)

#Get Hostname of Computer this script is executed from
$Hostname = hostname

#Specify File names for Individual CSVs
$strAgentsStateFile = “$strBaseDir\$Hostname-SCOMAgentsState.csv”
$strAgentsinHealthyStateFile = “$strBaseDir\$Hostname-SCOMAgentsinHealthyState.csv”
$strAgentsinUnMonitoredStateFile = “$strBaseDir\$Hostname-SCOMAgentsinUnMonitoredState.csv”
$strAgentsinGreyStateFile = “$strBaseDir\$Hostname-SCOMAgentsinGreyState.csv”

#Determine whether or not display results in Script Windows
$blnShowResults = $true

#Count Number of Agents
$TotalAgentsCount = 0
$TotalAgentsinHealthyStateCount = 0
$TotalAgentsinUnmonitoredStateCount = 0
$TotalAgentsinGreyStateCount = 0

#*******************************************************************

#***********************SCRIPT STARTS FROM HERE********************

#Check which version of SCOM is installed
#Detect the Operations Managers installation via Registry. WMI dont work well with installed softwares.
$SCOM2012Version = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where {$_.DisplayName -like “*2012*Operations Manager*”} | Select-Object -expand Displayversion

$SCOM2007Version = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where {$_.DisplayName -like “*Operations Manager*2007*”} | Select-Object -expand Displayversion

if ($SCOM2012Version -like “7.*”)
{
Write-Host “SCOM Version is System Center 2012 R2 Operations Managers”
}

if ($SCOM2007Version -like “6.*”)
{
Write-Host “SCOM Version is System Center Operations Manager 2007 R2”
}

#Load System Center Operations Manager module
if ($SCOM2012Version)
{
#Initialize SCOM snapin
Import-Module OperationsManager

#Get the agent class and the each object which is in a grey state
$MSCAgent = Get-SCClass -name “Microsoft.SystemCenter.Agent”
$objects = Get-SCOMMonitoringObject -class:$MSCAgent | Where-Object {($_.InMaintenanceMode -eq $False)}
}
Elseif ($SCOM2007Version)
{
#Initialize SCOM snapin
$RMS=’localhost’
$strSpapin =’Microsoft.EnterpriseManagement.OperationsManager.Client’
$objSnapin = Get-PSSnapin | ?{$_.Name -eq $strSpapin}
if (-not $objSnapin) { Add-PSSnapin $strSpapin }

set-location ‘OperationsManagerMonitoring::’ | out-null
new-managementGroupConnection -ConnectionString:$RMS | out-null
set-location $RMS

#Get the agent class and the each object which is in a grey state
$MSCAgent = get-monitoringclass -name “Microsoft.SystemCenter.Agent”
$objects = Get-MonitoringObject -monitoringclass:$MSCAgent | Where-Object {($_.InMaintenanceMode -eq $False)}
}
Else
{
Write-Host “Appropriate module for Operations Manager not found, exiting script now”
break
}

#Check if Script path is obtained or not
if (!($strBaseDir))
{
if (!(Test-path “C:\temp\”))
{
New-Item -ItemType directory “c:\temp”
}
$strBaseDir = “C:\temp”
$strAgentsStateFile = “$strBaseDir\SCOMAgentsState.csv”
$strAgentsinHealthyStateFile = “$strBaseDir\SCOMAgentsinHealthyState.csv”
$strAgentsinUnMonitoredStateFile = “$strBaseDir\SCOMAgentsinUnMonitoredState.csv”
$strAgentsinGreyStateFile = “$strBaseDir\SCOMAgentsinGreyState.csv”
}

#Export files as required
if ($blnExportToCSV)
{
#Export Single CSV with results
if ($blnSingleCSV -eq $true)
{
$objects | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsStateFile
Write-host “Exported the File to $strAgentsStateFile”

$TotalCount = $objects.Count

ForEach ($object in $objects)
{
$ArrAllAgentsState.add($object.DisplayName)
}

Write-host “All Agents State – ($TotalCount)”
Write-host “——————————-”
foreach ($agent in $ArrAllAgentsState)
{
write-host $Objagent
}
Write-host “——————————-”
}
else
{
#Export each object into a file

if ($SCOM2012Version)
{
#Get all Healthy Agents
$TotalAgentsinHealthyState = Get-SCOMMonitoringObject -class:$MSCAgent
$TotalAgentsinHealthyState | Where-Object {($_.IsAvailable -eq $true) -and ($_.InMaintenanceMode -eq $False)} | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsinHealthyStateFile
$TotalAgentsinHealthyStateCount = ($TotalAgentsinHealthyState | Where-Object {($_.IsAvailable -eq $true) -and ($_.InMaintenanceMode -eq $False)}).count

#Get all Un Monitored Agents
$TotalAgentsinUnmonitoredState = Get-SCOMMonitoringObject -class:$MSCAgent
$TotalAgentsinUnmonitoredState | Where-Object {($_.IsAvailable -eq $false) -and ($_.HealthState -eq “Uninitialized”) -and ($_.InMaintenanceMode -eq $False)} | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsinUnMonitoredStateFile
$TotalAgentsinUnmonitoredStateCount = ($TotalAgentsinUnmonitoredState | Where-Object {($_.IsAvailable -eq $false) -and ($_.HealthState -eq “Uninitialized”) -and ($_.InMaintenanceMode -eq $False)}).Count

#Get all Grey Agents
$TotalAgentsinGreyState = Get-SCOMMonitoringObject -class:$MSCAgent
$TotalAgentsinGreyState | Where-Object {($_.IsAvailable -eq $false) -and (!($_.HealthState -eq “Uninitialized”)) -and ($_.InMaintenanceMode -eq $False)} | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsinGreyStateFile
$TotalAgentsinGreyStateCount = ($TotalAgentsinGreyState | Where-Object {($_.IsAvailable -eq $false) -and (!($_.HealthState -eq “Uninitialized”)) -and ($_.InMaintenanceMode -eq $False)} ).Count
}
Elseif ($SCOM2007Version)
{
#Get all Healthy Agents
$TotalAgentsinHealthyState = Get-MonitoringObject -monitoringclass:$MSCAgent
$TotalAgentsinHealthyState | Where-Object {($_.IsAvailable -eq $true) -and ($_.InMaintenanceMode -eq $False)} | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsinHealthyStateFile
$TotalAgentsinHealthyStateCount = ($TotalAgentsinHealthyState | Where-Object {($_.IsAvailable -eq $true) -and ($_.InMaintenanceMode -eq $False)}).count

#Get all Un Monitored Agents
$TotalAgentsinUnmonitoredState = Get-MonitoringObject -monitoringclass:$MSCAgent
$TotalAgentsinUnmonitoredState | Where-Object {($_.IsAvailable -eq $false) -and ($_.HealthState -eq “Uninitialized”) -and ($_.InMaintenanceMode -eq $False)} | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsinUnMonitoredStateFile
$TotalAgentsinUnmonitoredStateCount = ($TotalAgentsinUnmonitoredState | Where-Object {($_.IsAvailable -eq $false) -and ($_.HealthState -eq “Uninitialized”) -and ($_.InMaintenanceMode -eq $False)}).Count

#Get all Grey Agents
$TotalAgentsinGreyState = Get-MonitoringObject -monitoringclass:$MSCAgent
$TotalAgentsinGreyState | Where-Object {($_.IsAvailable -eq $false) -and (!($_.HealthState -eq “Uninitialized”)) -and ($_.InMaintenanceMode -eq $False)} | Select DisplayName, HealthState, IsAvailable | Export-Csv $strAgentsinGreyStateFile
$TotalAgentsinGreyStateCount = ($TotalAgentsinGreyState | Where-Object {($_.IsAvailable -eq $false) -and (!($_.HealthState -eq “Uninitialized”)) -and ($_.InMaintenanceMode -eq $False)} ).Count
}
else
{

}

}
}

#Display Results
if ($blnShowResults -eq $true)
{
ForEach ($object in $objects)
{
#Agent is in Healthy State
if ($object.IsAvailable -eq “True”)
{
$ArrAgentsinHealthyState.add($object.DisplayName)
}
Else #Agent is Not in Healthy State
{
if ($object.HealthState -eq “Uninitialized”)
{
$ArrAgentsinUnMonitoredState.add($object.DisplayName)
}
Else
{
$ArrAgentsinGreyState.add($object.DisplayName)
}
}
}

Write-host “All Agents in Healthy State – ($TotalAgentsinHealthyStateCount)”
Write-host “——————————-”
foreach ($agent in $ArrAgentsinHealthyState)
{
write-host $agent
}
Write-host “——————————-”
Write-host ” ”
Write-host “All Agents in UnMonitored State – ($TotalAgentsinUnmonitoredStateCount)”
Write-host “——————————-”
foreach ($agent in $ArrAgentsinUnMonitoredState)
{
write-host $agent
}
Write-host “——————————-”
Write-host ” ”
Write-host “All Agents in Grey State – ($TotalAgentsinGreyStateCount)”
Write-host “——————————-”
foreach ($agent in $ArrAgentsinGreyState)
{
write-host $agent
}
Write-host “——————————-”
}
Write-host ” ”

Write-host “Script Finished Successfully”
#*************************SCRIPT ENDS HERE**********************

 

I hope this helps you!
Please feel free to comment and also ways to improve the script.
I welcome positive or negative feedback.

One thought on “Get SCOM Agents Greyed out, Uninitialized or Monitored State using Powershell

  1. This is very helpful. Thank you very much.
    I modified it a bit to suit my current organizations requirements and works perfectly.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s