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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<# .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.
Answer this question
To reply or comment, use the 'Comment' link on the relevant answer or question.