Public/Remove-SCOMObsoleteReferenceFromMPFile.ps1
|
Function Remove-SCOMObsoleteReferenceFromMPFile { <# .Synopsis Will remove obsolete aliases from unsealed management packs. .DESCRIPTION This will not alter the original file but rather will output modified versions to the designated output folder. .EXAMPLE PS C:\> Remove-SCOMObsoleteReferenceFromMPFile -inFile 'C:\Unsealed MPs\*.xml' -outDir 'C:\Usealed MPs\Modified MPs' .EXAMPLE PS C:\> Remove-SCOMObsoleteReferenceFromMPFile -inFile 'C:\Unsealed MPs\MyOverrides.xml' -outDir 'C:\Usealed MPs\Modified MPs' .EXAMPLE PS C:\> Remove-SCOMObsoleteReferenceFromMPFile -inFile (GetChildItem 'C:\Unsealed MPs\*.xml').FullName .EXAMPLE PS C:\> Get-ChildItem -Path 'C:\Unsealed MPs\*.xml' | Remove-SCOMObsoleteReferenceFromMPFile .NOTES Author: Tyson Paul Blog: https://monitoringguys.com/ Version: 1.0 Date: 20018.05.09 #> [CmdletBinding()] Param ( # Path to input file(s) (YourUnsealedPack.xml) [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Alias("FullName")] [string[]]$inFile, # Path to output folder, to save modified .xml file(s) [Parameter(Mandatory=$false, ValueFromPipeline=$false, Position=1)] [string]$outDir ) Begin{} Process{ [System.Object[]]$paths = (Get-ChildItem -Path $inFile ) ForEach ($FilePath in $paths){ If (-not [bool]$outDir){ $outDir = (Join-Path (Split-Path $FilePath -Parent) "MODIFIED") } If (-NOT (Test-Path -Path $outDir) ){ New-Item -Path $outDir -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } Write-Host "Input File: " -NoNewline; Write-Host $FilePath -F Cyan Try{ [xml]$xml = Get-Content -Path $FilePath $References = $xml.ManagementPack.Manifest.References }Catch{ Continue } If (-NOT [bool]$References) {Continue} $References.SelectNodes("*") | ForEach-Object { # If neither the Alias or ID of the referenced MP is found within the body of the MP, remove the reference. If (-NOT (($xml.InnerXml -match ("$($_.Alias)!")) -or ($xml.InnerXml -match ("$($_.ID)!"))) ) { Write-Host "Obsolete Reference Removed: $($_.Alias) : $($_.ID)" -F Green $References.RemoveChild($_) | Out-Null } } $outFile = (Join-Path $outDir (Split-Path $FilePath -Leaf)) Write-Host "Saving to file: " -NoNewline; Write-Host $outFile -F Yellow $Xml.Save($outFile) } } End{} } |