Scripts/get-mpbinfo.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#requires -version 2.0
# required because of the way we use new-object
# Get-MPBInfo.ps1
# Retrieve management pack and resource information from a .MPB file
param ( $file, $computername = "localhost", [switch]$mpinfo, [switch]$dumpStream )
$PACKDLL = "Microsoft.EnterpriseManagement.Packaging"
$FSTYPE  = "Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore"
$BUNDLET = "Microsoft.EnterpriseManagement.Packaging.ManagementPackBundleFactory"
$pkgasm = [reflection.assembly]::LoadWithPartialName($PACKDLL)
if ( ! $pkgasm ) { throw "Can't load packaging dll" }
# get a bundlefactory type
$BFACTORYT = $pkgasm.GetType($BUNDLET)
# create a bundle reader
# create a filestore, which is used by the bundlereader
# and then read the bundle
$br = $BFACTORYT::CreateBundleReader()

$files = resolve-path $file

foreach($mpfile in $files)
{
    $path = $mpfile.path
    if ( ! $path ) { throw "Could not find '$file'" }
    $fs = new-object $FSTYPE
    $mpb = $br.Read($path,$fs)
    $p = [io.path]::GetFileName($path)
    # for each managementpack, get the resources and create a custom object
    $mpb.ManagementPacks|%{
        $theMP = $_
        $ManagementPack = $_.name
        $Version = $_.Version
        $KeyToken = $_.KeyToken
        # keep track of whether the MP is sealed or no
        if ( $_.Sealed ) { $Sealed = "Sealed" } else { $Sealed = "Not Sealed" }
        if ( $mpb.GetStreams($_).count -gt 0 -and ! $mpinfo )
        {
            $mpb.GetStreams($_) |%{ 
                $hadStreams = $true
                $streams = $_
                # retrieve the keys and create a custom object which we'll use
                # in formatting
                $streams.keys | %{
                    $ResourceName = $_
                    $Length = $streams.Item($ResourceName).Length
                    # this emits a custom object which can then be used with
                    # PowerShell formatting
                    # Get-MPBInfo <file>|ft -group ManagementPack Length,ResourceName
                    if ( $sealed -eq "sealed" ) { $s = $true } else { $s = $false }
                    new-object -type psobject -prop @{
                        MP             = $theMP
                        ManagementPack = "$ManagementPack"
                        Length         = $Length
                        Sealed         = $s
                        ResourceName   = $ResourceName
                        MPBFile        = $p
                        }
                    }
                }
        }
        else
        {
            if ( $sealed -eq "sealed" ) { $s = $true } else { $s = $false }
            new-object -type psobject -prop @{
                MP             = $_
                ManagementPack = "$ManagementPack"
                Sealed         = $s
                MPBFile        = $p
                }
        }
    }
    $fs.dispose()
}