Skip to content

Commit 2450350

Browse files
authored
Major Update
1 parent 89b4082 commit 2450350

File tree

1 file changed

+73
-53
lines changed

1 file changed

+73
-53
lines changed

Powershell/Remove-MPDependencies.ps1

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
# THIS SCRIPT CAN BE DANGEROUS, MAKE BACKUPS OF YOUR OPSDB AND DW!
1+
# THIS SCRIPT CAN BE DANGEROUS, MAKE BACKUPS OF YOUR OPSDB AND DW! OR RUN WITH THE '-PauseOnEach' PARAMETER
22

3-
# PowerShell script to automagically remove an MP and its dependencies.
3+
# PowerShell script to automatically remove an MP and its dependencies.
44
#
55
# Original Author : Christopher Crammond, Chandra Bose
66
# Modified By : Blake Drumm (blakedrumm@microsoft.com)
77
# Date Created : April 24th, 2012
8-
# Date Modified : July 19th, 2022
8+
# Date Modified : July 19th, 2021
99
#
10-
# Version : 2.0.6
10+
# Version : 2.0.7
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

14-
<# NOTE:
15-
16-
Added ability to remove XML Reference from Unsealed Management Packs - August 19th, 2021
14+
<#
15+
Updated - August 19th, 2021:
16+
Added ability to remove XML Reference from Unsealed Management Packs
1717
18+
Updated - July 19th, 2022:
19+
Attempted to fix some issues with processing MP Elements
1820
#>
1921

2022
# Example:
23+
# .\Remove-MPDependencies -ManagementPackName "*APM*"
24+
#
25+
# .\Remove-MPDependencies.ps1 -ManagementPackName Microsoft.Azure.ManagedInstance.Discovery
26+
#
2127
# Get-SCOMManagementPack -DisplayName "Microsoft Azure SQL Managed Instance (Discovery)" | .\Remove-MPDependencies.ps1 -DryRun -PauseOnEach
2228
#
2329
# Get-SCOMManagementPack -DisplayName "Microsoft Azure SQL Managed Instance (Discovery)" | .\Remove-MPDependencies.ps1
24-
#
25-
# .\Remove-MPDependencies.ps1 -ManagementPackName Microsoft.Azure.ManagedInstance.Discovery
2630

2731
# Needed for SCOM SDK
2832
param
@@ -68,19 +72,19 @@ function Remove-MPDependencies
6872
$ExportPath = $env:TEMP
6973
}
7074
if (!(Test-Path $ExportPath))
71-
{
72-
New-Item -ItemType Directory -Path $ExportPath | Out-Null
73-
}
75+
{
76+
New-Item -ItemType Directory -Path $ExportPath | Out-Null
77+
}
7478
if (!$ManagementPackName -and !$ManagementPackId)
7579
{
7680
Write-Host "-ManagementPackName or -ManagementPackId is required!" -ForegroundColor Red
7781
set-location $cwd
7882
break
7983
}
80-
if ($ManagementPackName -contains "*")
81-
{
82-
$ManagementPackName = Get-SCManagementPack -Name $ManagementPackName | Select-First 1
83-
}
84+
if ($ManagementPackName -contains "*")
85+
{
86+
$ManagementPackName = Get-SCManagementPack -Name $ManagementPackName | Select-First 1
87+
}
8488
$ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
8589
$rootMS = "{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName
8690

