How to Delete SCOM 2012 R2 Managed Computers using PowerShell

Usage

DeleteSCOMAgents.ps1 -MSServer "zOM01.scom2k12.com" -AgentComputerName "xSP01.scom2k12.com", "xDV02.scom2k12.com"

deleteAgents

Download Script: Link

Script:

Param(
  [string[]]$AgentComputerName,
  [string]$MSServer
)

[System.Reflection.Assembly]::Load("Microsoft.EnterpriseManagement.Core, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
[System.Reflection.Assembly]::Load("Microsoft.EnterpriseManagement.OperationsManager, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")



function New-Collection ( [type] $type ) 
{
	$typeAssemblyName = $type.AssemblyQualifiedName;
	$collection = new-object "System.Collections.ObjectModel.Collection``1[[$typeAssemblyName]]";
	return ,($collection);
}




# Connect to management group
Write-output "Connecting to management group"

$ConnectionSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MSServer)
$admin = $ConnectionSetting.GetAdministration()


Write-output "Getting agent managed computers"
$agentManagedComputers = $admin.GetAllAgentManagedComputers()

# Get list of agents to delete
foreach ($name in $AgentComputerName) 
{
    Write-output "Checking for $name"
    foreach ($agent in $agentManagedComputers)
    {
        if ($deleteCollection -eq $null) 
        {
            $deleteCollection = new-collection $agent.GetType()
        }

        
        if (@($agent.PrincipalName -eq $name))
        {
	    Write-output "Matched $name"
            $deleteCollection.Add($agent)
            break
        }
    }
}

if ($deleteCollection.Count -gt 0) 
{
    Write-output "Deleting agents"
    $admin.DeleteAgentManagedComputers($deleteCollection)
    if($?){ Write-output "Agents deleted" }
}

 

 

5 Responses to How to Delete SCOM 2012 R2 Managed Computers using PowerShell

  1. Tommy May 27, 2016 at 2:38 am #

    Hi Tim,
    I assume this is for deleting systems that are already taken offline so the agent couldn’t’ be uninstalled in the first place?
    Regards
    Tommy

    • Tim McFadden May 27, 2016 at 11:14 am #

      Yes. For servers that are decommissioned.

  2. Kamal Sharma January 11, 2022 at 5:59 am #

    How to uninstall Agent software from Agent before remove ?
    Is It support For Bulk Agent Remove ?

Trackbacks/Pingbacks

  1. System Center Haziran 2016 Bülten – Sertaç Topal - June 18, 2016

    […] How to Delete SCOM 2012 R2 Managed Computers using PowerShell […]

  2. How to Delete SCOM Agents using PowerShell – Site Title - February 15, 2018

    […] https://www.scom2k7.com//how-to-delete-scom-2012-r2-managed-computers-using-powershell/ […]

Leave a Reply