1- # THIS SCRIPT CAN BE DANGEROUS, MAKE BACKUPS OF YOUR OPSDB AND DW! OR RUN WITH THE '-PauseOnEach' PARAMETER
1+ # THIS SCRIPT CAN BE DANGEROUS, MAKE BACKUPS OF YOUR OPSDB AND DW! OR RUN WITH THE '-PauseOnEach' AND `-DryRun` PARAMETERS
22
33# PowerShell script to automatically remove an MP and its dependencies.
44#
@@ -17,6 +17,10 @@ Updated - August 19th, 2021:
1717
1818Updated - July 20th, 2022:
1919 Major Update, my attempt to fix some issues with processing MP Elements
20+
21+ Updated - July 26th, 2022:
22+ Minor Update
23+
2024#>
2125
2226# Example:
@@ -93,14 +97,16 @@ function Remove-MPDependencies
9397 function Remove-MPsHelper
9498 {
9599 param ($mpList )
100+ $recursiveListOfManagementPacksToRemove = $null
101+ $mpList = $mpList
96102 foreach ($mp in $mpList )
97103 {
98- $id + = $null
104+ $id = $null
99105 $recursiveListOfManagementPacksToRemove = Get-SCManagementPack - Name $mp.Name - Recurse
100106 if ($recursiveListOfManagementPacksToRemove.Count -gt 1 )
101107 {
102108 Write-Output " `r`n "
103- Write-Host " The following dependent management packs have to be deleted, before deleting " - NoNewline
109+ Write-Host " The following dependent management packs have to be edited/ deleted, before deleting " - NoNewline
104110 Write-Host $ ($mp.Name ) - ForegroundColor Cyan
105111
106112 # $recursiveListOfManagementPacksToRemove | format-table name, Sealed
@@ -117,21 +123,14 @@ function Remove-MPDependencies
117123 }
118124 else
119125 {
120- if ($DryRun )
121- {
122- Write-Host " * NOT Uninstalling " - NoNewLine - ForegroundColor Gray
123- Write-Host $ ($mp.Name ) - ForegroundColor Cyan
124- Write-Host " Cannot continue, as this script relies on realtime data to go through each Management Pack." - ForegroundColor Gray
125- break
126- }
127- elseif ($mpPresent.Sealed -eq $false )
126+ if ($mpPresent.Sealed -eq $false )
128127 {
129128 foreach ($arg in $firstArgName )
130129 {
131130 Write-Host " * Removing " - NoNewline - ForegroundColor Green
132131 Write-Host $arg - ForegroundColor Cyan - NoNewline
133132 Write-Host " reference from " - NoNewLine - ForegroundColor Green
134- Write-Host $ ($mp.Name ) - ForegroundColor DarkCyan
133+ Write-Host $ ($mp.Name ) - ForegroundColor Magenta
135134 try
136135 {
137136 [array ]$removed = $null
@@ -147,19 +146,19 @@ function Remove-MPDependencies
147146 Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .backup-v$mpversion .xml" - ForegroundColor Cyan
148147 $xmlData.Save (" $ExportPath `\$ ( $mpPresent.Name ) .backup.xml" )
149148 $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
152149
153- Write-Host " * Updating Management Pack Version to: " - NoNewline - ForegroundColor DarkCyan
154- Write-Host $ ($xmldata.ManagementPack.Manifest.Identity.Version ) - ForegroundColor Cyan
155150 $aliases = $null
156151 $xmlData.ChildNodes.Manifest.References.Reference | Where-Object { $_.ID -eq $arg } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $aliases += $_.Alias ; [void ]$_.ParentNode.RemoveChild ($_ ); }
157152 foreach ($alias in $aliases )
158153 {
159154 Write-Host " * Alias found: " - NoNewline - ForegroundColor Green
160- Write-Host $alias - ForegroundColor DarkCyan
155+ Write-Host $alias - ForegroundColor Magenta
156+
161157 # Type Definitions
162158 $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 ($_ ) }
159+ $xmlData.ChildNodes.TypeDefinitions.ModuleTypes.DataSourceModuleType | Where-Object { $ ($_.RunAs -split ' !' )[0 ] -eq $alias -or ($ ($_.Id ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $id += $_.Id ; $removed += $_.InnerXML ; $id += $_.Id ; [void ]$_.RemoveChild ($_ ) }
160+ $xmlData.ChildNodes.TypeDefinitions.ModuleTypes.ProbeActionModuleType.ModuleImplementation.Managed.Assembly | Where-Object { $ ($_ -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += ($_.ParentNode ).InnerXML; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
161+ $xmlData.ChildNodes.TypeDefinitions.MonitorTypes.UnitMonitorType | Where-Object { $ ($_.RunAs -split ' !' )[0 ] -eq $alias -or ($ ($_.Id ) -split ' !' )[0 ] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; $id += $_.Id ; [void ]$_.ParentNode.RemoveChild ($_ ) }
163162
164163 # Discoveries
165164 $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 ($_ ) }
@@ -196,21 +195,45 @@ function Remove-MPDependencies
196195 {
197196 $xmlData.ChildNodes.LanguagePacks.LanguagePack.DisplayStrings.DisplayString | Where-Object { $_.ElementID -eq $identifer } | ForEach-Object { $removed += $_.ParentNode.InnerXML ; [void ]$_.ParentNode.RemoveChild ($_ ) }
198197 }
199- Write-Host " * Removed the following XML Data from the MP: " - NoNewline - ForegroundColor DarkYellow
200- Write-Host $ ($mp.Name ) - ForegroundColor Cyan
201- Write-Host " `" $removed `" " - ForegroundColor Gray
202-
203- Write-Host " * Saving Updated MP to: " - NoNewline - ForegroundColor DarkCyan
204- Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ForegroundColor Green
205- $xmlData.Save (" $ExportPath `\$ ( $mpPresent.Name ) .xml" )
206- Write-Host " * Importing MP: " - NoNewLine - ForegroundColor Green
207- Write-Host $ ($mpPresent.Name ) - ForegroundColor Cyan
198+ if ($removed )
199+ {
200+ Write-Host " * Current Management Pack Version: " - NoNewline - ForegroundColor DarkCyan
201+ Write-Host $mpversion - ForegroundColor Cyan
202+
203+ Write-Host " * Updating Management Pack Version to: " - NoNewline - ForegroundColor DarkCyan
204+ Write-Host $ ($xmldata.ManagementPack.Manifest.Identity.Version ) - ForegroundColor Cyan
205+
206+ Write-Host " * Removed the following XML Data from the MP: " - NoNewline - ForegroundColor DarkYellow
207+ Write-Host $ ($mp.Name ) - ForegroundColor Cyan
208+ Write-Host " `" $removed `" " - ForegroundColor Gray
209+
210+ Write-Host " * Saving Updated MP to: " - NoNewline - ForegroundColor DarkCyan
211+ Write-Host " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ForegroundColor Green
212+ $xmlData.Save (" $ExportPath `\$ ( $mpPresent.Name ) .xml" )
213+ }
214+ else
215+ {
216+ Write-Host " * Unable to locate any XML Nodes to remove" - ForegroundColor Red
217+ }
208218 $Error.Clear ()
209219 try
210220 {
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
221+ if ($DryRun )
222+ {
223+ Write-Host " * Dry Run Parameter Present - Skipping Import Step for: " - NoNewLine - ForegroundColor Gray
224+ Write-Host $ ($mp.Name ) - ForegroundColor Cyan
225+ }
226+ else
227+ {
228+ if ($removed )
229+ {
230+ Write-Host " * Importing MP: " - NoNewLine - ForegroundColor Green
231+ Write-Host $ ($mpPresent.Name ) - ForegroundColor Cyan
232+ Import-SCManagementPack - FullName " $ExportPath `\$ ( $mpPresent.Name ) .xml" - ErrorAction Stop | Out-Null
233+ Write-Host " * Imported modified Management Pack: " - NoNewLine - ForegroundColor Green
234+ Write-Host $ ($mpPresent.Name ) - ForegroundColor Cyan
235+ }
236+ }
214237 }
215238 catch
216239 {
@@ -231,15 +254,26 @@ function Remove-MPDependencies
231254 Write-Host " * Backing up Sealed Management Pack: " - NoNewLine - ForegroundColor DarkCyan
232255 Write-Host " $ExportPath `\$ ( $mp.Name ) .xml" - ForegroundColor Green
233256 $mpPresent | Export-SCManagementPack - Path $ExportPath - PassThru - ErrorAction Stop | Out-Null
234- Write-Host " * Uninstalling Sealed Management Pack: " - NoNewLine - ForegroundColor Red
235- Write-Host $ ($mp.Name ) - ForegroundColor Cyan
236- Remove-SCManagementPack - managementpack $mp
257+
258+ if ($DryRun )
259+ {
260+ Write-Host " * Dry Run Parameter Present - Skipping Delete Step for: " - NoNewLine - ForegroundColor Gray
261+ Write-Host $ ($mp.Name ) - ForegroundColor Cyan
262+ }
263+ else
264+ {
265+ Write-Host " * Uninstalling Sealed Management Pack: " - NoNewLine - ForegroundColor Red
266+ Write-Host $ ($mp.Name ) - ForegroundColor Cyan
267+ Remove-SCManagementPack - managementpack $mp
268+ }
269+
237270 }
238271 }
239272 if ($PauseOnEach )
240273 {
241274 pause
242275 }
276+ continue
243277 }
244278 }
245279 }
0 commit comments