functions/New-RemoteSession.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
function New-RemoteSession { 
[CmdletBinding()]
param(
    [parameter(Mandatory=$true)] $ComputerName,
    [switch][bool] $NoSsl,
    [switch][bool] $Ssl,
    [switch][bool] $Reuse = $true,
    $ServerInfo = $null,
    [switch][bool] $ClearCredentials,
    $port,
    [switch][bool] $cim,
    [parameter(Mandatory=$false)] 
    [pscredential]
    #[System.Management.Automation.Credential()]
    $credential = [pscredential]::Empty,
    [System.Management.Automation.Runspaces.AuthenticationMechanism] $Authentication = [System.Management.Automation.Runspaces.AuthenticationMechanism]::negotiate,
    [switch][bool] $reloadSessionMap = $false
)  
    $map = find-sessionmap -reload:$reloadSessionMap
    $bound = $PSBoundParameters
    if ($bound.reloadSessionMap -ne $null) { $null = $bound.Remove("reloadSessionMap")  }
    if ($bound.ErrorAction -ne $null) { $null = $bound.Remove("ErrorAction")  }
    try {
        $Error.Clear()
        write-verbose "===> connecting with '$Authentication' auth method"
        $s = $null
        $s = _new-remotesession @bound -ErrorAction:SilentlyContinue
    } 
    catch {
        # this was the first try, ignore erorrs
        write-verbose $_
    }

    
    if ($s -ne $null) {
        write-verbose "I have some session:"
        $s | format-table | out-string | write-verbose
    }
    else {
        if ($Error.Count -eq 0) {
        } 
    }
    if ($Error.Count -gt 0 -or $s -eq $null) {
        write-verbose "===> fallback: connecting with manual credentials"
        #$bound["Authentication"] = [System.Management.Automation.Runspaces.AuthenticationMechanism]::Basic
        try {
            $s = _new-remotesession @bound -ErrorAction:Continue 
        } catch {
            if ($_.Exception.Message.Contains(" Basic,")) {
                write-warning "have you enabled basic authentication method, like this:"
                write-warning 'set-item WSMan:\localhost\Client\Auth\Basic -Value true -Confirm:$false'
            }
            throw 
        }
    }

    return $s
}