@@ -125,49 +129,61 @@ function Remove-MPDependencies
125129
}
126130
elseif ($mpPresent.Sealed -eq $false)
127131
{
128-
Write-Host " * Removing reference from " -NoNewLine
129-
Write-Host $($mp.Name) -ForegroundColor Cyan
130-
try
132+
foreach ($arg in $firstArgName)
131133
{
132-
[array]$removed = $null
133-
[array]$alias = $null
134-
$mpPresent | Export-SCOMManagementPack -Path $ExportPath -PassThru -ErrorAction Stop | Out-Null
135-
$xmldata = [xml](Get-Content "$ExportPath`\$($mpPresent.Name).xml" -ErrorAction Stop);
136-
$xmlData.Save("$ExportPath`\$($mpPresent.Name).backup.xml")
137-
[version]$mpversion = $xmldata.ManagementPack.Manifest.Identity.Version
138-
$xmldata.ManagementPack.Manifest.Identity.Version = [version]::New($mpversion.Major, $mpversion.Minor, $mpversion.Build, $mpversion.Revision + 1).ToString()
139-
$xmlData.ChildNodes.Manifest.References.Reference | Where-Object { $_.ID -eq $firstArgName } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $aliases += $_.Alias; [void]$_.ParentNode.RemoveChild($_); }
140-
141-
foreach ($alias in $aliases)
142-
{
143-
$xmlData.ChildNodes.Monitoring.Overrides.SecureReferenceOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id = $_.Id; [void]$_.ParentNode.RemoveChild($_) }
144-
$xmlData.ChildNodes.Monitoring.Overrides.MonitorPropertyOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id = $_.Id; [void]$_.ParentNode.RemoveChild($_) }
145-
$xmlData.ChildNodes.Monitoring.Overrides.DiscoveryConfigurationOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id += $_.Id; [void]$_.ParentNode.RemoveChild($_) }
146-
$xmlData.ChildNodes.Monitoring.Overrides.RulePropertyOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id += $_.Id; [void]$_.ParentNode.RemoveChild($_) }
147-
}
148-
149-
foreach ($identifer in $id)
134+
Write-Host " * Removing " -NoNewline
135+
Write-Host $arg -ForegroundColor Green -NoNewline
136+
Write-Host " reference from " -NoNewLine
137+
Write-Host $($mp.Name) -ForegroundColor Cyan
138+
try
150139
{
151-
$xmlData.ChildNodes.LanguagePacks.LanguagePack.DisplayStrings.DisplayString | Where-Object { $_.ElementID -eq $identifer } | ForEach-Object { $removed += $_.ParentNode.InnerXML; [void]$_.ParentNode.RemoveChild($_) }
140+
[array]$removed = $null
141+
[array]$alias = $null
142+
$mpPresent | Export-SCOMManagementPack -Path $ExportPath -PassThru -ErrorAction Stop | Out-Null
143+
$xmldata = [xml](Get-Content "$ExportPath`\$($mpPresent.Name).xml" -ErrorAction Stop);
144+
Write-Host " * Backing up to:" -ForegroundColor Green -NoNewline
145+
Write-Host "$ExportPath`\$($mpPresent.Name).backup.xml" -ForegroundColor Cyan
146+
$xmlData.Save("$ExportPath`\$($mpPresent.Name).backup.xml")
147+
[version]$mpversion = $xmldata.ManagementPack.Manifest.Identity.Version
148+
$xmldata.ManagementPack.Manifest.Identity.Version = [version]::New($mpversion.Major, $mpversion.Minor, $mpversion.Build, $mpversion.Revision + 1).ToString()
149+
$aliases = $null
150+
$xmlData.ChildNodes.Manifest.References.Reference | Where-Object { $_.ID -eq $arg } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $aliases += $_.Alias; [void]$_.ParentNode.RemoveChild($_); }
151+
foreach ($alias in $aliases)
152+
{
153+
Write-Host " * Alias found: $alias"
154+
$xmlData.ChildNodes.Monitoring.Overrides.SecureReferenceOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id = $_.Id; [void]$_.ParentNode.RemoveChild($_) }
155+
$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($_) }
156+
$xmlData.ChildNodes.Monitoring.Overrides.DiscoveryConfigurationOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id += $_.Id; [void]$_.ParentNode.RemoveChild($_) }
157+
$xmlData.ChildNodes.Monitoring.Overrides.RulePropertyOverride | Where-Object { $($_.Context -split '!')[0] -eq $alias } | ForEach-Object { $removed += $_.ParentNode.InnerXML; $id += $_.Id; [void]$_.ParentNode.RemoveChild($_) }
158+
}
159+
160+
foreach ($identifer in $id)
161+
{
162+
$xmlData.ChildNodes.LanguagePacks.LanguagePack.DisplayStrings.DisplayString | Where-Object { $_.ElementID -eq $identifer } | ForEach-Object { $removed += $_.ParentNode.InnerXML; [void]$_.ParentNode.RemoveChild($_) }
163+
}
164+
Write-Host " * Removed the following XML Data from the MP: " -NoNewline
165+
Write-Host $($mp.Name) -ForegroundColor Cyan
166+
$removed
167+
168+
Write-Host " * Saving copy to: " -NoNewline -ForegroundColor Green
169+
Write-Host "$ExportPath`\$($mpPresent.Name).xml" -ForegroundColor Cyan
170+
$xmlData.Save("$ExportPath`\$($mpPresent.Name).xml")
171+
Write-Host " * Importing MP: " -NoNewLine
172+
Write-Host $($mpPresent.Name) -ForegroundColor Cyan
173+
Import-SCOMManagementPack -FullName "$ExportPath`\$($mpPresent.Name).xml" | Out-Null
174+
Write-Host " * Imported modified Management Pack: " -NoNewLine
175+
Write-Host $($mpPresent.Name) -ForegroundColor Cyan
152176
}
153-
Write-Host " * Removed the following XML Data from the MP: " -NoNewline
154-
Write-Host $($mp.Name) -ForegroundColor Cyan
155-
$removed | Foreach { $_ }
156-
157-
$xmlData.Save("$ExportPath`\$($mpPresent.Name).xml")
158-
Import-SCOMManagementPack -FullName "$ExportPath`\$($mpPresent.Name).xml" | Out-Null
177+
catch
178+
{ Write-Warning $_ }
159179
}
160-
catch
161-
{ Write-Warning $_ }
162-
Write-Host " * Imported modified Management Pack: " -NoNewLine
163-
Write-Host $($mp.Name) -ForegroundColor Cyan
164180
#pause
165181
}
166182
else
167183
{
168184
Write-Host " * Backing up Sealed Management Pack: " -NoNewLine -ForegroundColor Green
169-
Write-Host $($mp.Name) -ForegroundColor Cyan
170-
$mpPresent | Export-SCOMManagementPack -Path $ExportPath -PassThru -ErrorAction Stop
185+
Write-Host "$ExportPath`\$($mp.Name).xml" -ForegroundColor Cyan
186+
$mpPresent | Export-SCOMManagementPack -Path $ExportPath -PassThru -ErrorAction Stop | Out-Null
171187
Write-Host " * Uninstalling Sealed Management Pack: " -NoNewLine -ForegroundColor Red
172188
Write-Host $($mp.Name) -ForegroundColor Cyan
173189
Uninstall-ManagementPack -managementpack $mp
@@ -232,8 +248,8 @@ function Remove-MPDependencies
232248
#
233249
if ($ManagementPackName -like "*")
234250
{
235-
$firstArgName = Get-SCManagementPack -Name $ManagementPackName | Select-Object Name -ExpandProperty Name
236-
}
251+
$firstArgName = (Get-SCManagementPack -Name $ManagementPackName).Name
252+
}
237253
elseif ($ManagementPackName -like "*,*")
238254
{
239255
$firstArgName = ($ManagementPackName.Split(",").Split("["))[1]
@@ -282,6 +298,10 @@ if ($ManagementPackName -or $ManagementPackId -or $PauseOnEach -or $DryRun -or $
282298
else
283299
{
284300
# Example:
285-
# Remove-MPDependencies -ManagementPackName "*APM*" -ExportPath C:\Users\omadmin\Desktop\Backup
301+
# Remove-MPDependencies -ManagementPackName "*APM*" -ExportPath C:\MPBackups
302+
# or
303+
# Remove-MPDependencies -ManagementPackName "*APM*"
304+
# or
305+
# Remove-MPDependencies -ManagementPackName Microsoft.Azure.ManagedInstance.Discovery
286306
Remove-MPDependencies
287307
}

0 commit comments

Comments
 (0)