Private/Assert-VersionRequirement.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Function Assert-VersionRequirement {
    <#
.SYNOPSIS
Affirms that a version satisfies a required level

.DESCRIPTION
Throws an error if a provided version number odes not meet or exceed a required level.

.PARAMETER ExternalVersion
A version number to comapre against the required version

.PARAMETER RequiredVersion
The version number the ExternalVersion Number must meet.

.PARAMETER MaximumVersion
The version number the ExternalVersion Number cannot exceed.

.EXAMPLE
Assert-VersionRequirement -ExternalVersion 1.0 -RequiredVersion 2.0

Throws an error as 1.0 does not equal or exceed 2.0

.EXAMPLE
Assert-VersionRequirement -ExternalVersion 1.0 -RequiredVersion 0.5

Returns nothing as 1.0 exceeds 0.5

.NOTES
General notes
#>

    [CmdletBinding()]
    Param(
        # The Current Software Version
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [System.Version]
        $ExternalVersion = $Script:ExternalVersion,

        # The Required Software Version
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [System.Version]
        $RequiredVersion,

        # The Maximum Supported Software Version
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [System.Version]
        $MaximumVersion
    )

    Begin {

        $ParentFunction = $((Get-ParentFunction).FunctionName)
        $UsedParameterSet = $((Get-ParentFunction).ParameterSetName)

    }

    Process {

        switch ($PSBoundParameters.Keys) {

            'RequiredVersion' {

                If (-not (Compare-MinimumVersion -Version $ExternalVersion -MinimumVersion $RequiredVersion)) {

                    Throw "CyberArk $ExternalVersion does not meet the minimum version requirement of $RequiredVersion for $ParentFunction (using ParameterSet: $UsedParameterSet)"

                }

                Continue

            }
        
            'MaximumVersion' {

                If (-not (Compare-MaximumVersion -Version $ExternalVersion -MaximumVersion $MaximumVersion)) {

                    Throw "CyberArk $ExternalVersion exceeds the maximum supported version of $MaximumVersion for $ParentFunction (using ParameterSet: $UsedParameterSet)"

                }

                Continue

            }

        }

    }

    End { }

}