Challenge
This short post will provide some PowerShell scripting to let you play with class instances in SCOM. Since there’s not much information on the web on this topic and I had a request from a partner I will provide some code below how to handle this.
In the examples below I will use a class from the OpsLogix VMware management pack, but you can use any (public) class you want. All code just be run on a SCOM MS server. All examples are separate runnable.
Add a class instance
In the example below, I will add a new VMWare ESX host to a datacenter belonging to a vCenter connection. As below:
Before we run the script, we will have to know the Key properties so we can configure the parent child relationship. In this case the we have the following relationship:
vCenter01:7443 -> Datacenter01 -> My_ESX_Host
In this case the Host we want to add is called “My_ESX_Host” and we set the “AssetTag” to “unkown” .
## ====================================================================== ## example script how to add a class instance in SCOM ## ====================================================================== ## Michel Kamp ## ======================================================================
New-SCOMManagementGroupConnection $mg $ClassInstanceDisplayName= $Class
##================= create/add class instance $ClassObject # set the KEY props $ClassObject[$Class.FindHostClass().FindHostClass(),“vCenterHostName”].Value = “vCenter01:7443” $ClassObject[$Class.FindHostClass(),“DatacenterName”].Value = “Datacenter01” $ClassObject[$Class,“ESXServerName”].Value = # Set NON key props $ClassObject[$Class,“AssetTag”].Value = “unkown” # add and write back to scom $discovery $discovery.Add($ClassObject) $discovery.Overwrite($mg)
##================= END ================================================ |
Change a property of a class instance
Below we change a property called “AssetTag” to “test tag 123” of the ESXHost class instance “My_ESX_Host”. Keep in mind that only NON-Key properties can be changed.
See script below:
## ====================================================================== ## example script how to change a class instance in SCOM ## ====================================================================== ## Michel Kamp ## ======================================================================
New-SCOMManagementGroupConnection $mg $ClassInstanceDisplayName= $Class
##================= change property of class instance ## get the just created class $ClassInstance # change the properties # only NON-KEY props can be changed $ClassInstance[$Class,“AssetTag”].Value = “test tag 123” # write it to scom $ClassInstance.Overwrite()
##================= END ================================================ |
Delete a Class Instance
In this example we delete the ESXHost class instance “My_ESX_Host”.
## ====================================================================== ## example script how to delete a class instance in SCOM ## ====================================================================== ## Michel Kamp ## ======================================================================
New-SCOMManagementGroupConnection $mg $ClassInstanceDisplayName= $Class
##============== remove a class instance ## get the class instance to remove $ClassInstance # add the class instance to delete $discovery $discovery.RemoveInternal($ClassInstance,$ClassInstance.GetClasses()[0]) ## execute the delete $discovery.Commit($mg)
##================= END ================================================ |
Happy SCOM’ing
Michel Kamp
TOUCHING SCOM
One Response to “Using PowerShell to add update and delete Class instances in SCOM”