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
Deleted class instance now rediscovering never adds it back again.
Never mind. I must have broken my discovery script in my management pack
How to add windows instance to scom, instead of VMware
Hi, you can add any class as long you supply the correct class name , key properties and relations .
Hi how can we check key properties and relations for windows class, is their any command to check
Hi I’m new to the power shell please help me in this. Here I want to add windows instance to the scom monitoring. here I’m using “Microsoft.windows.operatingsystem”(class) but I’m unable to add windows instance. it is throwing error, property can’t be found on this object
Hi ,
Totally unsupported, but it works.
Below will add an windows computer
New-SCOMManagementGroupConnection -ComputerName “<>”
$mg = Get-SCOMManagementGroup
$ClassInstanceDisplayName = “MyComputer”
$Class = Get-SCOMClass -Name Microsoft.Windows.Computer
#================= create/add class instance
$ClassObject = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($mg,$class)
list all properties that can be assigned , if key = true you will have to set the unique value.
# $Class.PropertyCollection | ft name,key
set the KEY props
#$ClassObject[$Class.FindHostClass(),”PrincipalName”].Value = “ClientServer01.contoso.com”
$ClassObject[$Class,”PrincipalName”].Value = $ClassInstanceDisplayName
Set NON key props
$ClassObject[$Class,”DNSName”].Value = $ClassInstanceDisplayName
$ClassObject[$Class,”DisplayName”].Value = $ClassInstanceDisplayName
add and write back to scom
$discovery = New-Object Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData
$discovery.Add($ClassObject)
$discovery.Overwrite($mg)
Check creation
Get-SCOMClassInstance -Class $Class | select FullName , HealthState | sort -Property FullName
//
Michel