SCOM

Updating SCOM Agent Managment Group Setting via VBS

Updated 3/14/2016

  • Updated typos in script
  • Thanks to Gregory Ma for his assistance and testing.

We just finished moving (almost!) all agents out of our SCOM 2007 R2 environment into the new SCOM 2012 SP1 environment. We did this in batches over a few weeks to keep mgmt happy…and to be sure we didn’t flood SCOM 2012.

I needed a way to automate this without the requirement for powershell, so VBS was the natural choice. I scripted the move as an “add” to SCOM 2012 to be later followed by a separate “remove” from SCOM 2007. I did this in two steps to ensure I didn’t miss any agents, and because some agents were remaining in SCOM 2007 until our ACS environment moves to 2012.

The basic process is this:
1. Read in a list of servers
2. For each server, copy a vbs to c:\temp. This vbs contains the actual “work”
3. Remotely run the vbs
*My “copied” VBS file creates a SCOM object, updates the management group(s) listed, and restarts the health service.

“Core” vbs run from a central machine.  I created a separate add and remove version, but the logic is the same (only the copied/executed VBS name changes).

' Author - Nicole Welch
' Version - 2.0
' Date - 3/14/2016
' Script Name - Add.vbs
'
' Requires a file named add.txt listing each server (one per line) to be updated. This file must be in the same directory with this file, AddSCOMMgmtGrp.vbs, and psexec.
' *************************************************************************************************************************************************************************************
Option Explicit
on error resume next
'*************************************************************************************************************************************************************************************
' initialize and open text file
'
'*************************************************************************************************************************************************************************************
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim MyFile
Set MyFile = fso.OpenTextFile("Add.txt", 1, True)
wscript.echo "Opened add.txt"

'*************************************************************************************************************************************************************************************
' copy vbs and then run remotely
' *************************************************************************************************************************************************************************************

Dim servername
Dim currentpath, WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
currentpath = WshShell.CurrentDirectory
wscript.echo currentpath

Do While MyFile.AtEndOfStream <> True
servername = MyFile.ReadLine

wscript.echo servername
Set oExec = WshShell.Exec("robocopy " & currentpath & " \\" & servername & "\c$\temp AddSCOMMgmtgrp.vbs /w:1 /r:1")
wscript.echo ("robocopy " & currentpath & " \\" & servername & "\c$\temp AddSCOMMgmtgrp.vbs /w:1 /r:1")
WScript.Sleep 1000
Set oExec = WshShell.Exec("psexec \\"  & servername & " cscript c:\temp\AddSCOMMgmtgrp.vbs -n 60")
wscript.echo ("psexec \\"  & servername & " cscript c:\temp\AddSCOMMgmtgrp.vbs -n 60")
Loop

wscript.echo "Done"
MyFile.Close

Copied and locally run VBS:
Adding a mgmt group…….(AddSCOMMgmtgrp.vbs)

'Populate this file with the management group name and the name of the server you wish the agents to report to
On Error Resume Next
Dim objMSConfig
Set objMSConfig = CreateObject("AgentConfigManager.MgmtSvcCfg")
Call objMSConfig.AddManagementGroup ("[your mgmt group]", "[your mgmt server]",5723)
Set WshShell = CreateObject"WScript.Shell")
Set oExec = WshShell.Exec("net stop HealthService")
WScript.Sleep 10000
Set oExec = WshShell.Exec("net start HealthService")

To remove mgmt groups (RemoveSCOMMgmtgrp.vbs):

'On error resume next is required otherwise if you try to remove a mgmt that doesn't exist, the script will error out
On Error Resume Next
Dim objMSConfig

Set objMSConfig = CreateObject("AgentConfigManager.MgmtSvcCfg")
Call objMSConfig.RemoveManagementGroup ("[mgmt group 1]")
Call objMSConfig.RemoveManagementGroup ("[mgmt group 2]")
Call objMSConfig.RemoveManagementGroup ("[mgmt group 3]")

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("net stop HealthService")
WScript.Sleep 10000
 Set oExec = WshShell.Exec("net start HealthService")
Advertisements

One thought on “Updating SCOM Agent Managment Group Setting via VBS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s