SCOM 2012 Powershell command to remove agent

Hello,

Has anyone had any luck removing agents via Powershell to mimic the delete agent from the console?

Blogs don't turn up anything useful for this task.

The 2012 Powershell covers how to approve agents, add proxy flag, and uninstall agent, but nothing to remove.  Used the SQL stored procedure previously in 2007R2 (and same procedure exists in 2012 SP1. 

Anyone aware of Powershell automate agent removal?

Thanks!

SCOM Power Shell to add SCOM 2012 agent
Get-SCOMPendingManagement | where {$_.AgentName -eq "ServernameFQDN"} | Approve-SCOMPendingManagement
Enable Proxy on newly added agent
Get-ScomAgent | where-object {$_.DisplayName -like "okcnmo*"} | where-object {$_.ProxyingEnabled.Value -eq $False} | Enable-SCOMAgentProxy

SQL Query to remove
http://social.technet.microsoft.com/Forums/en-US/operationsmanagergeneral/thread/e413c030-509b-45e4-bcd4-b4ae1a649c73
Uninstall Agent
http://technet.microsoft.com/library/hh545186.aspx
Remove Unix agents
http://social.technet.microsoft.com/Forums/en-US/operationsmanagerunixandlinux/thread/081f2801-7dfa-4db9-8c78-6adbc487a031/

June 13th, 2013 10:06pm

Removing orphaned Operations Management, pls. refer to the following post
http://blogs.technet.com/b/momteam/archive/2012/02/21/removing-orphaned-operations-management-agents-using-powershell.aspx
http://support.microsoft.com/kb/2626752
Roger
Free Windows Admin Tool Kit Click here and download it now
June 14th, 2013 4:24am

Hi,

You can use Uninstall-SCOMAgent command to remove scom agent.

refer below URL for more information

http://technet.microsoft.com/library/hh545186.aspx

for delete refer below link

http://blogs.msdn.com/b/scshell/archive/2007/04/25/deleting-agents.aspx

Regards

June 14th, 2013 4:27am


Hi,

Please see if the following method helps:

QuickTricks: How to bulk uninstall agents in OpsMgr 2012 by name in PowerShell (#SCOM, #PowerShell, #SYSCTR)
http://blogs.catapultsystems.com/cfuller/archive/2012/06/21/quicktricks-how-to-bulk-uninstall-agents-in-opsmgr-2012-by-name-in-powershell-scom-powershell-sysctr.aspx

Thanks.

Free Windows Admin Tool Kit Click here and download it now
June 14th, 2013 6:52am

I'm not really sure of what you're trying to achieve, but uninstalling an agent using powershell is rather easy and well documented :

Get-SCOMAgent "dnshostname.ofagent.com" | Uninstall-SCOMAgent -ActionAccount (Get-Credential)

June 14th, 2013 9:51am

Gents,
Thank you Sridhar, but the 2007 Powershell commandlet isn't available in 2012 (Delete-Agent).
Thank you CyrAz and Nicholas for Uninstall syntax, already aware.

Let me try again, removing an agent from SCOM console is NOT uninstalling an agent.

Reproducing via PowerShell to delete agent from SCOM console:
Click on Administration Tab, Agent Managed, and right clicking Agent, choosing Delete (removes the agent from SCOM)

As stated in the delete agent blog Sridhar quoted, Uninstall agent doesn't cover all scenarios.

  • Agent/server is down
  • SCOM Administrator does NOT have admin on removed machine
  • SCOM Local System Action/Windows Account does not work, only administrator level access specified in PowerShell works

Nothing I have come across nor the responses indicate how to delete an agent via Powershell without going to SQL backend.

NOTE: SCOM console calls stored procedure to remove the agent.

Anyone aware how in PowerShell to mimic SCOM Console agent delete when Uninstall-Agent fails based on scenarios above without going to backend SQL script?

Thanks

Free Windows Admin Tool Kit Click here and download it now
June 14th, 2013 4:08pm

One thing, if you do have the scom snapping from 2007r2 you can still import it and use the old cmdlets.  I'm trying to figure out a way to do it using the new cmdlets and I'll get back to you when I find something.  That's a pretty big missing cmdlet, I'm glad you brought it up. 
June 16th, 2013 6:14pm

Hi,

I got this fixed today. I modified Roger Sprague's script(http://blogs.msdn.com/b/scshell/archive/2007/04/25/deleting-agents.aspx) to a 2012 usable version:

Import-Module OperationsManager             New-SCOMManagementGroupConnection -ComputerName %SCOM_MGMT Server% $Servername = "%AgentName%"

$administration = (Get-SCOMManagementGroup).GetAdministration(); $agentManagedComputerType = [Microsoft.EnterpriseManagement.Administration.AgentManagedComputer]; $genericListType = [System.Collections.Generic.List``1] $genericList = $genericListType.MakeGenericType($agentManagedComputerType) $agentList = new-object $genericList.FullName

$agent = Get-SCOMAgent *$Servername*

$agentList.Add($agent); $genericReadOnlyCollectionType = [System.Collections.ObjectModel.ReadOnlyCollection``1] $genericReadOnlyCollection = $genericReadOnlyCollectionType.MakeGenericType($agentManagedComputerType) $agentReadOnlyCollection = new-object $genericReadOnlyCollection.FullName @(,$agentList);     $administration.DeleteAgentManagedComputers($agentReadOnlyCollection);

Best Regards

Thomas



  • Edited by Thomas Munk Monday, December 23, 2013 12:26 PM
  • Proposed as answer by (David Arnold) Thursday, January 02, 2014 3:41 PM
Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2013 12:25pm

Thomas,

Thank you very much for posting this!

January 2nd, 2014 3:41pm

We actually have used the stored procedure for 2007 and 2012

OperationsManager.dbo.p_TypedManagedEntityDelete

If you want to use Invoke-Sqlcmd to run the query.

Another way from SQL which can be put in PowerShell

 

--Grab GUID for that health service

select * from BaseManagedEntity

where FullName like '%<servername>%'

and IsDeleted = 1

 

Look for FullName with Microsoft.SystemCenter.HealthService:servername

-- Use this to test the entity delete for the healthservice GUID

 

DECLARE @EntityId uniqueidentifier;

DECLARE @TimeGenerated datetime;

 

-- change "GUID" to the ID of the invalid entity

SET @EntityId = '<GUID here>';

SET @TimeGenerated = getutcdate();

 

BEGIN TRANSACTION

 

EXEC dbo.p_TypedManagedEntityDelete @EntityId, @TimeGenerated;

 

COMMIT TRANSACTION

 

--Check what is in deleted

select * from BaseManagedEntity

where IsDeleted = 1

Free Windows Admin Tool Kit Click here and download it now
February 27th, 2014 4:49pm

Thomas,

TYVM for the code above, wanted to let other folks know it works on SCOM 2012 exactly as described, and gladly not have to mess with SCOM db directly.

tip: if you tried to script delete agents that are NOT orphaned, you end up with ghost agent entries that do not work, and you cant delete via the gui/script, they will go away for about a min and come back on the admin console.  Have not found a solution to this yet, but more than happy to not have to do agent cleanup via gui one by one anymore.

Jack


  • Edited by jjunky Tuesday, May 20, 2014 10:38 PM
May 20th, 2014 10:37pm

Thomas,

Been combing the web for days looking for exactly this.

Thanks a bunch

+10pts to you!

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2014 5:38pm

Hello

What's this (''1)? It's not working for me.

$genericReadOnlyCollectionType = [System.Collections.ObjectModel.ReadOnlyCollection``1]

June 1st, 2015 3:25pm

Max,

I am not sure if Thomas will respond to your question about his code.

The stored procedure worked pretty good, but missed some objects like clustered servers and all their virtual aliases.


Try this...

############################################################################
# Script:  ScomAgentDeleteSDK.ps1 
# this script will delete agents using the SDK binaries and .NET based SDK commands
# Takes a single parameter of a computer FQDN
############################################################################

############################################################################
# Function Get-SDKServername is only needed if you want to make 
# this script portable to any machine that has the SCOM Console installed
# You can choose to hard code this or use the function
############################################################################
function Get-SDKServerName
{
	# Get SDK server name
	# Lifted from Microsoft.EnterpriseManagement.OperationsManager.CleintShell.Functions.ps1
	$UserRegKeyPath = "HKCU:\software\Microsoft\Microsoft Operations Manager\3.0\User Settings";
	$MachineRegKeyPath = "HKLM:\software\Microsoft\Microsoft Operations Manager\3.0\Machine Settings";
	$UserRegValueName = "SDKServiceMachine";
	$MachineRegValueName = "DefaultSDKServiceMachine";
	$regValue = $null;
	$server = $null;

	$regValue = Get-ItemProperty -path:$UserRegKeyPath -name:$UserRegValueName -ErrorAction:SilentlyContinue;

	if ($regValue -ne $null)
	{
	    $server = $regValue.SDKServiceMachine;
	}

	if ($server -eq $null -or $server.Length -eq 0)
	{
	    $regValue = Get-ItemProperty -path:$MachineRegKeyPath -name:$MachineRegValueName -ErrorAction:SilentlyContinue;
     
	    if ($regValue -ne $null)
	    {
	        $server = $regValue.DefaultSDKServiceMachine;
	    }
	}

	if ($server -eq $null) 
	{
	    $server = "localhost"
	}

	return $server
}

############################################################################
# Function Get-InstallDirectory is used to find the installation path  
# of SCOM in order to located the SDK binaries which need to be loaded
############################################################################
function Get-InstallDirectory
{
	# Get Install directory
	$SetupRegKeyPath = "HKLM:\software\Microsoft\Microsoft Operations Manager\3.0\Setup";
	$InstallDirRegValueName = "InstallDirectory";

	$InstallDir = "$env:SystemDrive\Program Files\System Center Operations Manager 3.0";

	$regvalue = $null;
	$regValue = Get-ItemProperty -path:$SetupRegKeyPath -name:$InstallDirRegValueName -ErrorAction:SilentlyContinue;
	if ($regvalue -ne $null) 
	{
	    $InstallDir = $regValue.InstallDirectory;
	}

	return $InstallDir
}


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

############################################################################
# Log output file name per server when script called
# Uncomment this for logging purposes
############################################################################


# Load SDK dlls
$server = Get-SDKServerName
$InstallDir = Get-InstallDirectory
$sdkDir = "$InstallDir\SDK Binaries"
echo "Loading SDK"
$dummy = [System.Reflection.Assembly]::LoadFrom("$sdkDir\Microsoft.EnterpriseManagement.Core.dll")
$dummy = [System.Reflection.Assembly]::LoadFrom("$sdkDir\Microsoft.EnterpriseManagement.OperationsManager.dll")
$dummy = [System.Reflection.Assembly]::LoadFrom("$sdkDir\Microsoft.EnterpriseManagement.Runtime.dll")

# Connect to management group
echo "Connecting to management group"
$managementGroup = [Microsoft.EnterpriseManagement.ManagementGroup]::Connect($server)
$admin = $managementGroup.GetAdministration()

echo "Getting agent managed computers"
$agentManagedComputers = $admin.GetAllAgentManagedComputers()

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

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

if ($deleteCollection.Count -gt 0) 
{
    echo "Deleting agents"
    $admin.DeleteAgentManagedComputers($deleteCollection)
    if($?){ echo "Agents deleted" }
}
else
{
    echo "No Match for $name"
    Write-Host -ForegroundColor Yellow "NO agent $name found, manual intervention required"
}

Free Windows Admin Tool Kit Click here and download it now
June 1st, 2015 4:54pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics