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"
