Private/Initialize-CallVars.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
function Initialize-CallVars
{
    [CmdletBinding()]
    param(
        [Alias('host')]
        [string]$WAPIHost,
        [Alias('version')]
        [string]$WAPIVersion,
        [PSCredential]$Credential,
        [Alias('session')]
        [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession,
        [switch]$IgnoreCertificateValidation
    )

    # Rather than putting this logic in every function that uses it,
    # we'll consolidate it in a private function here that everything
    # else can use.

    # We're essentially building an $APIBase and $WebSession object
    # out of the existing saved module variables and the explicit
    # parameters from the function call. Explicit parameters always
    # override saved module variables.

    # let the saved module variables pass through if explicit the
    # explicit parameters are not defined
    if ([String]::IsNullOrWhiteSpace($WAPIHost)) {
        $WAPIHost = $script:WAPIHost
    }
    if ([String]::IsNullOrWhiteSpace($WAPIVersion)) {
        $WAPIVersion = $script:WAPIVersion
    }
    else {
        # sanity check the version string

        # strip the 'v' prefix if they used it on accident
        if ($WAPIVersion[0] -eq 'v') {
            $WAPIVersion = $WAPIVersion.Substring(1)
        }

        # auto-parse it using the [Version] cast
        [Version]$WAPIVersion | Out-Null
    }
    if (!$Credential) {
        $Credential = $script:Credential
    }
    if (!$WebSession) {
        $WebSession = $script:WebSession
    }

    # build the APIBase URL
    $APIBase = "https://$WAPIHost/wapi/v$WAPIVersion/"

    # build a valid WebSession
    if ($WebSession) {
        if ($Credential) {
            # override the credential embedded in the existing WebSession
            # if it's empty or doesn't match the username
            if (!($WebSession.Credentials -and 
                $WebSession.Credentials.UserName -eq $Credential.GetNetworkCredential().UserName)) {
                Write-Verbose "overriding WebSession.Credentials"
                $WebSession.Credentials = $Credential.GetNetworkCredential()
            }
        }
    }
    elseif ($Credential) {
        # create a WebSession with the specified credential
        $WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
        $WebSession.Credentials = $Credential.GetNetworkCredential()
    }
    else {
        # the caller hasn't defined any credential to use, so just create
        # an empty WebSession that will ultimately generate a 401 error.
        $WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    }

    if ($PSBoundParameters.ContainsKey('IgnoreCertificateValidation')) {
        $certIgnore = $IgnoreCertificateValidation
    }
    else {
        $certIgnore = $script:IgnoreCertificateValidation
    }

    # return the results
    [PSCustomObject]@{
        APIBase=$APIBase;
        WebSession=$WebSession;
        IgnoreCertificateValidation=$certIgnore;
        WAPIVersion=$WAPIVersion;
    }
}