function _New-RemoteSession {

param(
[parameter(Mandatory=$true)] $ComputerName,
[switch][bool] $NoSsl,
[switch][bool] $Ssl,
[switch][bool] $Reuse = $true,
$ServerInfo = $null,
[switch][bool] $ClearCredentials,
[System.Management.Automation.Runspaces.AuthenticationMechanism] $Authentication = [System.Management.Automation.Runspaces.AuthenticationMechanism]::Negotiate,
$port,
[switch][bool] $cim,
[Parameter(Mandatory=$false)]
[pscredential]
$credential = [pscredential]::Empty
) 
    # ssl is the default
    $use_ssl_by_default = $true
    if ($Ssl.IsPresent) {
        $NoSsl = !$Ssl
    }
    elseif (!$NoSsl.IsPresent) {
        $NoSsl = !$use_ssl_by_default
        if ($port -eq 5985) { $NoSsl = $true }
        if ($port -eq 5986) { $NoSsl = $false }
    }    

    $sessionVar = $ComputerName
    if ($cim) { $sessionVar = "$($ComputerName)_cim" }
    if ($ClearCredentials) { $Reuse = $false }
    if ($Reuse -and (Test-Path variable:global:$sessionVar)) {
        $currentSession = (Get-Variable -Name "$sessionVar").Value
        if ($currentSession -ne $null -and ($cim -or ($currentSession.State -eq "Opened" -and $currentSession.Availability -eq "Available"))) {
            write-verbose "retrieving exisitng session for '$computername'"
            return $currentSession
        }
    }

    
        $hash = @{ 
            "-UseSSL" = !$NoSsl
            "-ComputerName" = $ComputerName
        }
        $found = $false
        if ($global:psSessionsMap -ne $null) {
            $null = ipmo publishmap -Verbose:$false
            $ServerInfo = get-entry $ComputerName -map $global:psSessionsMap 
            $found = $ServerInfo -ne $null
        }
   
        if ($found) {
            write-verbose "found '$ComputerName' in session map at '$Global:psSessionsMapPath'"
            
        } elseif ($ServerInfo -ne $null) {
            if ($global:psSessionsMap -eq $null) {
                $global:psSessionsMap = @{}
            }
            $global:psSessionsMap[$ComputerName] = $ServerInfo
        }
        else {
            write-verbose "'$ComputerName' not found in session map"
        }

        if ($ServerInfo -ne $null) {
            $ServerInfo.Keys | % { 
                if ($hash.ContainsKey("-$_")) {
                    write-verbose " -$_ = $($ServerInfo[$_]) [from sessionmap]"
                    $hash["-$_"] = $ServerInfo[$_]
                } elseif (!$_.StartsWith("_") -and $_ -ne "vars") {
                    write-verbose " -$_ = $($ServerInfo[$_]) [from sessionmap]"
                    $hash["-$_"] = $ServerInfo[$_]
                }
            }

            
            if ($ServerInfo.UseSSL -ne $null) { $NoSsl = !$ServerInfo.UseSSL  }
            if ($ServerInfo.Port -ne $null) { $port = $ServerInfo.Port }

            if ($port -eq $null) {
                if ($nossl) {
                    $port = 5985
                } else {
                    $port = 5986
                }
            }
        } else {             

            if ($port -eq $null) {
                $hasSsl = test-port $ComputerName 5986
                if($hasSsl -and !$NoSsl) {
                    $hash["-UseSSL"] = $true
                    write-verbose " -UseSSL = $true [port 5986 is available and nossl=false]"                    
                    $port = 5986
                } else {
                    $hasPlain = test-port $ComputerName 5985
                    if ($hasPlain) {
                        $hash["-UseSSL"] = $false
                        write-verbose " -UseSSL = $false [port 5985 is available and/or nossl=false]"                    
                        $port = 5985
                    } else {
                        throw "no entry in sessionmap for '$computername'. some Default ports are not available (5986[ssl]=$hasSsl and 5985[nossl]=$NoSsl)"
                    }
                }                    
            } else {
                write-verbose " -UseSSL = !$NoSsl [from -nossl:$nossl]"                    
                $hash["-UseSSL"] = !$NoSsl
            }        
            
             
        }

        if ($ClearCredentials) {
            Cache\Remove-CredentialsCached -container "$ComputerName.cred" 
        }

        $bound = $PSBoundParameters
                
        $useCredentials = $ServerInfo -ne $null `
            -or ($Authentication -eq [System.Management.Automation.Runspaces.AuthenticationMechanism]::Basic) `
            -or $ComputerName.endswith("cloudapp.net") `
            -or ($credential -ne [pscredential]::Empty) `
            -or ($bound.credential -eq $null)
        #$useCredentials = $credential -ne [pscredential]::Empty

        if ($useCredentials) {
            write-verbose "will use credentials"
            <#
            if ($username -ne $null -and $password -ne $null) {
                $secpass = ConvertTo-SecureString $password -AsPlainText -force
                $cred = new-object System.Management.Automation.pscredential -ArgumentList @($username,$secpass)
                $hash["-Credential"] = $cred
            }
            #>

            if ($bound.credential -eq $null) {
                write-verbose "trying cached credentials"
                # auto credentials
                $cred = Cache\Get-CredentialsCached -Message "Enter credentials for $ComputerName" -container "$ComputerName.cred" -verbose
                if ($cred -eq $null) {
                    throw "credentials are required for remote connection to '$ComputerName', but there are no cached credentials in container '$ComputerName.cred'!"
                }
            } 
            else {
                # use provided credentials
                write-verbose "using provided credentials"
                $cred = $credential  
            }

            if ($cred -eq $null) {
                throw "credentials are required for remote connection to '$ComputerName', but none given!"
            }
            write-verbose "passing credentials for user $($cred.username)"
            $hash["-Credential"] = $cred
        } else {
            write-verbose "not using credentials"
        }

        if ($port -ne $null) {
            $hash["-Port"] = $port
        }

        if ($PSBoundParameters["Authentication"] -ne $null) {
            $hash["-Auth"] = $Authentication
        }
        
        $Error.Clear()
        write-verbose "connecting with parameters:"
        $hash | format-table -AutoSize | out-string -Stream | write-verbose

        $session = $null
        if ($cim) {

            if ($hash.ContainsKey("-UseSSL")) { 
                $hash.Remove("-UseSSL") 
            }
            $opts = New-CimSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck -UseSsl:$(!$nossl)
            
            $session = New-CimSession @hash -SessionOption $opts 
            if ($session -eq $null) { throw "failed to create remote CIM sesssion" }
        } else {
            $opts = New-PSSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck
            $session = New-PSSession @hash -ErrorAction:$ErrorActionPreference -SessionOption $opts
            if ($Error.Count -ne 0) {
                if ($Error[0] -match "SSL certificate is signed by an unknown certificate authority" -or $Error[0].Exception.ErrorCode -eq 12175) {
                    write-host "getting remote cert"
                    $p = $port 
                    if ($p -eq $null) { $p = "rps" }
                    $crt = Get-RemoteCert -computername $ComputerName -port $p
                    write-host "found certificate for $($crt.Subject) issuer=$($crt.issuer). Installing to Cert:\CurrentUser\Root"
                    $crt | Export-Certificate -FilePath "$ComputerName.cer"
                    Import-Certificate -FilePath "$ComputerName.cer" -CertStoreLocation Cert:\CurrentUser\Root -Confirm:$false
                    $Error.Clear()
                    $session = New-PSSession @hash -SessionOption $opts 
                }
                if ($Error.Count -ne 0) {  
                    $err = @()
                    $error | % { $err += $_ }
                    throw $error[0]
                }
            }
        }

        if ($session -eq $null) { throw "failed to create remote powershell sesssion" }        

        Write-Verbose "storing session to '$ComputerName' in 'global:$sessionVar'"
        Set-Variable -Name "global:$sessionVar" -Value $session
        
        if ($session -eq $null) { throw "Cannot enter remote session, because it has not been initialized" }
        if ($ServerInfo -ne $null -and ![string]::IsNullOrEmpty($ServerInfo._defaultdir) -and !$cim) {
            $r = icm -Session $session -ScriptBlock { param($dir) cd $dir } -ArgumentList @($ServerInfo._defaultdir)
        }

       return $session
    
}

