Testing the Integrity of a Management Pack   Leave a comment

When I’m creating a new management pack, I want to be sure that the management pack is valid before I import it. I know that if it’s wrong, the system won’t import it, but I generally like to know these things before I try. In order to do that, I wrote a pretty simple script to test the management pack. It takes advantage of the Verify method on management pack object. This verification does a number of things. First, it checks to be sure that there are no XSD validation errors. If there are, the method throws an exception. The Verify method also checks to be sure that references are correct and present. Note that there may still be some errors that are found upon import, but most of the issues will be caught by this script.

When everything works right, you’ll see something like the following:

PS> test-managementpack .\PowerShell.WATest.xml|ft Verified,Name,FullName -au

Verified Name              FullName
-------- ----              --------
True     PowerShell.WATest C:\Program Files\System Center Management Packs\PowerShell.WATest.xml

However, if something is busted, you’ll see this:

PS> test-managementpack .\BigHonkingMP.xml|Ft Verified,Name,FullName -au

Verified Name         FullName
-------- ----         --------
False    BigHonkingMP C:\Program Files\System Center Management Packs\BigHonkingMP.xml

The results of test-managementpack include the error, you can see what happened, by getting the Error property:

PS> test-managementpack .\BigHonkingMP.xml|Fl name,error


Name  : BigHonkingMP
Error : {Exception calling "Verify" with "0" argument(s): "Verification failed with 1 errors:
        -------------------------------------------------------
        Error 1:
        : Failed to verify class: BigHonkingMP.ConcreteMicroBleh
        Host class BigHonkingMP.AbstractBleh and hosted class BigHonkingMP.ConcreteMicroBleh define the same set of key properties.
        -------------------------------------------------------
        "}

You can also pipe files at test-managementpack, so you can use it like this:

PS> ls *.xml|test-managementpack

Verified Name                                           FullName
-------- ----                                           --------
False    BigHonkingMP                                   C:\Program Files\System Center Management Packs\BigHonkingMP.xml
. . .

Which I thought was pretty handy – going through a bunch of MPs at once.

Here’s the script, it relies on version 2.0 of PowerShell. I’ve added online help, so you can run get-help test-managementpack to get info on it. The business end of the script is in line 29 and 30. That’s where I create a management pack object and then call verify. If the management pack object can’t be created, or if it fails verify, the exception gets caught and then the script builds up an error message. Rather than using Write-Error to indicate the problem, I decided to include the error message in resultant object. I did this because I didn’t really want the error output at this point, but I wanted to hang on to the verify failure.

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
#requires -version 2.0

param ( 
    [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
    [string[]]$fullname 
    )

BEGIN
{
# GLOBAL REFERENCES
# $SMDIR
$NS = "Microsoft.EnterpriseManagement"
[reflection.assembly]::LoadWithPartialName("${NS}.Core")|out-null
$EMG = new-object "${NS}.EnterpriseManagementGroup" localhost
$MPSType = "${NS}.Configuration.IO.ManagementPackFileStore"
$MPType  = "${NS}.Configuration.ManagementPack"
}

PROCESS
{
    $fullname | %{ 
        $path = (resolve-path $_).path
        $FInfo = [io.fileinfo]$path
        $dir = $FInfo.DirectoryName
        $fil = $FInfo.FullName
        $error.clear()
        try
        {
            $MP = new-object $MPType $fil,$EMG
            $MP.Verify() 
        }
        catch { ; }
        if ( $error.count ) 
        { 
            $Verified = $false 
            $msgs = $error|%{$_.Exception;$_.Exception.InnerException}
            $ErrorMessage = $msgs -join "`n"
        } 
        else 
        { 
            $ErrorMessage = "No Errors" 
            $Verified = $true 
        }
        $PSO = new-object PSObject
        $PSO.psobject.typenames[0]=  "Microsoft.EnterpriseManagement.Configuration.ManagementPack.CustomVerification"
        $PSO | add-member NoteProperty Verified $Verified
        $PSO | add-member NoteProperty Name $MP.Name
        $PSO | add-member NoteProperty FullName $fil
        $PSO | add-member NoteProperty Error $error.clone()
        $PSO | add-member NoteProperty ErrorMessage $ErrorMessage
        $PSO
    }
}

<#
.SYNOPSIS
    Verify the integrity of a management pack
.DESCRIPTION
    The cmdlet attempts to create a management pack object based on the
    provided file and then calls the verify method to determine whether
    the management pack is valid.
.PARAMETER fullname
    The fullname of the management pack
.EXAMPLE
ls wf*.xml|test-ManagementPack
Verified Name FullName
——– —- ——–
True WF.NoWorkflow C:\Program Files\System Center Management Packs\wf.NoWorkflow.xml
True WF.Simple C:\Program Files\System Center Management Packs\wf.Simple.xml
True WF.SingleCmdTask C:\Program Files\System Center Management Packs\wf.SingleCmdTask.xml
True WF.SingleTask C:\Program Files\System Center Management Packs\wf.SingleTask.xml

.INPUTS
    Output from get-childitem
    Any object which has a fullname property which represents a management pack
.OUTPUTS
    A custom object which contains the following:
        Verified – A boolean indicating whether the MP was verified
        Name – The name of the management pack
        FullName – The path to the management pack
        Error – Errors produced while verifying the management pack
.LINK
    Export-ManagementPack
    Get-ManagementPack
    Import-ManagementPack
    New-ManagementPack
    New-SealedManagementPack
    Remove-ManagementPack

#>

Advertisements

Posted May 12, 2010 by jtruher3 in ServiceManager

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: