@@ -55,11 +55,11 @@ PROCESS
5555 Write-Warning " $ ( Time- Stamp) Unable to run `' Get-SCOMManagementPack`' , stopping script:`n $ ( $error [0 ]) "
5656 break
5757 }
58- if ($recurseMPList.Count -eq 0 )
59- {
60- Write-Output " $ ( Time- Stamp) Did not find any Management Packs!"
61- return
62- }
58+ if ($recurseMPList.Count -eq 0 )
59+ {
60+ Write-Output " $ ( Time- Stamp) Did not find any Management Packs!"
61+ return
62+ }
6363 $sealedMPs = $recurseMPList.Where ({ $_.Sealed })
6464 $unsealedMPs = $recurseMPList.Where ({ ! $_.Sealed })
6565 Write-Output " $ ( Time- Stamp) Sealed MPs: (Count: $ ( $sealedMPs.Count ) ):"
@@ -82,18 +82,87 @@ PROCESS
8282 [Parameter (Mandatory = $true ,
8383 ValueFromPipeline = $true ,
8484 Position = 0 )]
85- $ManagementPack
85+ $ManagementPack ,
86+ [Parameter (Mandatory = $false ,
87+ Position = 1 )]
88+ [switch ]$Sealed ,
89+ [Parameter (Mandatory = $false ,
90+ Position = 2 )]
91+ [switch ]$Unsealed ,
92+ [Parameter (Mandatory = $false ,
93+ Position = 3 )]
94+ [string ]$ReferenceToRemove ,
95+ [Parameter (Mandatory = $true ,
96+ Position = 4 )]
97+ [string ]$ExportPath
8698 )
99+
87100 trap
88101 {
89102 Write-Warning " $ ( $PSItem.Exception ) `n Function: Inner-RemoveMP"
90103 }
91104 Write-Output " $ ( Time- Stamp) Backing Up the following: $ ( $ManagementPack.DisplayName ) "
92105 $ManagementPack | Export-SCOMManagementPack - Path $ExportPath - ErrorAction Stop | Out-Null
93106 Write-Output " $ ( Time- Stamp) Saved to: $ExportPath \$ ( $ManagementPack.Name ) .xml"
94- # Start of Inner Remove MP Function
95- Write-Output " $ ( Time- Stamp) Removing the Management Pack: $ ( $ManagementPack.DisplayName ) "
96- Remove-SCManagementPack - ManagementPack $ManagementPack - Confirm:$false - ErrorAction Stop
107+ if ($Unsealed )
108+ {
109+ Write-Output " $ ( Time- Stamp) Attempting to remove related data in the Unsealed Management Pack: $ExportPath \$ ( $ManagementPack.Name ) .xml"
110+ $xmldata = [xml ](Get-Content " $ExportPath \$ ( $ManagementPack.Name ) .xml" - ErrorAction Stop);
111+ # Save a backup of the MP
112+ $xmlData.Save (" $ExportPath \$ ( $ManagementPack.Name ) .backup.xml" )
113+ # Get the version of the MP
114+ [version ]$mpversion = $xmldata.ManagementPack.Manifest.Identity.Version
115+ # Increment the version of the MP
116+ $xmldata.ManagementPack.Manifest.Identity.Version = [version ]::New($mpversion.Major , $mpversion.Minor , $mpversion.Build , $mpversion.Revision + 1 ).ToString()
117+ # Grab all the references
118+ $references = $xmlData.ChildNodes.Manifest.References.Reference | Where { $_.ID -eq $ReferenceToRemove } # | ForEach-Object { $removed += $_.ParentNode.InnerXML; $aliases += $_.Alias; [void]$_.ParentNode.RemoveChild($_); }
119+ [array ]$referencingId = @ ()
120+ [array ]$nodes = @ ()
121+ # Go through each reference
122+ foreach ($reference in $references )
123+ {
124+ # Find all overrides
125+ $Overrides = $xmlData.ChildNodes | Select-Xml - Xpath " //Overrides"
126+ foreach ($Override in $Overrides )
127+ {
128+ $nodes += $Override | Select-Object - ExpandProperty Node
129+ }
130+
131+ foreach ($node in $nodes )
132+ {
133+ $aliasFound = $node.ChildNodes.Where { $_.Context -match " $ ( $reference.Alias ) " }
134+
135+ foreach ($context in $aliasFound )
136+ {
137+ $referencingId += $context.Id
138+ [void ]$context.ParentNode.RemoveChild ($context )
139+ }
140+ }
141+ [void ]$reference.ParentNode.RemoveChild ($reference )
142+ $languagePacks = $xmlData.ManagementPack.LanguagePacks.LanguagePack.DisplayStrings.DisplayString | Where { $_.ElementID -like $referencingId }
143+ [void ]$languagePacks.ParentNode.RemoveChild ($languagePacks )
144+
145+ }
146+ $xmlData.Save (" $ExportPath \$ ( $ManagementPack.Name ) .xml" )
147+ Write-Output " $ ( Time- Stamp) Importing the modified Unsealed Management Pack: $ExportPath \$ ( $ManagementPack.Name ) .xml"
148+ Import-SCOMManagementPack - FullName " $ExportPath \$ ( $ManagementPack.Name ) .xml" | Out-Null
149+ <#
150+
151+ foreach ($alias in $aliases)
152+ {
153+ $xmlData.ChildNodes.Monitoring.Overrides.MonitorPropertyOverride | Where { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id = $_.Id; [void]$_.ParentNode.RemoveChild($_) }
154+ $xmlData.ChildNodes.Monitoring.Overrides.DiscoveryConfigurationOverride | Where { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id += $_.Id; [void]$_.ParentNode.RemoveChild($_) }
155+ $xmlData.ChildNodes.Monitoring.Overrides.RulePropertyOverride | Where { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id += $_.Id; [void]$_.ParentNode.RemoveChild($_) }
156+ }
157+ #>
158+
159+ }
160+ if ($Sealed )
161+ {
162+ # Start of Inner Remove MP Function
163+ Write-Output " $ ( Time- Stamp) Removing the Sealed Management Pack: $ ( $ManagementPack.DisplayName ) "
164+ }
165+ # Remove-SCManagementPack -ManagementPack $ManagementPack -Confirm:$false -ErrorAction Stop
97166 sleep 10
98167 }
99168 function Inner-GetMPRecurse
@@ -115,7 +184,14 @@ PROCESS
115184 {
116185 foreach ($MP in $recurseMP )
117186 {
118- Inner- RemoveMP - ManagementPack $MP
187+ if ($MP.Sealed -eq $false )
188+ {
189+ Inner- RemoveMP - ManagementPack $MP - Unsealed - ReferenceToRemove $ ($ManagementPack.Name ) - ExportPath $ExportPath
190+ }
191+ else
192+ {
193+ Inner- RemoveMP - ManagementPack $MP - Sealed - ExportPath $ExportPath
194+ }
119195 $recurseMP = Get-SCManagementPack - Id $ManagementPack.Id - Recurse - ErrorAction Stop
120196 if ($recurseMP.Count -gt 1 )
121197 {
175251 {
176252 Remove-SCOMManagementPackDependencies - ManagementPackName Microsoft.SQLServer.Windows.Discovery
177253 }
254+
178255 Write-Output " $ ( Time- Stamp) Completed Script!"
179256}
0 commit comments