Private/Test-VersionString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function Test-VersionString {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]$Value,
        [switch]$AllowLatest,
        [switch]$ThrowOnFail
    )

    # conditionally allow the 'latest' string
    if ($AllowLatest -and $Value -eq 'latest') { return $true }

    if (-not ($Value -as [Version])) {
        if ($ThrowOnFail) {
            throw [Management.Automation.ValidationMetadataException] "Value must be a valid version string (1.0, 1.7.2, 2.5, etc)."
        }
        return $false
    }

    return $true
}