SCOM – Remotely update Management Group configuration on SCOM agent via PowerShell


One of my customers installed the agent via SCCM and it had a typo in the management group name. This script removes the current management group configuration the agent is assigned to, and adds a new management group configuration with the correct management group name.

$OLDMG = "oldMG"
$newMG = "newMG1"
$MS = "serverOM"
$Agentlist = "computer1",”computer2”,”computer3”
$Agentlist | % {Invoke-command -computername $_ -ArgumentList $oldMG,$newMG,$MS -scriptBlock {
param($oldMG,$newMG, $MS)
try {$o = new-object -com AgentConfigManager.MgmtSvcCfg
#remove old MG name and disable AD integration
try {
$oMG= @($o.getmanagementgroup($oldMG))
if ($oMG.count -ne $null)
{
$o.RemoveManagementGroup($oldMG)
$o.DisableActiveDirectoryIntegration()
$o.reloadconfiguration()
write-host "Removed $oldMG from agent configuration" -ForegroundColor Green
$o=new-object -com AgentConfigManager.MgmtSvcCfg
$o.getmanagementgroups()
}
}
catch {write-host "MG $oldMG not found" -ForegroundColor Magenta}
#add new MG Name
try {$nMG= @($o.getmanagementgroup($newMG))
Write-Host "agent already configured with $newMG" -ForegroundColor Magenta}
catch {
write-host "MG $newMG not found. Adding the new MG to agent's configuration" -ForegroundColor Green
$o.AddManagementGroup($newMG,$MS,5723)
$o.reloadconfiguration()
write-host "$newMG MG has been added to agent" -ForegroundColor Green
#clear the healthstore cache
get-service -Name HealthService | Stop-Service
Remove-Item "C:\Program Files\Microsoft Monitoring Agent\Agent\Health Service State\Health Service Store" -re -Force
get-service -Name HealthService | Start-Service
}
}
catch {write-host "Microsoft Monitoring Agent not installed"}
}
}

I'm always after this script, and I seem to always have re-write it. Maybe next time I need it I'll remember it's here


Comments (0)

Skip to main content