Freigeben über


Recursively removing Management Packs

As a developer working with SCOM and management packs, I have had several occasions where I have needed to remove a lot of management packs (MPs).  Unfortunately, you must remove the MPs one-at-a-time in the UI.  Granted, in a production environment you are probably not removing a lot of MPs; however, if you are developing MPs you might need to remove a lot of MPs (in my case - frequently).

For instance, during development of the JEE Application Server MPs it was not uncommon to import all 15 or 16 MPs to verify a bug.  When I learned that I might have to do this several times to resolve a bug, I quickly realized I needed to find an automated way to remove our MPs!  Back in August I posted a script to do this (along with importing all JEE MPs in a directory), but I think it the recursive delete desrves it's own posting.

The script below takes a single argument which is the case insensitve name of the Management Pack to remove.  Sometimes I have noticed that I needed to manually remove the secure references overide MP (i.e. Microsoft.SystemCenter.SecureReferenceOverride).  Sometimes the API would find and remove this, sometimes not.

Script

 

 # Needed for SCOM SDK
 $ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
 $rootMS = "{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName
 
 
 #######################################################################
 #
 # Helper method that will remove the list of given Management Packs.
 #
 function RemoveMPsHelper
 {param($mpList)
 foreach ($mp in $mpList)
 {
 Echo " * Uninstalling $mp"
 Uninstall-ManagementPack -managementpack $mp
 }
 }
 
 #######################################################################
 #
 # Remove 'all' of the JEE MPs as well as MPs that are dependent on them.
 # The remove is done by finding the base MP (Microsoft.JEE.Library) and finding
 # all MPs that depend on it. This list will be presented to the user prompting
 # if the user wants to continue and removing the list of presented MPs
 #
 function RemoveMPs
 {param($mp)
 Import-Module 'E:\Program Files\System Center 2012\Operations Manager\Powershell\OperationsManager\OperationsManager.psd1'
 
 $listOfManagementPacksToRemove = Get-SCOMManagementPack -Name $mp -Recurse
 $listOfManagementPacksToRemove | format-table name
 
 $title = "Uninstall Management Packs"
 $message = "Do you want to uninstall the above management packs"
 
 $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Uninstall selected Management Packs."
 $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not remove Management Packs."
 $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
 
 $result = $host.ui.PromptForChoice($title, $message, $options, 0) 
 
 switch ($result)
 {
 0 {RemoveMPsHelper $listOfManagementPacksToRemove}
 1 {"Exiting without removing any management packs"}
 }
 }
 
 #######################################################################
 # Begin Script functionality
 #
 if ($args.Count -ne 1)
 {
 Echo "Improper command line arguments"
 Echo ""
 Echo "Specify a command line argument to either import or export MPs"
 exit 1
 }
 else
 {
 $firstArg = $args[0]
 
 add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
 $cwd = get-location
 set-location "OperationsManagerMonitoring::";
 $mgConnection = new-managementGroupConnection -ConnectionString:$rootMS;
 
 RemoveMPs $firstArg
 
 set-location $cwd
 remove-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"; 
 }
 

Sample output

 PS C:\drop> .\RecursiveRemove.ps1 microsoft.jee.library
 
 Name
 ----
 Tomcat.Seven.Tasks
 Microsoft.JEE.Tomcat.5
 Microsoft.JEE.Tomcat.6
 Microsoft.JEE.Tomcat.7
 Microsoft.JEE.JBoss.Library
 Microsoft.JEE.Tomcat.Library
 Microsoft.JEE.Library
 
 
 
 Uninstall Management Packs
 Do you want to uninstall the above management packs
 [Y] Yes [N] No [?] Help (default is "Y"): y
 * Uninstalling [Tomcat.Seven.Tasks, 31bf3856ad364e35, 7.3.2034.0]
 * Uninstalling [Microsoft.JEE.Tomcat.5, 31bf3856ad364e35, 7.3.2034.0]
 * Uninstalling [Microsoft.JEE.Tomcat.6, 31bf3856ad364e35, 7.3.2034.0]
 * Uninstalling [Microsoft.JEE.Tomcat.7, 31bf3856ad364e35, 7.3.2034.0]
 * Uninstalling [Microsoft.JEE.JBoss.Library, 31bf3856ad364e35, 7.3.2034.0]
 * Uninstalling [Microsoft.JEE.Tomcat.Library, 31bf3856ad364e35, 7.3.2034.0]
 * Uninstalling [Microsoft.JEE.Library, 31bf3856ad364e35, 7.3.2034.0]

RecursiveRemove.txt