Public/Utils/New-ConnectionParameters.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
function New-ConnectionParameters {
    <#
    .SYNOPSIS
    Creates an universal connection parameters object that can be conveniently used for opening connections.

    .DESCRIPTION
    It returns following hashtable:
    ```
    @{
        Nodes = <array of nodes>
        NodesAsString = <nodes in string format>
        RemotingMode = <RemotingMode>
        Credential = <Credential>
        Authentication = <Authentication>
        Port = <Port>
        Protocol = <Protocol>
        PSSessionParams = <hashtable that can be used for splatting in Invoke-Command>
        CimSessionParams = <hashtable that can be used for splatting in New-CimSession>
        MsDeployDestinationString = <string that can be used for opening msdeploy connections>
        OptionsAsString = <string describing all options for logging purposes>
    }
    ```

    .PARAMETER Nodes
    Names of remote nodes where the connection will be established.

    .PARAMETER RemotingMode
    Defines type of remoting protocol to be used for remote connection. Available values:
    - **PSRemoting** (default)
    - **WebDeployHandler** - https://<server>:<8172>/msdeploy.axd
    - **WebDeployAgentService** - http://<server>/MsDeployAgentService

    .PARAMETER Credential
    A PSCredential object that will be used when opening a remoting session to any of the $Nodes specified.

    .PARAMETER Authentication
    Defines type of authentication that will be used to establish remote connection. Available values:
    - **Basic**
    - **NTLM**
    - **CredSSP**
    - **Default** (default)
    - **Digest**
    - **Kerberos**
    - **Negotiate**
    - **NegotiateWithImplicitCredential**

    .PARAMETER Port
    Defines the port used for establishing remote connection.

    .PARAMETER Protocol
    Defines the transport protocol used for establishing remote connection (HTTP or HTTPS). Available values:
    - **HTTP** (default)
    - **HTTPS**

    .PARAMETER CrossDomain
    Should be on when destination nodes are outside current domain.

    .EXAMPLE
    New-ConnectionParameters -Nodes server1
    #>

    
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory=$false)]
        [string[]]
        $Nodes,

        [Parameter(Mandatory=$false)]
        [ValidateSet('PSRemoting', 'WebDeployHandler', 'WebDeployAgentService')]
        [string]
        $RemotingMode = 'PSRemoting',

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential] 
        $Credential,

        [Parameter(Mandatory=$false)]
        [ValidateSet($null, 'Basic', 'NTLM', 'Credssp', 'Default', 'Digest', 'Kerberos', 'Negotiate', 'NegotiateWithImplicitCredential')]
        [string]
        $Authentication,

        [Parameter(Mandatory=$false)]
        [string]
        $Port,

        [Parameter(Mandatory=$false)]
        [string]
        [ValidateSet($null, 'HTTP', 'HTTPS')]
        $Protocol,

        [Parameter(Mandatory=$false)]
        [switch]
        $CrossDomain
    )

    $psRemotingParams = @{}
    $cimSessionParams = @{}
    if ($RemotingMode -eq 'PSRemoting') {
        
        if ($Nodes) {
            $psRemotingParams['ComputerName'] = $Nodes
        }
        if ($Authentication -and $Nodes) {
            $psRemotingParams['Authentication'] = $Authentication
        }

        if ($Credential) {
            $psRemotingParams['Credential'] = $Credential
            # if we have Credentials, we need to have ComputerName (to properly select parameter set in Invoke-Command)
            if (!$Nodes) {
                $psRemotingParams['ComputerName'] = 'localhost'
            }
        }

        if ($Port) {
            $psRemotingParams['Port'] = $Port
        }

        if ($Protocol -eq 'HTTPS') {
            $psRemotingParams['UseSSL'] = $true
            $psRemotingParams['SessionOption'] = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
        }

        if ($Nodes) {
            $cimSessionParams['ComputerName'] = $Nodes
        }
        if ($Authentication) {
            $cimSessionParams['Authentication'] = $Authentication
        }
        #$params['SkipTestConnection'] = $true

        if ($Credential) {
            $cimSessionParams['Credential'] = $Credential
        }

        if ($Port) {
            $cimSessionParams['Port'] = $Port
        }

        if ($Protocol -eq 'HTTPS') {
            $cimSessionParams['SessionOption'] = New-CimSessionOption -UseSsl -SkipCACheck -SkipCNCheck -SkipRevocationCheck
        }
        $msDeployDestinationString = $null
    } 
    else {
         if ($Nodes.Count -ne 1) {
            throw "Only one node can be specified for RemotingMode = $RemotingMode."
         }
         if ($RemotingMode -eq "WebDeployHandler") {
            if (!$Port) {
                # default port
                $Port = '8172'
            }
            if ($Protocol -eq 'HTTP') {
                $urlProtocol = 'http'
            } 
            else {
                $urlProtocol = 'https'
            }
            $url = "{0}://{1}:{2}/msdeploy.axd" -f $urlProtocol, $Nodes[0], $Port
        } 
        else {
            if (!$Port) {
                # default port
                $Port = '80'
            }
            if ($Protocol -eq 'HTTPS') {
                $urlProtocol = 'https'
            } 
            else {
                $urlProtocol = 'http'
            }
            $url = "{0}://{1}:{2}/MsDeployAgentService" -f $urlProtocol, $Nodes[0], $Port
        }
        
        $msDeployDestinationString = "computerName=`"$url`""
        if ($Authentication) {
           $msDeployDestinationString += ",authType=`"$Authentication`""
        }
        if ($Credential) {
           $msDeployDestinationString += ",userName=`"$($Credential.UserName)`""
           if ($Credential.GetNetworkCredential().Password) {
              $msDeployDestinationString += ",password=`"$($Credential.GetNetworkCredential().Password)`""
            }
        }
        $msDeployDestinationString += (",includeAcls=false")
        $msDeployDestinationString += " -allowUntrusted"
    }
    if ($Credential) {
        $userName = $Credential.UserName
    } 
    else {
        $userName = ""
    }

    return @{
        Nodes = @($Nodes)
        NodesAsString = @($Nodes) -join ','
        RemotingMode = $RemotingMode
        Credential = $Credential
        Authentication = $Authentication
        Port = $Port
        Protocol = $Protocol
        CrossDomain = $CrossDomain
        PSSessionParams = $psRemotingParams
        CimSessionParams = $cimSessionParams
        MsDeployDestinationString = $msDeployDestinationString
        OptionsAsString = "Credential: '$userName', RemotingMode: '$RemotingMode', Auth: '$Authentication', Protocol: '$Protocol', Port: '$Port'"
    }
}