List Management Pack’s Dependencies – PowersHell Script

I had a neat part in my UninstallMP script that I thought might be useful as a standalone version.

This script takes 2 Arguments:

– RMS (FQDN of the Root Management Server)

– MPName (Management Pack Name)

Example: ListMPDenedencies “rms.code4ward.net” “Microsoft.Windows.Library”

 

It will then get the Management Pack and check which Management Packs rely on it. Additionally it will query each of these Management Packs for dependencies as well, creating Tree View.

image

Find the PS1 file attached below.

 

The Code:

# Author: Andreas Zuckerhut
# This Script will take a Management Pack and gets all Management Packs that rely on it.

#Parameters:
#Param1 – RMS = The FQDN of the Root Management Server
#Param2 – MPName = The full MPName

#Example: ListMPDependencies.ps1 “rms.code4ward.net” “MyCustom.ManagementPack”
param([string]$rms,[string]$mpName)

function Main()
{
    Write-Host “—————————–“
    Write-Host “Received Arguments:rnRMS = $rms,rnMPName = $mpName”
    Write-Host “—————————–“
    if (Check-Parameters)
    {
        # Load the Operations Manager Environment
        Load-OperationsManagerEnvironment($rms)
        # Query for all Management Packs that are currently installed in your Management Group
        # We need that list for the calculation of the Dependencies.
        $Script:MPFullQuery = get-managementpack
        Write-Host “——————————————–“
        if ($mpName.Length -gt 0)
        {   
            # Get the Management Pack with the MPName that was provided.
            $MP = get-managementpack | ?{$_.Name -eq $mpName}
            $MPName = $MP.name
            # Output the MP that was found.
            if ($MP -eq $null)
            {
                Write-Host “Management Pack with ID $CurrentMPName was not found.”
            }
            else
            {
                Write-Host “Management Pack $MPName was found.”
                Write-Host “——————————————–“
                Write-Host $MPName
                Get-MPReferences $mpName 0
                Write-Host “——————————————–“
            }
        }
    }
}

function Get-MPReferences([string]$mpName,[int]$level)
{   
    # Create a Collection for all Referencing MP Names
    $ReferencingMPNames = New-Object System.Collections.ArrayList
    # For each Level we create a new Prefix for the Dependency Output.
    # Basically, per Level we need to add some Spacing.
    $CurrentLevel = 0
    [string]$Prefix = “+—“
    while ($CurrentLevel -lt $level)
    {
        $Prefix = “|   ” + $Prefix
        $CurrentLevel = $CurrentLevel + 1
    }
    # Raise the Level by one.
    $level = $level + 1
    # We check each MP in the Management Group if it refers to our MP
    foreach ($MPToCheck in $Script:MPFullQuery)
    {
        foreach ($Reference in $MPToCheck.References)
        {
            if ($Reference.Name -eq $mpName)
            {
                # Additionally we add it to the current MPs List
                $ReferencingMPNames.Add($MPToCheck.Name) | Out-Null
            }
        }
    }
    # We check all the Dependencies of the current MP
    foreach ($ReferencingMPName in $ReferencingMPNames)
    {
        #Output the Dependencies
        Write-Host “$Prefix$ReferencingMPName”
        # Afterwards we check this MPs Dependencies.
        Get-MPReferences $ReferencingMPName $level
    }   
}

function Check-Parameters
{
    # Verify that all needed Parameters were provided.
    if ($rms.Length -eq 0 )
    {
        Write-Host “The Root Management Server Parameter was not provided.”
        return $false
    }
    if ($mpName.Length -eq 0)
    {
        if ($mpPartialName.Length -eq 0)
        {
            Write-Host “The Management Pack Name was not provided.”
            Write-Host “Example: ListMPDependencies.ps "rms.code4ward.net"MyCustom.ManagementPack.For.Something.Awesome“”
            return $false
        }
        else
        {
            return $true
        }
    }
    else
    {
        return $true
    }
}

function Load-OperationsManagerEnvironment([string]$rms)
{
    #Check if PSSnapin is already loaded, if not, load it.
    if ((Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.EnterpriseManagement.OperationsManager.Client’}) -eq $null)
    {
        Write-Host(“Loading Operations Manager PS Snapin”)
        Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client    
    }
    else
    {
        Write-host(“Operations Manager PS Snapin is already loaded”)
    }
    # Load the Assemblies
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.EnterpriseManagement.OperationsManager”)
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.EnterpriseManagement.OperationsManager.Common”)
    # Set Location to the RMS
    Write-Host(“Setting Location $rms.”)
    Set-Location “OperationsManagerMonitoring::” -ErrorVariable errSnapin;
    $MG = New-ManagementGroupConnection -ConnectionString:$rms -ErrorVariable errSnapin;   
    $Script:MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($rms)
    Set-Location $rms -ErrorVariable errSnapin;   
}

# Call the Main function.
Main

Leave a Reply