Using PowerShell to add update and delete Class instances in SCOM

29 Nov

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
-ComputerName
“localhost”

$mg
=
Get-SCOMManagementGroup

$ClassInstanceDisplayName=
“My_ESX_Host”

$Class
=
Get-SCOMClass
-Name
OpsLogix.IMP.VMWare.ESXHost

##================= create/add class instance

$ClassObject
=
New-Object
Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($mg,$class)

# set the KEY props

$ClassObject[$Class.FindHostClass().FindHostClass(),“vCenterHostName”].Value = “vCenter01:7443”

$ClassObject[$Class.FindHostClass(),“DatacenterName”].Value = “Datacenter01”

$ClassObject[$Class,“ESXServerName”].Value =
$ClassInstanceDisplayName

# Set NON key props

$ClassObject[$Class,“AssetTag”].Value = “unkown”

# add and write back to scom

$discovery
=
New-Object
Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData

$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
-ComputerName
“localhost”

$mg
=
Get-SCOMManagementGroup

$ClassInstanceDisplayName=
“My_ESX_Host”

$Class
=
Get-SCOMClass
-Name
OpsLogix.IMP.VMWare.ESXHost

##================= change property of class instance

## get the just created class

$ClassInstance
=
Get-SCOMMonitoringObject
-Class
$Class
|
where { $_.DisplayName -eq $ClassInstanceDisplayName}

# 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
-ComputerName
“localhost”

$mg
=
Get-SCOMManagementGroup

$ClassInstanceDisplayName=
“My_ESX_Host”

$Class
=
Get-SCOMClass
-Name
OpsLogix.IMP.VMWare.ESXHost

##============== remove a class instance

## get the class instance to remove

$ClassInstance
=
Get-SCOMMonitoringObject
-Class
$Class
|
where { $_.DisplayName -eq $ClassInstanceDisplayName}

# add the class instance to delete

$discovery
=
New-Object
Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData

$discovery.RemoveInternal($ClassInstance,$ClassInstance.GetClasses()[0])

## execute the delete

$discovery.Commit($mg)

##================= END ================================================

Happy SCOM’ing

Michel Kamp

TOUCHING SCOM

https://michelkamp.wordpress.com/

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: