Home > Scripting, VMware > Require a minimum version of PowerCLI

Require a minimum version of PowerCLI

February 24th, 2010 Eric Leave a comment Go to comments

A lot of the script that I have been writing rely on new cmdlets or properties in the current release of PowerCLI. Telling people to upgrade before running the scripts has not been enough so I am going to do the smart thing and check that the appropriate version is running.

Here is the code to that I am using to do the validation.

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
# By default this will check that the running version of PowerCLI is greater
# than or equal to the specified version and throw an exception if not.
# For an exact match specify the -exact parameter.
function Require-PowerCliVersion
{
    param
    (
        [Int32]$build = $(throw "Require-PowerCliVersion: No build number specified."),
        [switch]$exact # If true requires the exact build number to match.
    )
 
    $passed = $false
 
    if (Get-Command Get-PowerCliVersion -ErrorAction SilentlyContinue)
    {
        $cliBuild = (Get-PowerCliVersion).Build
 
        if ($exact)
        {
            if ($build -eq $cliBuild)
            {
                $passed = $true
            }
        }
        elseif ($cliBuild -ge $build)
        {
            $passed = $true
        }
    }
 
    if (!$passed)
    {
        throw "Require-PowerCliVersion: Minimum PowerCLI version requirement not met."
    }
}
Categories: Scripting, VMware Tags:
  1. No comments yet.
  1. No trackbacks yet.

Switch to our mobile site