KB: Scripting the Ping MP with PowerShell

When dealing with large amounts of ping targets it is convenient to be able to add/remove them through PowerShell. This kb article contains a sample script for adding, removing targets and hosts with PowerShell.

 

Use below script to add or edit ping targets. Make sure to change the parameters for your intended purpose. All the examples should be run on a SCOM management server.

Create/Add/Edit a new Ping target

Tip: If you specify an existing target IP address you are able to change the display name of that target.

## ======================================================================
## How to add Ping targets in SCOM using Powershell
## ======================================================================
## Michel Kamp / Elias Fröjd
## ======================================================================
## Run this script on a MS server
## ======================================================================

Import-Module OperationsManager

## login to the MS group
New-SCOMManagementGroupConnection -ComputerName "localhost"
$mg = Get-SCOMManagementGroup

## ===========================================================
## == create/add/edit a new Ping target
## ===========================================================

## Specify the ping source Source name:
$PingSourceName = "DEVOM2012SP1-01.contoso.com"

## Specify the ping target IP address and display name:
## To update/edit an existing target display name specify the target IP and type in the wanted name
$TargetIPAdress = "172.16.10.1"
$TargetDisplayName = "Gateway"

## ===========================================================
## Do NOT change below
## ===========================================================

## create discovery handler
$SourceClass = Get-SCOMClass -Name OpsLogix.IMP.Ping.Host
$TargetClass = Get-SCOMCLass -Name OpsLogix.IMP.Ping.Target
$WindowsComputerClass = Get-SCOMCLass -Name Microsoft.Windows.Computer

$discovery = New-Object Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData
$ConnectionSourceClassObject = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($mg,$SourceClass)
$ConnectionTargetClassObject = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($mg,$TargetClass)

# set the source KEY props
$ConnectionSourceClassObject[$SourceClass.FindHostClass(),"PrincipalName"].Value = $PingSourceName
$ConnectionSourceClassObject[$SourceClass,"HostName"].Value = $PingSourceName
# set the target KEY props
$ConnectionTargetClassObject[$TargetClass.FindHostClass().FindHostClass(),"PrincipalName"].Value = $PingSourceName
$ConnectionTargetClassObject[$TargetClass.FindHostClass(),"HostName"].Value = $PingSourceName
$ConnectionTargetClassObject[$TargetClass,"TargetHost"].Value = $TargetIPAdress
$ConnectionTargetClassObject[$TargetClass,"DisplayName"].Value = $TargetDisplayName


# finally add the target:
$discovery.Add($ConnectionSourceClassObject)
$discovery.Add($ConnectionTargetClassObject)
$discovery.Overwrite($MG)


## ======================================================================
## End of script
## ======================================================================

Remove a ping target

To remove ping targets use below script:

## ======================================================================
## Example script how to remove a ping target in SCOM using Powershell
## ======================================================================
## Michel Kamp / Elias Fröjd
## ======================================================================
## Run this script on a MS server
## ======================================================================

Import-Module OperationsManager

## login to the MS group
New-SCOMManagementGroupConnection -ComputerName "localhost"
$mg = Get-SCOMManagementGroup

## ===========================================================
## == remove a ping target
## ===========================================================

## Specify the target IP address of the PING target you want to remove here:
$TargetIPAdress = "172.16.10.1"

## ===========================================================
## Do NOT change below
## ===========================================================

## get the class instance to remove
$Class = Get-SCOMClass -Name OpsLogix.IMP.Ping.Target
$ClassInstance = Get-SCOMMonitoringObject -Class $Class | where { $_."[OpsLogix.IMP.Ping.Target].TargetHost".value -like $TargetIPAdress}

# 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 of script
## ======================================================================

Remove a ping host and all its targets

And if you want to remove a source use below script:

NOTE! This will also remove all targets on the source.

## ======================================================================
## Example script how to remove a ping host and all its targets
## in SCOM using Powershell
## ======================================================================
## Michel Kamp / Elias Fröjd
## ======================================================================
## Run this script on a MS server
## ======================================================================

Import-Module OperationsManager

## login to the MS group
New-SCOMManagementGroupConnection -ComputerName "localhost"
$mg = Get-SCOMManagementGroup

## ===========================================================
## == remove a ping host and all its targets
## ===========================================================

## Specify the source host name that you want to remove here:
$PingSourceHostName = "DEVOM2012SP1-01.contoso.com"


## ===========================================================
## Do NOT change below
## ===========================================================

## get the class instance to remove

$Class = Get-SCOMClass -Name OpsLogix.IMP.Ping.Host
$ClassInstance = Get-SCOMMonitoringObject -Class $Class | where { $_.DisplayName -like $PingSourceHostName}
# 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 of script
## ======================================================================