Management Group - REMOVE

Agent.Management.RemoveManagementGroup.Task (Task)

Element properties:

TargetAgent.Management.Class
AccessibilityPublic
CategoryCustom
EnabledTrue
RemotableFalse
Timeout120

Member Modules:

ID Module Type TypeId RunAs 
PA WriteAction Microsoft.Windows.ScriptWriteAction Default

Source Code:

<Task ID="Agent.Management.RemoveManagementGroup.Task" Accessibility="Public" Enabled="true" Target="Agent.Management.Class" Timeout="120" Remotable="true">
<Category>Custom</Category>
<WriteAction ID="PA" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>Agent.Management.RemoveManagementGroup.Task.vbs</ScriptName>
<Arguments>MGName</Arguments>
<ScriptBody><Script>
Option Explicit
On Error Resume Next
Dim oArgs, MGName, objMSConfig, ScriptName, oAPI, objWMIService, objProcess, strCommand, objProgram, oShell, oShellEnv, strShell, computerName
Set oArgs=Wscript.Arguments
MGName=oArgs(0)

'Create COM object to get MGs
Set objMSConfig = CreateObject("AgentConfigManager.MgmtSvcCfg")

ScriptName = "Agent.Management.RemoveManagementGroup.Task.vbs"

'Load momscript API
Set oAPI = CreateObject("MOM.ScriptAPI")
'Log script event that we are starting
Call oAPI.LogScriptEvent(ScriptName, 1310, 2, "A command to remove a management group from this agent is being issued. Management Group name being removed is (" &amp; MGName &amp; ")")

'Remove management group
Call objMSConfig.RemoveManagementGroup(MGName)
If Err.number &lt;&gt; 0 Then
wscript.echo ("Failed to remove " + MGName)
Else
wscript.echo ("Management Group " + MGName + " has been removed")

'Begin Healthservice Restart
Set oShell = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
computerName = oShellEnv("ComputerName")
'Echo that we are about to start for task output
WScript.echo "Beginning Restart attempt for HealthService on " &amp; computerName
strCommand = "cmd /c net stop HealthService &amp; cmd /c net start HealthService"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")
Set objProgram = objProcess.Methods_( _
"Create").InParameters.SpawnInstance_
objProgram.CommandLine = strCommand
Set strShell = objWMIService.ExecMethod( _
"Win32_Process", "Create", objProgram)
'Echo that we are restarting for task output
WScript.echo "Restarting HealthService on " &amp; computerName
'End Healthservice Restart

End If
</Script></ScriptBody>
<TimeoutSeconds>60</TimeoutSeconds>
</WriteAction>
</Task>