PoSh Remove previously managed SCOM agent

A while back a customer approached me if perhaps I knew a way to remove an agent entry from SCOM when the underlying server doesn’t exist anymore.

Of course they could simply delete the agent entry from within the SCOM console manually, but the customer really wanted to include it in their automated decommissioning procedure which includes things like removing the server record from SCCM, removing the DHCP reservation and DNS records. For all these steps they had been able to use PowerShell effectively and expected the

 

Uninstall-SCOMAgent cmdlet would provide the needed functionality but they were surprised it didn’t.

The Internet did not reveal working solutions for SCOM 2012 R2 on the first search, so I took it upon myself to investigate and write a script for it. An old post “Deleting Agents” by Roger Sprague talks about the Uninstall-SCOMAgent cmdlet to uninstall agents from agent-managed computers which cannot be used for uninstalling an agent if the computer the agent is hosted by is not available. The article provides alternative PowerShell code to remove agent registrations from SCOM 2007 when the hosting server no longer exists. This code uses .NET but does not work on SCOM 2012 R2 and is hard to understand and modify for the novice PowerShell user.

The System Center Operations Manager Engineering Blog featured the post “How to Delete an Operations Manager 2007 Managed Object from a Management Group using PowerShell (Updated PS script which works on PowerShell V2 and above)” which gave me even more insight into using the SDK binaries for SCOM, but the code wasn’t working straight of the bat.

Other blog posts went into removing agent records using SQL code to solve the issue such as “Delete Decommissioned Servers From SQL That Still Show In The SCOM Console” leaving me disappointed. The Network Steve Forum thread “How to delete Dead Agent Managed Computer from SCOM with PowerShell” showed: “There’s no cmdlet to delete a decommissioned agent with PowerShell, although you could write your own cmdlet as it is all available in the .net code.”

Using the .NET code fragments I started working on a script but first I checked the  location for the SCOM 2012 SDK binaries and loaded them. Then I checked the MSDN library for some background information on:

The following image shows the relationship between the MSDN .NET information and the script:

scom-agent-remove-powershell-dotnet

And here the final code I came up with:

This script connects to the management server specified in the script (using the credentials of the currently logged on user), where it loads the SDK binaries available on the SCOM management server. The action is performed remotely.

Before posting my solution I searched the internet and was pleased to find another similar script SCOM 2012 R2 – Delete Agent using PowerShell by David Allen over at aquilaweb.net you should check out.

Let’s hope System Center Operations Manager 2016 ships with a “Remove-SCOMAgent” cmdlet or a -force option on the “Uninstall-SCOMAgent” cmdlet that does the job for us.

This is an old post that was originally posted on it-automators.nl (which no longer exists) and has been backdated to reflect the date it was originally posted.

Share this:

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.