Development Management Pack PowerShell Troubleshooting

PowerShell – Comparing Management Pack Version

Programming

I bumped into an interesting problem while writing a custom PowerShell module. The module should compare the management packs from a SCOM management group and some local repository. If the local repository has a higher version number than the currently installed management pack, the script should import the management pack into SCOM.

This post should show you how to compare the management pack version properly.

Let’s make an example:

# Get one management pack just for demo
$MP = Get-SCOMManagementPack | Where-Object -FilterScript {
$_.Name -eq 'Microsoft.SystemCenter.Visualization.Component.Library'
}
# Set some fake version value
$VersionCompare = '10.0.1.0'
Write-Host -ForegroundColor Cyan "Version demo value is of type: $($VersionCompare.GetType().Name)"
# Get the management pack version of the demo MP
$MPVersion = $MP.Version
Write-Host -ForegroundColor Cyan "Version of management pack is of type: $($MPVersion.GetType().Name)"
# Compare MP version agianst demo value
If($MPVersion -lt $VersionCompare)
{
Write-Host -ForegroundColor Yellow -Object "$($MP.Name) - $MPVersion is lower than $VersionCompare)"
}
Elseif ($MPVersion  -gt $VersionCompare)
{
Write-Host -ForegroundColor Green -Object "$($MP.Name) - $MPVersion is greater than $VersionCompare)"
}

This will give you an output like this…

image

…as you can see the version is figured out properly, although the $VersionCompare variable is of type string.

There might be cases where you build a custom object and define a management pack version property. The property will be added as type of string. We will simulate this by converting the $MPVersion value by changing this line…

$MPVersion = $MP.Version

to…

[string]$MPVersion = $MP.Version

…the output will be wrong…

image

…because there is a string comparison which simply does not work.

To avoid any wrong result we need to convert both values into the correct data type. .Net provides a System.Version class which will exactly do what we want. We need to change these lines…

$VersionCompare = '10.0.1.0'
[string]$MPVersion = $MP.Version

to …

[Version]$VersionCompare = '10.0.1.0'
[Version][string]$MPVersion = $MP.Version

or just more simple and recommended…

[Version]$VersionCompare = '10.0.1.0'
[Version]$MPVersion = $MP.Version

…then the output will be like this…

image

You might have experienced that PowerShell has some magic in place and tries always to figure out what the best / correct data type is. This makes PowerShell very handy and it is easy to get started with. But as soon you try to build more advanced scripts it is recommend that you define what data type you need as you do it in “real” / strong typed programming languages like C, C#, Java etc..

I hope this helps you building your solution.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.