Function Get-ObsoleteReferences ([XML]$MPXML,[System.Collections.ArrayList]$arrCommonMPs)
{
$arrAliasToBeDelete = New-Object System.Collections.ArrayList
$Refs = $MPXML.ManagementPack.Manifest.References.Reference
Foreach ($Ref in $Refs)
{
$RefMPID = $Ref.ID
$Alias = $Ref.Alias
$strRef = "$Alias`!"
If (!($MPXML.Innerxml.contains($strRef)))
{
#The alias is obsolete
#Ignore Common MPs
If (!($arrCommonMPs.contains($RefMPID)))
{
#Referecing MP is not a common MP
[Void]$arrAliasToBeDelete.Add($Alias)
}
}
}
,$arrAliasToBeDelete
}
#Create ManagementPackXMLWriter object if backup is required
If ($BackupBeforeModify -eq $true)
{
If (Test-Path $BackupLocation)
{
$date = Get-Date
$BackupSubDir = "$($date.day)-$($date.month)-$($date.year) $($date.hour)`.$($date.minute)`.$($date.second)"
$BackupDir = Join-Path $BackupLocation $BackupSubDir
} else {
Write-Host "Invalid Backup Location specified."
Exit
}
}
#Get all unsealed MPs
Write-Host "Getting Unsealed management packs..."
$strMPquery = "Sealed = 'false'"
$mpCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackCriteria($strMPquery)
$arrMPs = $MG.GetManagementPacks($mpCriteria)
Write-Host "Total number of unsealed management packs: $($arrMPs.count)"
Write-Host ""
$iTotalUpdated = 0
Foreach ($MP in $arrMPs)
{
Write-Host "Checking MP: '$($MP.Name)'..." -ForegroundColor Green
#Firstly, get the XML
$MPXML = Get-MpXmlString $MP
#Then get obsolete references (if there are any)
$arrRefToDelete = Get-ObsoleteReferences $MPXML $arrCommonMPs
If ($arrRefToDelete.count -gt 0)
{
Write-Host " - Number of obsolete references found: $($arrRefToDelete.count)" -ForegroundColor Yellow
If ($BackupBeforeModify -eq $true)
{
If ($WhatIf -eq $false)
{
#Create Backup Dir if it's not present
if (!(Test-path $BackupDir))
{
New-Item -type directory -Path $BackupDir | Out-Null
}
#Create mpwriter if it's not created
If (!$mpwriter)
{
$mpWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter($BackupDir)
}
Write-Host " - Backing up $($MP.Name) to $BackupDir before modifying it."
$mpWriter.WriteManagementPack($MP) | Out-Null
} else {
Write-Host " - $($MP.Name) would have been backed up to $BackupDir before modifying it."
}
}
Foreach ($item in $arrRefToDelete)
{
If ($WhatIf -eq $false)
{
Write-Host " - Deleting reference '$item'"
$MP.References.remove($item) | out-Null
} else {
Write-Host " - The reference '$item' would have been deleted."
}
}
#calculate the new version number if $IncrementVersion switch is specified
if ($IncrementVersion -eq $true)
{
$CurrentVersion = $MP.Version.Tostring()
$NewVersion = Increase-Version $CurrentVersion
}
If ($WhatIf -eq $false)
{
If ($IncrementVersion -eq $true)
{
Write-Host " - Updating MP Version from $CurrentVersion to $NewVersion"
$MP.Version = $NewVersion
}
#accept changes if MP is verified. otherwise reject changes
If ($bVerified -eq $false)
{
Write-Host "MP Verify failed. Reject changes." -ForegroundColor Red
$MP.RejectChanges()
} else {
Write-Host "MP Verified. Accepting changes."
$MP.AcceptChanges()
#Increase total updated count only when changes are accepted.
$iTotalUpdated ++
}
} else {
If ($IncrementVersion -eq $true)
{
Write-Host " - The MP version would have been updated from $CurrentVersion to $NewVersion."
$iTotalUpdated ++
}
}
}
Write-Host ""
}
If ($WhatIf -eq $true){
Write-host "Total number of unsealed management packs would have been updated: $iTotalUpdated" -ForegroundColor Green
} else {
Write-host "Total number of unsealed management packs have been updated: $iTotalUpdated" -ForegroundColor Green
}