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/

8 Responses to “Using PowerShell to add update and delete Class instances in SCOM”

  1. Haimiko September 10, 2021 at 21:56 #

    Deleted class instance now rediscovering never adds it back again.

    • haimiko September 10, 2021 at 22:22 #

      Never mind. I must have broken my discovery script in my management pack

  2. Krishna November 3, 2022 at 13:56 #

    How to add windows instance to scom, instead of VMware

    • Michel Kamp November 3, 2022 at 18:51 #

      Hi, you can add any class as long you supply the correct class name , key properties and relations .

      • Krishna November 4, 2022 at 09:15 #

        Hi how can we check key properties and relations for windows class, is their any command to check

      • Krishna Reddy November 8, 2022 at 11:21 #

        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

      • michel kamp November 8, 2022 at 18:44 #

        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

Trackbacks/Pingbacks

  1. System Center Aralık 2018 Bülten – Sertaç Topal - December 23, 2018

    […] Using PowerShell to add update and delete Class instances in SCOM […]

Leave a comment