1- # THIS SCRIPT CAN BE DANGEROUS, MAKE BACKUPS OF YOUR OPSDB AND DW! OR RUN WITH THE '-PauseOnEach' PARAMETER TO BE SAFE
1+ # THIS SCRIPT CAN BE DANGEROUS, MAKE BACKUPS OF YOUR OPSDB AND DW! OR RUN WITH THE '-PauseOnEach' PARAMETER
22
33# PowerShell script to automatically remove an MP and its dependencies.
44#
55# Original Author : Christopher Crammond, Chandra Bose
6- # Modified By : Blake Drumm (blakedrumm@microsoft.com)
6+ # Author : Blake Drumm (blakedrumm@microsoft.com)
77# Date Created : April 24th, 2012
88# Date Modified : July 19th, 2021
99#
10- # Version : 2 .0.7
10+ # Version : 3 .0.0
1111#
1212# Arguments : ManagementPackName. (Provide the value of the management pack name or id from the management pack properties. Otherwise, the script will fail.)
1313
1414<#
15- Change Log
16- ----------
17- August 19th, 2021:
15+ Updated - August 19th, 2021:
1816 Added ability to remove XML Reference from Unsealed Management Packs
19-
20- July 19th , 2022:
21- Attempted to fix some issues with processing MP Elements
17+
18+ Updated - July 20th , 2022:
19+ Major Update, my attempt to fix some issues with processing MP Elements
2220#>
2321
2422# Example:
2523# .\Remove-MPDependencies -ManagementPackName "*APM*"
2624#
2725# .\Remove-MPDependencies.ps1 -ManagementPackName Microsoft.Azure.ManagedInstance.Discovery
2826#
29- # Get-SCOMManagementPack -DisplayName "Microsoft Azure SQL Managed Instance (Discovery)" | .\Remove-MPDependencies.ps1 -DryRun -PauseOnEach
27+ # Get-SCManagementPack -DisplayName "Microsoft Azure SQL Managed Instance (Discovery)" | .\Remove-MPDependencies.ps1 -DryRun -PauseOnEach
3028#
31- # Get-SCOMManagementPack -DisplayName "Microsoft Azure SQL Managed Instance (Discovery)" | .\Remove-MPDependencies.ps1
29+ # Get-SCManagementPack -DisplayName "Microsoft Azure SQL Managed Instance (Discovery)" | .\Remove-MPDependencies.ps1
3230
3331# Needed for SCOM SDK
3432param
4846 [Parameter (Position = 4 )]
4947 [string ]$ExportPath
5048)
51-
5249function Remove-MPDependencies
5350{
5451 param
@@ -75,18 +72,16 @@ function Remove-MPDependencies
7572 }
7673 if (! (Test-Path $ExportPath ))
7774 {
75+ Write-Host " Creating directory for export: " - NoNewline - ForegroundColor Green
76+ Write-Host $ExportPath - ForegroundColor Cyan
7877 New-Item - ItemType Directory - Path $ExportPath | Out-Null
7978 }
8079 if (! $ManagementPackName -and ! $ManagementPackId )
8180 {
8281 Write-Host " -ManagementPackName or -ManagementPackId is required!" - ForegroundColor Red
83- set-location $cwd
82+ # set-location $cwd
8483 break
8584 }
86- if ($ManagementPackName -contains " *" )
87- {
88- $ManagementPackName = Get-SCManagementPack - Name $ManagementPackName | Select-First 1
89- }
9085 $ipProperties = [System.Net.NetworkInformation.IPGlobalProperties ]::GetIPGlobalProperties()
9186 $rootMS = " {0}.{1}" -f $ipProperties.HostName , $ipProperties.DomainName
9287
@@ -95,30 +90,30 @@ function Remove-MPDependencies
9590 #
9691 # Helper method that will remove the list of given Management Packs.
9792 #
98- function RemoveMPsHelper
93+ function Remove-MPsHelper
9994 {
10095 param ($mpList )
10196 foreach ($mp in $mpList )
10297 {
103- $id = $null
104- $recursiveListOfManagementPacksToRemove = Get-SCOMManagementPack - Name $mp.Name - Recurse
98+ $id + = $null
99+ $recursiveListOfManagementPacksToRemove = Get-SCManagementPack - Name $mp.Name - Recurse
105100 if ($recursiveListOfManagementPacksToRemove.Count -gt 1 )
106101 {
107102 Write-Output " `r`n "
108103 Write-Host " The following dependent management packs have to be deleted, before deleting " - NoNewline
109104 Write-Host $ ($mp.Name ) - ForegroundColor Cyan
110105
111106 # $recursiveListOfManagementPacksToRemove | format-table name, Sealed
112- RemoveMPsHelper $recursiveListOfManagementPacksToRemove
107+ Remove-MPsHelper $recursiveListOfManagementPacksToRemove
113108 }
114109 else
115110 {
116- $mpPresent = Get-ManagementPack - Name $mp.Name
111+ $mpPresent = Get-SCManagementPack - Name $mp.Name
117112 $Error.Clear ()
118113 if ($mpPresent -eq $null )
119114 {
120115 # If the MP wasn't found, we skip the uninstallation of it.
121- Write-Host " $mp has already been uninstalled"
116+ Write-Host " $ ( $mp .Name ) has already been uninstalled" - ForegroundColor Cyan
122117 }
123118 else
124119 {
@@ -133,48 +128,98 @@ function Remove-MPDependencies
133128 {
134129 foreach ($arg in $firstArgName )
135130 {
136- Write-Host " * Removing " - NoNewline
137- Write-Host $arg - ForegroundColor Green - NoNewline
138- Write-Host " reference from " - NoNewLine
139- Write-Host $ ($mp.Name ) - ForegroundColor Cyan
131+ Write-Host " * Removing " - NoNewline - ForegroundColor Green
132+ Write-Host $arg - ForegroundColor Cyan - NoNewline
133+ Write-Host " reference from " - NoNewLine - ForegroundColor Green
134+ Write-Host $ ($mp.Name ) - ForegroundColor DarkCyan
140135 try
141136 {
142137 [array ]$removed = $null
143138 [array ]$alias = $null
144- $mpPresent | Export-SCOMManagementPack - Path $ExportPath - PassThru - ErrorAction Stop | Out-Null
139+ [array ]$AlertMessage = $null
140+ [array ]$id = $null
141+ Write-Host " * Exporting MP to: " - ForegroundColor DarkCyan - NoNewline
142+ Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ForegroundColor Cyan
143+ $mpPresent | Export-SCManagementPack - Path $ExportPath - PassThru - ErrorAction Stop | Out-Null
145144 $xmldata = [xml ](Get-Content " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ErrorAction Stop);
146- Write-Host " * Backing up to:" - ForegroundColor Green - NoNewline
147- Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .backup.xml" - ForegroundColor Cyan
148- $xmlData.Save (" $ExportPath `\$ ( $mpPresent.Name ) .backup.xml" )
149145 [version ]$mpversion = $xmldata.ManagementPack.Manifest.Identity.Version
146+ Write-Host " * Backing up to: " - ForegroundColor DarkCyan - NoNewline
147+ Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .backup-v$mpversion .xml" - ForegroundColor Cyan
148+ $xmlData.Save (" $ExportPath `\$ ( $mpPresent.Name ) .backup.xml" )
150149 $xmldata.ManagementPack.Manifest.Identity.Version = [version ]::New($mpversion.Major , $mpversion.Minor , $mpversion.Build , $mpversion.Revision + 1 ).ToString()
150+ Write-Host " * Current Management Pack Version: " - NoNewline - ForegroundColor DarkCyan
151+ Write-Host $mpversion - ForegroundColor Cyan
152+
153+ Write-Host " * Updating Management Pack Version to: " - NoNewline - ForegroundColor DarkCyan
154+ Write-Host $ ($xmldata.ManagementPack.Manifest.Identity.Version ) - ForegroundColor Cyan
151155 $aliases = $null
152156 $xmlData.ChildNodes.Manifest.References.Reference | Where-Object { $_.ID -eq $arg } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $aliases += $_.Alias ; [void ]$_.ParentNode.RemoveChild ($_ ); }
153157 foreach ($alias in $aliases )
154158 {
155- Write-Host " * Alias found: $alias "
156- $xmlData.ChildNodes.Monitoring.Overrides.SecureReferenceOverride | Where-Object { $ ($_.Context -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id = $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
157- $xmlData.ChildNodes.Monitoring.Overrides.MonitorPropertyOverride | Where-Object { $ ($_.Context -split ' !' )[0 ] -eq $alias -or ($ ($_.Monitor ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id = $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
159+ Write-Host " * Alias found: " - NoNewline - ForegroundColor Green
160+ Write-Host $alias - ForegroundColor DarkCyan
161+ # Type Definitions
162+ $xmlData.ChildNodes.TypeDefinitions.EntityTypes.ClassTypes.ClassType | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Base ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { if ($_.Property ) { $id += $_.Id }; $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
163+
164+ # Discoveries
165+ $xmlData.ChildNodes.Monitoring.Discoveries.Discovery | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Target ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
166+
167+ # Rules
168+ $xmlData.ChildNodes.Monitoring.Rules.Rule | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Target ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
169+
170+ # Tasks
171+ $xmlData.ChildNodes.Monitoring.Tasks.Task | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Target ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
172+
173+ # Monitors
174+ # Aggregate Monitor
175+ $xmlData.ChildNodes.Monitoring.Monitors.AggregateMonitor | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Target ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { if ($_.AlertSettings ) { $AlertMessage += $_.AlertSettings.AlertMessage ; $id += $_.AlertSettings.AlertMessage }; $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
176+ # Dependency Monitor
177+ $xmlData.ChildNodes.Monitoring.Monitors.DependencyMonitor | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Target ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { if ($_.AlertSettings ) { $AlertMessage += $_.AlertSettings.AlertMessage ; $id += $_.AlertSettings.AlertMessage }; $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
178+ # Unit Monitor
179+ $xmlData.ChildNodes.Monitoring.Monitors.UnitMonitor | Where-Object { $ ($_.Id -split ' !' )[0 ] -eq $alias -or ($ ($_.Target ) -split ' !' )[0 ] -eq $alias -or ($ ($_.ParentMonitorID ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
180+
181+ # Overrides
182+ $xmlData.ChildNodes.Monitoring.Overrides.SecureReferenceOverride | Where-Object { $ ($_.Context -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
183+ $xmlData.ChildNodes.Monitoring.Overrides.MonitorPropertyOverride | Where-Object { $ ($_.Context -split ' !' )[0 ] -eq $alias -or ($ ($_.Monitor ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
158184 $xmlData.ChildNodes.Monitoring.Overrides.DiscoveryConfigurationOverride | Where-Object { $ ($_.Context -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
159185 $xmlData.ChildNodes.Monitoring.Overrides.RulePropertyOverride | Where-Object { $ ($_.Context -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
160186 }
161187
188+ # Presentation
189+ foreach ($alertMsg in $AlertMessage )
190+ {
191+ $xmlData.ChildNodes.Presentation.StringResources | Where-Object { $_.StringResource.Id -eq $alertMsg } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
192+ }
193+
194+ # Language Packs
162195 foreach ($identifer in $id )
163196 {
164197 $xmlData.ChildNodes.LanguagePacks.LanguagePack.DisplayStrings.DisplayString | Where-Object { $_.ElementID -eq $identifer } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; [void ]$_.ParentNode.RemoveChild ($_ ) }
165198 }
166- Write-Host " * Removed the following XML Data from the MP: " - NoNewline
199+ Write-Host " * Removed the following XML Data from the MP: " - NoNewline - ForegroundColor DarkYellow
167200 Write-Host $ ($mp.Name ) - ForegroundColor Cyan
168- $removed
201+ Write-Host " `" $removed `" " - ForegroundColor Gray
169202
170- Write-Host " * Saving copy to: " - NoNewline - ForegroundColor Green
171- Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ForegroundColor Cyan
203+ Write-Host " * Saving Updated MP to: " - NoNewline - ForegroundColor DarkCyan
204+ Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ForegroundColor Green
172205 $xmlData.Save (" $ExportPath `\$ ( $mpPresent.Name ) .xml" )
173- Write-Host " * Importing MP: " - NoNewLine
174- Write-Host $ ($mpPresent.Name ) - ForegroundColor Cyan
175- Import-SCOMManagementPack - FullName " $ExportPath `\$ ( $mpPresent.Name ) .xml" | Out-Null
176- Write-Host " * Imported modified Management Pack: " - NoNewLine
206+ Write-Host " * Importing MP: " - NoNewLine - ForegroundColor Green
177207 Write-Host $ ($mpPresent.Name ) - ForegroundColor Cyan
208+ $Error.Clear ()
209+ try
210+ {
211+ Import-SCManagementPack - FullName " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ErrorAction Stop | Out-Null
212+ Write-Host " * Imported modified Management Pack: " - NoNewLine - ForegroundColor Green
213+ Write-Host $ ($mpPresent.Name ) - ForegroundColor Cyan
214+ }
215+ catch
216+ {
217+ foreach ($errors in $Error )
218+ {
219+ Write-Warning $errors
220+ }
221+ pause
222+ }
178223 }
179224 catch
180225 { Write-Warning $_ }
@@ -183,12 +228,12 @@ function Remove-MPDependencies
183228 }
184229 else
185230 {
186- Write-Host " * Backing up Sealed Management Pack: " - NoNewLine - ForegroundColor Green
187- Write-Host " $ExportPath `\$ ( $mp.Name ) .xml" - ForegroundColor Cyan
188- $mpPresent | Export-SCOMManagementPack - Path $ExportPath - PassThru - ErrorAction Stop | Out-Null
231+ Write-Host " * Backing up Sealed Management Pack: " - NoNewLine - ForegroundColor DarkCyan
232+ Write-Host " $ExportPath `\$ ( $mp.Name ) .xml" - ForegroundColor Green
233+ $mpPresent | Export-SCManagementPack - Path $ExportPath - PassThru - ErrorAction Stop | Out-Null
189234 Write-Host " * Uninstalling Sealed Management Pack: " - NoNewLine - ForegroundColor Red
190235 Write-Host $ ($mp.Name ) - ForegroundColor Cyan
191- Uninstall-ManagementPack - managementpack $mp
236+ Remove-SCManagementPack - managementpack $mp
192237 }
193238 }
194239 if ($PauseOnEach )
@@ -206,40 +251,47 @@ function Remove-MPDependencies
206251 # all MPs that depend on it. This list will be presented to the user prompting
207252 # if the user wants to continue and removing the list of presented MPs
208253 #
209- function RemoveMPs
254+ function Remove-MPs
210255 {
211256 param ($mpname ,
212257 $mpid )
213258 if ($mpname )
214259 {
215- $listOfManagementPacksToRemove = Get-SCOMManagementPack - Name $mpname - Recurse
260+ $listOfManagementPacksToRemove = Get-SCManagementPack - Name $mpname - Recurse
216261 }
217262 elseif ($mpid )
218263 {
219- $listOfManagementPacksToRemove = Get-SCOMManagementPack - Id $mpid - Recurse
264+ $listOfManagementPacksToRemove = Get-SCManagementPack - Id $mpid - Recurse
220265 }
221266 if (! $listOfManagementPacksToRemove )
222267 {
223268 Write-Host " No Management Packs found for: $firstArgName " - ForegroundColor Yellow
224- Set-Location $cwd
269+ # Set-Location $cwd
225270 break
226271 }
227272 else
228273 {
274+ Write-Host " List of Management Packs that will be affected:" - ForegroundColor Green
229275 $listOfManagementPacksToRemove | Format-Table Name, Sealed, DisplayName | Out-Host
230-
231- $title = " Uninstall/Edit Management Packs"
232- $message = " Do you want to uninstall/edit the above $ ( $listOfManagementPacksToRemove.Count ) management packs and its dependent management packs?"
233-
234- $yes = New-Object System.Management.Automation.Host.ChoiceDescription " &Yes" , " Uninstall (Sealed) / Edit (Unsealed) selected Management Packs."
235- $no = New-Object System.Management.Automation.Host.ChoiceDescription " &No" , " Do not remove Management Packs."
236- $options = [System.Management.Automation.Host.ChoiceDescription []]($yes , $no )
237-
238- $result = $host.ui.PromptForChoice ($title , $message , $options , 0 )
276+ if (! $DryRun )
277+ {
278+ $title = " Uninstall/Edit Management Packs"
279+ $message = " Do you want to uninstall/edit the above $ ( $listOfManagementPacksToRemove.Count ) management packs and its dependent management packs?"
280+
281+ $yes = New-Object System.Management.Automation.Host.ChoiceDescription " &Yes" , " Uninstall (Sealed) / Edit (Unsealed) selected Management Packs."
282+ $no = New-Object System.Management.Automation.Host.ChoiceDescription " &No" , " Do not remove Management Packs."
283+ $options = [System.Management.Automation.Host.ChoiceDescription []]($yes , $no )
284+
285+ $result = $host.ui.PromptForChoice ($title , $message , $options , 0 )
286+ }
287+ else
288+ {
289+ $result = 0
290+ }
239291
240292 switch ($result )
241293 {
242- 0 { RemoveMPsHelper $listOfManagementPacksToRemove }
294+ 0 { Remove-MPsHelper $listOfManagementPacksToRemove }
243295 1 { Write-Host " `n Exiting without removing any management packs" - ForegroundColor DarkCyan }
244296 }
245297 }
@@ -271,25 +323,25 @@ function Remove-MPDependencies
271323 else
272324 {
273325 Write-Warning " Missing or Improper Arguments..`n`n Specify the ID of the management pack you wish to delete (usually like: Microsoft.Azure.ManagedInstance.Discovery)."
274- set-location $cwd
326+ # set-location $cwd
275327 break
276328 }
277- add-pssnapin " Microsoft.EnterpriseManagement.OperationsManager.Client" ;
278- $cwd = get-location
279- set-location " OperationsManagerMonitoring::" ;
329+ Add-PSSnapin " Microsoft.EnterpriseManagement.OperationsManager.Client" ;
330+ # $cwd = get-location
331+ # set-location "OperationsManagerMonitoring::";
280332 $mgConnection = new-managementGroupConnection - ConnectionString:$rootMS ;
281333 if ($firstArgName )
282334 {
283- RemoveMPs - mpname $firstArgName
335+ Remove-MPs - mpname $firstArgName
284336 }
285337 elseif ($firstArgId )
286338 {
287- RemoveMPs - mpid $firstArgId
339+ Remove-MPs - mpid $firstArgId
288340 }
289341
290342
291- set-location $cwd
292- remove-pssnapin " Microsoft.EnterpriseManagement.OperationsManager.Client" ;
343+ # Set-Location $cwd
344+ Remove-PSSnapin " Microsoft.EnterpriseManagement.OperationsManager.Client" ;
293345
294346 Write-Host ' Script Completed!' - ForegroundColor Green
295347}
305357 # Remove-MPDependencies -ManagementPackName "*APM*"
306358 # or
307359 # Remove-MPDependencies -ManagementPackName Microsoft.Azure.ManagedInstance.Discovery
360+ # or
361+ # Remove-MPDependencies Microsoft.Windows.Server.2016*
308362 Remove-MPDependencies
309363}
0 commit comments