Restart Agent with powershell task

Guys, i am stuck in another Authoring issue. Planning for a 2016 migration i have created a management pack wich have some agent/Health service tasks to add and remove management Groups.

The problem is that I want to restart the healhtservice after i have finished management Group configuration. I have seen some VB scripts to do this in a task, but i want to do it within the same task adding or removing MG’s. Until now i have tried to realoadConfiguration within the script, calling a CMD using net-stop and net-start without any luck

The script and task does work. but fails in the console since HS is stopped

<#

.SYNOPSIS
    Easially remove management group from agent

.DESCRIPTION
    Remove one connected management group from agent. Use in migration and/or test environments etc

.EXAMPLE
    Remove-SCOMManagementGroup.ps1 -MGMTGroupName SCOMMG
    Removes the specified MG from agent and restarts the health service.

.NOTES
    Author: Martin Ehrnst /Intility AS
    November 2016
	Version 1.0

#>

Param(	
   [Parameter(Mandatory=$True)]
   [string]$MGMTGroupName
)

$ErrorActionPreference='Stop'

Write-Host "This will remove $MGMTGroupName from agent. All monitoring will stop."

#Getting MG and removes if exist
try{
	#Logging event
	$api = new-object -comObject MOM.ScriptAPI
	$api.LogScriptEvent("Remove-SCOMManagementGroup.ps1", 1001, 4, "Removing agent from $MGMTGroupName Workflow initiated from OpsMgr Console")
    $Agent = New-Object -ComObject AgentConfigManager.MgmtSvcCfg
    $MG = $Agent.GetManagementGroup($MGMTGroupName)
        if ($MG){
            $Agent.RemoveManagementGroup($MGMTGroupName)}
        else{
            Write-Verbose "Could not find specified Management Group"}
}

catch{
    Write-host "An error occured please se exeption message:"
    Write-error $_.Exception.Message
    BREAK
}

Write-Host "Successfully removed $MGMTGroupName"

This is the same issue that the MS Flush Health cache task has - if the health service has stopped it is unable to report completion. The only way around it I’ve found is to restart the service “outside” of the task, by scheduling a self-deleting Task schedular task to restart the service a few seconds after the task ends, or starting a child process that sleeps before restarting the service, etc.

The task will report success, but you won’t really know if it worked until you wait and see if the agent starts/stops communicating with the management group.

As an aside, is the thinking here that you want to delegate the rollout/cleanup of agents to non-scom admins? The reason i ask is that the discovery wizard is smart enough to handle multi-homing, and when instructed to uninstall will only remove it’s current MGMT group if more than one is configured. This was how most 2007 -> 2012 migrations I’ve been involved in occurred.

1 Like

Hi,

We do not use the built in discovery wizard at all. not even opened in Our Networks so that havent cross my mind in this scenario.
As of the task it self, i figured i had to think differently, and I ended up creating a scheduled task, an i see now that i’m not the only one who ended up With that solution :slight_smile:
http://adatum.no/operationsmanager/scom-task-to-restart-agent-am-i-complicating-it