Approve SCOM pending agents via PowerShell

Recently I had to approve several SCOM pending agents in my environment. Of course I won’t do this manually so I started searching for the answers on internet 🙂 . I found SCOM module command called Approve-SCOMPendingManagement and I decided to create a script based on that and share it with you. Remember that you need appropriate permission to approve servers in pending management.

SCOM pending agents

Approving single server is pretty easy. You need to just use Where-Object in Get-SCOMPendingManagement, provide FQDN of server and after the pipeline use approve command:

#Checking if server is in pending management
Get-SCOMPendingManagement | Where-Object {$_.AgentName -eq "Server01.powershellbros.com"} 

#Checking if server is in pending management and approve it
Get-SCOMPendingManagement | Where-Object {$_.AgentName -eq "Server01.powershellbros.com"} | Approve-SCOMPendingManagement
SCOM pending agents example
SCOM pending agents example

For selecting servers list I used the same method based on System.Windows.Forms as in the “Tip of the week” article – link.

#Browsing file
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.filter = "Txt (*.txt)| *.txt"
[void]$FileBrowser.ShowDialog()
 
#Getting servers from txt file
Try
{
    $FilePath = $FileBrowser.FileName
    $Servers = Get-Content -Path $FilePath -ErrorAction Stop
}
Catch
{
  $_.Exception.Message
  Break
}

Output should look similar to that:

SCOM pending agents
SCOM pending agents

Final script:


Import-Module OperationsManager
$Array = @()
$ErrorActionPreference = 'SilentlyContinue'

#Browsing file
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.filter = "Txt (*.txt)| *.txt"
[void]$FileBrowser.ShowDialog()
 
#Getting servers from txt file
Try
{
    $FilePath = $FileBrowser.FileName
    $Servers = Get-Content -Path $FilePath -ErrorAction Stop
}
Catch
{
  $_.Exception.Message
  Break
}

If( $Servers.length -eq 0 ) 
{ 
    Write-Warning "Servers list is empty" 
    Pause 
    Break 
}

# Looping each server
Foreach($Server in $Servers)
{
    $Server = $Server.Trim()
    $FQDN = ([System.Net.Dns]::GetHostByName(("$Server")))

    If(!$FQDN)
    {
        Write-Warning "$Server does not exist"
    }
    Else
    {
        $Hostname = ($FQDN.HostName | Out-String).Trim()
        $CheckPending = Get-SCOMPendingManagement | Where-Object {$_.AgentName -eq "$Hostname"} 

        If(!$CheckPending)
        {
            $Object = New-Object PSObject -Property ([ordered]@{ 
   
                    "Server name"         = $Hostname
                    IPAddress             = $FQDN.AddressList[0]
                    Pending               = "False"
                    Approved              = "(null)" 
      
            })
     
            # Add custom object to our array
            $Array += $Object 
        }
        Else
        {
            # Approve server
            Get-SCOMPendingManagement | Where-Object {$_.AgentName -eq "$Hostname"} | Approve-SCOMPendingManagement
            
            $Check = Get-SCOMPendingManagement | Where-Object {$_.AgentName -eq "$Hostname"} 
    
            If(!$Check)
            {
                Write-Host "$Server has been approved" -ForegroundColor Green
                $Status = "True"
            }
            Else
            {
                Write-Host "$Server has not been approved" -ForegroundColor Red
                $Status = "False"
            }

            $Object = New-Object PSObject -Property ([ordered]@{ 
   
                    "Server name"         = $Hostname
                    IPAddress             = $FQDN.AddressList[0]
                    Pending               = "True"
                    Approved              = $Status
      
            })
     
            # Add custom object to our array
            $Array += $Object 
        }
    }
}

$Array | Format-Table -Wrap -AutoSize

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.