Dealing with Pending Agents

We were preparing our environment for an upgrade to SCOM 2012 R2 and a colleague asks me if there’s any easy way to grab a list of all servers that are sitting in Pending Management and then ping each one so he can determine whether to Approve or Reject them. Basically his idea was if they ping then they’re up and he’ll approve them. If they don’t ping, then he’ll reject them.

Sounds like sound logic :), so I decided to throw a little bit of PowerShell together for fun.

First up, I wanted to get a list of what we were dealing with, so here’s a little bit of code that’ll summarize how many agents you have listed in Pending Management and what type of Pending Status they are in. This doesn’t affect the outcome in any way, it’s just useful to have this information.

 

# Grab all of the Agents in Pending Management and display the results
“Pending Management Stats”
“========================”
$agentpending = Get-SCOMPendingManagement
switch ($agentpending | Select-Object AgentPendingActionType ) {
{$_.AgentPendingActionType -like “PushInstall”} {$pushInstall++}
{$_.AgentPendingActionType -like “PushInstallFailed”} {$PushInstallFailed++}
{$_.AgentPendingActionType -like “ManualApproval”} {$ManualApproval++}
{$_.AgentPendingActionType -like “RepairAgent”} {$repairAgent++}
{$_.AgentPendingActionType -like “RepairFailed”} {$RepairFailed++}
{$_.AgentPendingActionType -like “UpdateFailed”} {$UpdateFailed++}

}
“Push Install ……………….. = $pushInstall”
“Push Install Failed …………. = $PushInstallFailed”
“Manual Approval……………… = $ManualApproval”
“Repair Agent ……………….. = $repairAgent”
“Repair Failed ………………. = $RepairFailed”
“Update Failed ………………. = $UpdateFailed”

 

Next I wanted to only grab the agents and extract the name so I could ping them.

# A couple of placeholder arrays to store the names of the servers as we test them.
$badagents = @()
$goodagents = @()

# Grab all of the agents in Pending Management, optionally you might want to only select those agents with a specific Pending Type, like those set to Manual Approval. If you don’t care about which type they are, remove everything after the | symbol on the next line below.
$agentpending = Get-SCOMPendingManagement | ? {$_.AgentPendingActionType -eq “ManualApproval”}
# Grab only the Names of the Servers.
$servers = $agentpending | select -expandproperty AgentName
foreach ($server in $servers)
{
# Use ‘Test-Connection’ to Ping each Server.
if (!(Test-Connection -computername $Server -ea SilentlyContinue))

{
# Reject the Bad ones (those that FAIL the ping).
Write-Host -ForegroundColor Red “$server failed to ping”
$badagents += $server
Get-SCOMPendingManagement | ? {$_.AgentName -eq $server} | Deny-SCOMPendingManagement
}
else
{
# Approve the Good ones (those that SUCCEED with a ping).
Write-Host -ForegroundColor Green “$server is up”
$goodagents += $server

Get-SCOMPendingManagement | ? {$_.AgentName -eq $server} | Approve-SCOMPendingManagement
}
}

 

Since I didn’t know how many agents there were, I chose to ‘optionally’ send an email with the results of the script.

# Send an email with the results
send-mailmessage -to “Tim C <recipient@yourdomainhere.com>” -from “John Jones <sender@yourdomainhere.com>” -subject “Results of Pending Agents” -body “The following Agents in Pending Management will not Ping: $badagents `r`r The following Agents in Pending Management are responding: $goodagents” -smtpServer mail.yoursmtpserver.com

 

And the results will be something like this…

 

Pending Agent Email Results

 

 

 

 

 

 

 

 

 

Note: Yes I realise that there is superfluous code in this script, it was designed to be a quick and dirty solution rather than a robust script…but it works just fine. Hope you enjoy!

 

 
Comments

No comments yet.

Leave a Reply