New OM2012 PowerShell Function (Remove-SCOMRule)
Today I got a question from my fellow Premier Field Engineer Anders Bengtsson if I could help him with an OpsMgr PowerShell script he was creating for an Orchestrator Runbook he started to create. And while waiting for a PowerShell workshop I was about to deliver I booted my OM2012 servers and had a look at his PowerShell script.
He wanted to be able to delete an OpsMgr Rule using PowerShell. As you may know there is no Remove-SCOMRule Cmdlet or function available in the PowerShell OperationsManager Module. Let’s check to be sure.
No, we can disable, enable and get a SCOMRule but there is no Remove-SCOMRule Cmdlet.
So, if there is none, let’s create one!
First let’s have a look at the parameters for the Get-SCOMRule, we may could use the same parameters in our Remove-SCOMRule Function.
Get-Help Get-SCOMRule -Detailed
Looking at the list of parameters the Get-SCOMRule is using we start simple using the DisplayName and Name parameters for our new Remove-SCOMRule function.
Here is a function you can use and tweak to Remove SCOM Rules in your OM2012 environment.
<#
.Synopsis
Removes a list of monitoring rules.
.DESCRIPTION
The Remove-SCOMRule removes a list of monitoring rules.
.EXAMPLE
Remove-SCOMRule -DisplayName "test rule"
.EXAMPLE
Get-SCOMRule -Name "MomUIGeneratedRule2c30f12ddfb6493fbd0c92e41db94495" | Remove-SCOMRule -Verbose
#>
function Remove-SCOMRule
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Specifies the name of the object
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$Name,
# Specifies the displayname of the object
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
Position=1)]
[string]$dislayname
)
Begin
{
Write-Verbose "Starting Function Remove-SCOMRule"
}
Process
{
#Create Hashtable for Get-SCOMRule Cmdlet parameters
if ($name)
{
$parms = @{'name'=$name}
}
else
{
$parms = @{'displayname'=$displayname}
}
Write-Debug "`$parms: $parms"
$Rule = Get-SCOMRule @parms
Write-Verbose "Removing Rule: $($Rule.DisplayName)"
$MP = Get-SCOMManagementPack -Name ($Rule.ManagementPackName)
$MPObject = $MP.FindManagementPackElementByName($Rule.Name);
$MPObject.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingDelete
$MP.AcceptChanges()
}
End
{
Write-Verbose "Finished Function Remove-SCOMRule"
}
}
How to use this function?
- Save the above PowerShell Function in a script.
- Dot source (dot space scriptname location) the script where you have stored above function. And look at the help.
Now we can use the Get-SCOMRule Cmdlet to retrieve the Rule and pipe it to the Remove-SCOMRule Function. You can even remove multiple Rule in one go if you retrieve multiple rules using the Get-SCOMRule Cmdlet.
Remarks:
Function only work when the OperationsManager Powershell module is loaded in PowerShell session.
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND
ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free
right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree:
(i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid
copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and
Our suppliers from and against any claims or lawsuits, including attorneys' fees, that arise or result from the use or distribution of the Sample Code.
Comments
Anonymous
January 01, 2003
Hi Mike, If I find some time I'll try to create a remove-scomoverride function if you want. /StefanAnonymous
May 10, 2013
Very nice. Then we only need one to remove an override made to a specific rule :-)Anonymous
August 21, 2016
Hello,Can we use this trick to remove SCOMMonitor as well if yes how we can do that.