This is a continuation of a Data Center Automation series of posts that I have been working on with Anders Bengtsson. Here are the first four posts in this series:

Creating Management Packs in SCOM 2012 with PowerShell
Creating Performance Collection Rules in SCOM 2012 with PowerShell
Creating Event Based Alerting Rules in SCOM 2012 with PowerShell
Enabling or Disabling Workflows in SCOM 2012 with PowerShell

This script is also included as an activity in the Operations Manager Admin Integration Pack.

Syntax:  

.\DeleteObject.ps1 –ManagementServer ‘om01.contoso.com’ –ManagementPackID ‘custom.example.test’ –MPObjectID ‘custom.example.test.rule.test1’

Parameters:

Name Description
ManagementServer Name of MS to connect to
ManagementPackID ID of the MP you want to modify
MPObjectID Rule, Monitor, Discovery, or Task that you want to delete (MP must be unsealed)
1 Param( 2 [parameter(Mandatory=$true)] 3 $ManagementServer, 4 [parameter(Mandatory=$true)] 5 $ManagementPackID, 6 [parameter(Mandatory=$true)] 7 $MPObjectID 8 ) 9 10 Write-Host "ManagementServer: "$ManagementServer 11 Write-Host "ManagementPackID: "$ManagementPackID 12 Write-Host "MPObjectID: "$MPObjectID 13 14 Write-Host "Adding SCOM Snap-in" 15 Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client 16 17 Write-Host "Connecting to SCOM Management Group" 18 $ManagementServer = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer) 19 20 Write-Host "Getting MP Information and Incrementing Version" 21 try 22 { 23 $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0] 24 $VIncrement = $MP.Version.ToString().Split('.') 25 $VIncrement[$VIncrement.Length - 1] = ([system.int32]::Parse($VIncrement[$VIncrement.Length - 1]) + 1).ToString() 26 $MP.Version = ([string]::Join(".", $VIncrement)) 27 } 28 catch 29 { 30 Write-Host "MP Not Found, Exiting" 31 exit 32 } 33 34 Write-Host "Getting MP Object" 35 $MPObject = $MP.FindManagementPackElementByName($MPObjectID) 36 37 Write-Host "Deleting Object" 38 $MPObject.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingDelete 39 40 Write-Host "Writing Changes via SDK" 41 $MP.AcceptChanges() 42 43 Write-Host "Script Completed"