function Test-Port 
{
    [cmdletbinding()]
    Param(
        [parameter(ParameterSetName='ComputerName', Position=0)]
        [string]
        $ComputerName,

        [parameter(ParameterSetName='IP', Position=0)]
        [System.Net.IPAddress]
        $IPAddress,

        [parameter(Mandatory=$true , Position=1)]
        [int]
        $Port,

        [parameter(Mandatory=$false, Position=2)]
        [ValidateSet("TCP", "UDP")]
        [string]
        $Protocol = "TCP",
        $timeout = 1000
        )

    $RemoteServer = If ([string]::IsNullOrEmpty($ComputerName)) {$IPAddress} Else {$ComputerName};

    If ($Protocol -eq 'TCP')
    {
        $test = New-Object System.Net.Sockets.TcpClient;
        Try
        {            
            Write-verbose "Connecting to $RemoteServer :$Port (TCP)..";
            $r = $test.BeginConnect($RemoteServer, $Port, $null, $null);
            $s = $r.AsyncWaitHandle.WaitOne([timespan]::FromMilliseconds($timeout));
            if (!$s) {
                throw "connection timed out after $timeout"
            }
            Write-verbose "Connection successful";
            return $true
        }
        Catch
        {
            Write-verbose "Connection failed: $_";
            return $false
        }
        Finally
        {
            $test.Dispose();
        }
    }

    If ($Protocol -eq 'UDP')
    {
        Write-warning "UDP port test functionality currently not available."
        <#
        $test = New-Object System.Net.Sockets.UdpClient;
        Try
        {
            Write-Host "Connecting to "$RemoteServer":"$Port" (UDP)..";
            $test.Connect($RemoteServer, $Port);
            Write-Host "Connection successful";
        }
        Catch
        {
            Write-Host "Connection failed";
        }
        Finally
        {
            $test.Dispose();
        }
        #>

    }
}


