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
91
92
93
function Initialize-CallVars
{
    [CmdletBinding()]
    param(
        [Alias('host')]
        [string]$WAPIHost,
        [Alias('version')]
        [string]$WAPIVersion,
        [PSCredential]$Credential,
        [Alias('session')]
        [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession,
        [switch]$IgnoreCertificateValidation,
        [Parameter(ValueFromRemainingArguments = $true)]
        $Splat
    )

    $psb = $PSBoundParameters

    # The purpose of this function is to provide an easy way to get the
    # merged set of common connection parameters to use against
    # Invoke-IBWAPI. Calling functions will pass their set of explicit
    # parameters and we will merge them with the saved set for the
    # currently active WAPIHost. Explicit params always override saved
    # set params.

    # The $Splat param is a dummy var so that calling functions can pass
    # their $PSBoundParameters variable without modification. So get rid
    # of it.
    $psb.Remove('Splat') | Out-Null

    # make sure we have a non-empty WAPIHost
    if ([String]::IsNullOrWhiteSpace($psb.WAPIHost)) {
        if ([String]::IsNullOrWhiteSpace($script:CurrentHost)) {
            throw "WAPIHost missing or empty."
        }
        else {
            # use the saved value
            $psb.WAPIHost = $script:CurrentHost
            Write-Verbose "using saved WAPIHost $($psb.WAPIHost)"

            # remove the empty string config if it exists
            $script:Config.Remove('') | Out-Null
        }
    }

    # make sure a config exists for this host
    if ($script:Config[$psb.WAPIHost]) { $cfg = $script:Config[$psb.WAPIHost] }
    else { $cfg = $script:Config[''] }

    # check the version
    if ([String]::IsNullOrWhiteSpace($psb.WAPIVersion)) {
        if ([String]::IsNullOrWhiteSpace($cfg.WAPIVersion)) {
            throw "WAPIVersion missing or empty."
        }
        else {
            # use the saved value
            $psb.WAPIVersion = $cfg.WAPIVersion
            Write-Verbose "using saved WAPIVersion $($psb.WAPIVersion)"
        }
    }
    else {
        # sanity check the explicit version string

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

        # check format using a [Version] cast
        if (!($psb.WAPIVersion -as [Version])) {
            throw "WAPIVersion is not a valid version string."
        }
    }

    # check Credential and WebSession
    if (!$psb.Credential -and $cfg.Credential) {
        $psb.Credential = $cfg.Credential
        Write-Verbose "using saved Credential for $($psb.Credential.UserName)"
    }
    if (!$psb.WebSession -and $cfg.WebSession) {
        $psb.WebSession = $cfg.WebSession
        Write-Verbose "using saved WebSession for $($psb.WebSession.Credentials.UserName)"
    }

    if (!$psb.ContainsKey('IgnoreCertificateValidation') -and
        $cfg.ContainsKey('IgnoreCertificateValidation')) {
        $psb.IgnoreCertificateValidation = $cfg.IgnoreCertificateValidation
        Write-Verbose "using saved Ignore value $($psb.IgnoreCertificateValidation)"
    }

    # return our modified PSBoundParameters
    $psb
}