quinta-feira, 12 de julho de 2012

Adding a SCOM Agent Task to Update the Management Group


In a migration from SCOM 2007 R2 to SCOM 2012, I came into to the situation of needing to update the management groups to which the agents report to. I didn’t want to manually type the info more than 50 times for this customer, so, I spent some time digging and found all the pieces I needed to add the management groups from the Console, using an agent task.
The only challenge was that you can't use VBS itself to stop and start the services. Since the script runs in the context of the Health Agent, which is killed along with the services, it would stop, but would never come back. That's why the script has to create a local batch (which should be removed, I know...), and then starts it.


In the SCOM Console, go to the Authoring section, expand Management Pack Objects, right-click on Tasks and select Create a New Task:

In the next dialog, select run a Script and select or create a management pack to store the Task. If you’re using SCOM 2007, make sure you don’t select the Default Management Pack:

Name the task, fill in a good description and pick Health Service as target for the Task:

Click next and fill the information below as follows:

See the script below.
Click Finish.

Now you should see the Task available in the Tasks pane whenever you’re in the context of a Health Agent, as in the example below:

The script can be improved, to add some more error handling, for example, to give more feedback. I'll try to do that and update it here.
So, here goes the script:

On error resume next
Dim objMSConfig
Set objMSConfig = CreateObject("AgentConfigManager.MgmtSvcCfg")
wscript.echo "Adding NEWMG MG to the agent. Reporting to myserver.mydomain.local on port 5723"
Call objMSConfig.AddManagementGroup ("NEWMG", "myserver.mydomain.local",5723)
if err.number = 0 then
                dim strServiceName, objWMIService, colListofServices, objService
                strServiceName = "HealthService"
                wscript.echo "Successfully added Management Group."
                dim sFilePath
                Dim objFSO 'As FileSystemObject
                Dim objTextFile 'As Object
                Dim oShell
                Set oShell = CreateObject( "WScript.Shell" )
                'wscript.echo "Temp Path" & temppath
                wscript.echo "Restarting Agent."
                sText="net stop """ & strServiceName & """" & vbcrlf &  "net start """ & strServiceName & """"
                sFilePath=temppath & "\sshs.bat"
                'wscript.echo "Writing to " & sFilePath
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objTextFile = objFSO.CreateTextFile(sFilePath, True)
                ' Write a line.
                objTextFile.Write (sText)
                oShell.run sFilePath
                wscript.echo "Error adding Management group - " & err.Description
end if

Changing the script to remove the MG:
Call objMSConfig.RemoveManagementGroup ("MyManagementGroupToRemove”)

Please refer to this link for a complete documentation:

Hope it helps and happy SCOMming!