function Enter-RemoteSession {
param(
[parameter(Mandatory=$true)] $ComputerName,
[switch][bool] $NoSsl,
[switch][bool] $Ssl,
[switch][bool] $ClearCredentials,
$port,
[switch][bool] $Reuse = $true,
[switch][bool] $reloadSessionMap = $true,
[switch][bool] $NoEnter = $false,
[switch][bool] $cim,
[pscredential]
[System.Management.Automation.Credential()]
$credential = [PSCredential]::Empty
) 
    $bound = $PSBoundParameters
    $null = $bound.Remove("NoEnter") 
    $s = new-remotesession @bound
    if ($s -eq $null) {
        throw "failed to connect to '$computername'"
    }
    if (!$NoEnter -and !$cim) {
        $s | Enter-PSSession
    } else {
        return $s
    }
}

set-alias rps Enter-RemoteSession 


function _get-syncdir() {
    if (test-path "HKCU:\Software\Microsoft\OneDrive") 
    {
        $prop = get-itemproperty "HKCU:\Software\Microsoft\OneDrive\" "UserFolder"
        if ($prop -ne $null) {
            $dir = $prop.userfolder
        }        
    }

    return $dir
}





function enter-rdp ($name, [switch][bool]$wait) {
    $file = find-rdp $name
    $p = $null
    if ($file -eq $null -and $name.contains("."))  {
        write-host "running mstsc /v:$name"
        $p = Start-Process mstsc /v:$name -PassThru
    }
    else {
    
        if ($file -eq $null) { throw "rdp profile '$name' not found" }

        write-host "running mstsc '$file'..."
        $p = Start-Process mstsc $file -PassThru
    }

    if($wait) {
        $p.WaitForExit()
    }
}

function copy-sshid { 
    [CmdletBinding()]
    param($host,$port = 22, $alias, $id)

    $ssh_home = "$env:USERPROFILE\.ssh"
    if (!(test-path $ssh_home)) { $null = mkdir $ssh_home }
    if ($id -ne $null) { 
        $idfile = "$id.pub"
        $id = "$ssh_home\$idfile"
    }
    else {
        $id = "$ssh_home\id_rsa.pub"
    }
    if (!(Test-Path $id)) {
        & ssh-keygen
    }

    $cmd = "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys"
    write-verbose "executing: cmd /c `"ssh $host -p $port `"$cmd`" < $id`""
    cmd /c "ssh $host -p $port `"$cmd`" < $id"

    $hostname = $host
    $username = $null
    if ($hostname -match "(.*)@(.*)") {
        $username = $matches[1]
        $hostname = $matches[2]
    }
    if ($alias -eq $null) { $alias = $hostname }

    $config = "$ssh_home\config"
    if (!(test-path $config)) { out-file $config }

    $cfg = @(get-content $config)
    $found = $cfg | ? { $_ -match "Host $alias" }
    if ($found) {
        write-verbose "removing old config for host $alias"
        $newcfg = @()
        $startIdx = -1
        $endIdx = -1
        for($i = 0; $i -lt $cfg.Length; $i++) {
            $_ = $cfg[$i]
            if ($_ -match "Host $alias") {
                $startIdx = $i
                continue
            }
            if ($startIdx -ge 0 -and $_ -match "Host ") {
                $endIdx = $i-1
            }
            if ($startIdx -lt 0 -or $endIdx -gt 0) {
                $newcfg += $_
            }
        }
        $cfg = $newcfg
    }
    
    write-verbose "adding ssh config for host $alias"
    $cfg += "Host $alias"
    $cfg += " HostName $hostname"
    $cfg += " Port $port"
    if ($username -ne $null) {
        $cfg += " User $username"
    }
    if ($idfile -ne $null) {
        $cfg += " IdentityFile $idfile"
    }
    $cfg | Out-File $config -Encoding ascii

}

new-alias rdp enter-rdp -force
new-alias ssh-copy-id copy-sshid -force
new-alias init-ssh copy-sshid -force
new-alias new-sshhost copy-sshid -force