Deleting Agents
A few of you sent mail asking how to delete an agent using the Command Shell. There is a Cmdlet for uninstalling an agent called Uninstall-Agent. However, uninstalling an agent will not work if the computer the agent is hosted by is not available. To help address the issue I created a function to automate the process. Keep in mind this function should only be used to delete an agent hosted by a computer that no longer exists otherwise you should use Uninstall-Agent.
# Summary
# Delete an agent hosted by a non-existent computer.
# Params
# AgentNames - An array of strings that contain the FQDN of agents to delete.
# Returns
# None
function global:Delete-Agent([System.String[]] $agentNames)
{
$NoAgentsErrorMsg = "`nNo agent names specified. Please specify the FQDN for each agent you want to delete.`n";if ($agentNames -eq $null)
{
Write-Host $NoAgentsErrorMsg;
return;
}$administration = (get-item .).ManagementGroup.GetAdministration();
$agentManagedComputerType = [Microsoft.EnterpriseManagement.Administration.AgentManagedComputer];
$genericListType = [System.Collections.Generic.List``1]
$genericList = $genericListType.MakeGenericType($agentManagedComputerType)$agentList = new-object $genericList.FullName
foreach ($agentName in $agentNames)
{
$agent = Get-Agent | where {$_.PrincipalName -eq $agentName}
if ($agent -eq $null)
{
$msg = "Agent '{0}' not found." -f $agentName;
Write-Host $msg;}
else
{
$agentList.Add($agent);
}
}if ($agentList.Count -eq 0)
{
Write-Host $NoAgentsErrorMsg;
return;
}$genericReadOnlyCollectionType = [System.Collections.ObjectModel.ReadOnlyCollection``1]
$genericReadOnlyCollection = $genericReadOnlyCollectionType.MakeGenericType($agentManagedComputerType)$agentReadOnlyCollection = new-object $genericReadOnlyCollection.FullName @(,$agentList);
$msg = "`nDeleting {0} agents:`n" -f $agentReadOnlyCollection.Count;
Write-Host $msg;
foreach ($agent in $agentReadOnlyCollection)
{
Write-Host $agent.PrincipalName;
}
$administration.DeleteAgentManagedComputers($agentReadOnlyCollection);
}
Hope that helps and as always please post your questions and comments and I will make sure they are addressed in future posts.
Roger Sprague
Comments
Anonymous
February 05, 2008
I am not good at using the command shell. What kind of changes do i have to make to run this script? Please help. Thanks. This is a great help.Anonymous
August 20, 2008
Hello, Is there a way to use the administration.DeleteAgentManagedComputer without first converting your list of computers into a genericlist? I checked out all my options with the built in cmdlets for the operations manager 2007 console and there is no, Delete-Agent, which i find kind of strange as they have the Uninstall_Agent command let like you said. If there is no other way than i guess that is what i should go with. Thank you,Anonymous
August 20, 2008
I think i have found my answer, the other cmdlet that i think might work is Remove-RemotelyManagedComputer -Computer (Enter Computer Name Here) You can add more computer names by putting a comma between each of the computer names you add to your list.Anonymous
January 08, 2010
The comment has been removedAnonymous
March 21, 2012
Hi team, Is there a script for SCOM 2007 that will remove orphaned object of computer. We had a lot of these. Agent was not unintsalled and was just deleted from the console but we still see these computers in gray in Windows computers view.Anonymous
December 10, 2013
Will this work with SCOM 2012? I amam desperately in need of this functionality.