File size: 21,151 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

##
## WinRM Remoting helper functions for writing remoting tests
##

$Script:CIRemoteCred = $null

if ($IsWindows) {
    try { $Script:CIRemoteCred = Import-Clixml -Path "$env:TEMP\CIRemoteCred.xml" } catch { }
}

function Get-DefaultEndPointName
{
    $endPointName = "PowerShell.$(${PSVersionTable}.GitCommitId)"
    $endPoint = Get-PSSessionConfiguration -Name $endPointName -ErrorAction SilentlyContinue

    if ($endPoint -eq $null)
    {
        $null = Enable-PSRemoting -SkipNetworkProfileCheck
        $endPoint = Get-PSSessionConfiguration -Name $endPointName -ErrorAction SilentlyContinue

        if ($endPoint -eq $null)
        {
            Write-Warning "Unable to create the remoting configuration endpoint for this PowerShell version: $(${PSVersionTable}.PSVersion)"
            return $endPointName
        }
    }

    if ($endPoint.Permission -like "*NT AUTHORITY\NETWORK AccessDenied*")
    {
        $null = Enable-PSRemoting -SkipNetworkProfileCheck
        $endPoint = Get-PSSessionConfiguration -Name $endPointName -ErrorAction SilentlyContinue

        if ($endPoint.Permission -like "*NT AUTHORITY\NETWORK AccessDenied*")
        {
            Write-Warning "Unable to enable the remoting configuration endpoint: $(${PSVersionTable}.PSVersion)"
        }
    }

    return $endPointName
}

function New-RemoteRunspace
{
    param (
        [string] $ConfigurationName
    )

    # For PSCore6, we want to always test against a remoting endpoint running PSCore6 (not Windows PowerShell)
    if ([string]::IsNullOrEmpty($ConfigurationName))
    {
        $ConfigurationName = Get-DefaultEndPointName
    }

    $wsmanConInfo = [System.Management.Automation.Runspaces.WSManConnectionInfo]::new()

    $wsmanConInfo.ShellUri = 'http://schemas.microsoft.com/powershell/' + $ConfigurationName

    if ($Script:CIRemoteCred)
    {
        Write-Verbose "Using Global CI Credential" -Verbose
        $wsmanConInfo.Credential = $Script:CIRemoteCred
    }
    else
    {
        Write-Verbose "Using Implicit Credential" -Verbose
    }

    $remoteRunspace = [runspacefactory]::CreateRunspace($Host, $wsmanConInfo)
    $remoteRunspace.Open()

    Write-Verbose "Successfully created remote runspace on endpoint: $ConfigurationName"

    return $remoteRunspace
}

function New-RemoteRunspacePool
{
    param (
        [int] $MinRunspace = 1,

        [int] $MaxRunspace = 6,

        [string] $ConfigurationName
    )

    $wsmanConnection = [System.Management.Automation.Runspaces.WSManConnectionInfo]::new()

    if ($ConfigurationName -ne $null)
    {
        $wsmanConnection.ShellUri = "http://schemas.microsoft.com/powershell/$ConfigurationName"
    }

    if ($Script:CIRemoteCred)
    {
        Write-Verbose "Using Global CI Credential" -Verbose
        $wsmanConnection.Credential = $Script:CIRemoteCred
    }
    else
    {
        Write-Verbose "Using Implicit Credential" -Verbose
    }

    [System.Management.Automation.Runspaces.RunspacePool] $remoteRunspacePool = [runspacefactory]::CreateRunspacePool($MinRunspace, $MaxRunspace, $wsmanConnection)
    $remoteRunspacePool.Open()

    return $remoteRunspacePool
}

function CreateParameters
{
    param (
        [string] $ComputerName,
        [string[]] $Name,
        [string] $ConfigurationName,
        [switch] $CimSession,
        [System.Management.Automation.Remoting.PSSessionOption] $SessionOption,
        [System.Management.Automation.Runspaces.PSSession[]] $Session)

    if($ComputerName)
    {
        $parameters = @{ComputerName = $ComputerName}
    }
    else
    {
        if($Session)
        {
            $parameters = @{Session = $Session}
        }
        else
        {
            $parameters = @{ComputerName = '.'}
        }
    }

    if ($Name) {
        if($CimSession.IsPresent)
        {
            $parameters["Name"] = [String] $Name
        }
        else
        {
            $parameters["Name"] = $Name
        }
    }

    if ($ConfigurationName) {
        $parameters["ConfigurationName"] = $ConfigurationName
    }

    if ($SessionOption) {
        $parameters["SessionOption"] = $SessionOption
    }

    ## If a PSSession is provided, do not add credentials.
    if ($Script:CIRemoteCred -and (-not $Session))
    {
        Write-Verbose "Using Global CI Credential" -Verbose
        $parameters["Credential"] = $Script:CIRemoteCred
    }
    else
    {
        Write-Verbose "Using Implicit Credential" -Verbose
    }

    return $parameters
}

function New-RemoteSession
{
    param (
        [string] $Name,
        [string] $ConfigurationName,
        [switch] $CimSession,
        [System.Management.Automation.Remoting.PSSessionOption] $SessionOption
    )

    # For PSCore6, we want to always test against a remoting endpoint running PSCore6 (not Windows PowerShell)
    if ([string]::IsNullOrEmpty($ConfigurationName))
    {
        $ConfigurationName = Get-DefaultEndPointName
    }

    $parameters = CreateParameters -Name $Name -ConfigurationName $ConfigurationName -SessionOption $SessionOption -CimSession:$CimSession.IsPresent

    if ($CimSession) {
        $session = New-CimSession @parameters
    } else {
        $session = New-PSSession @parameters
    }

    Write-Verbose "Successfully created remote PSSession on endpoint: $ConfigurationName"

    return $session
}

function Invoke-RemoteCommand
{
    param (
        [string] $ComputerName,
        [scriptblock] $ScriptBlock,
        [string] $ConfigurationName,
        [switch] $InDisconnectedSession
    )

    # For PSCore6, we want to always test against a remoting endpoint running PSCore6 (not Windows PowerShell)
    if ([string]::IsNullOrEmpty($ConfigurationName))
    {
        $ConfigurationName = Get-DefaultEndPointName
    }

    $parameters = CreateParameters -ComputerName $ComputerName -ConfigurationName $ConfigurationName

    if($ScriptBlock)
    {
        $parameters.Add('ScriptBlock', $ScriptBlock)
    }

    if($InDisconnectedSession)
    {
        $parameters.Add('InDisconnectedSession', $InDisconnectedSession.IsPresent)
    }

    Invoke-Command @parameters
}

function Enter-RemoteSession
{
    param(
        [string] $Name,
        [string] $ConfigurationName,
        [System.Management.Automation.Remoting.PSSessionOption] $SessionOption)

    $parameters = CreateParameters -Name $Name -ConfigurationName $ConfigurationName -SessionOption $SessionOption
    Enter-PSSession @parameters
}

function Connect-RemoteSession
{
    param(
        [string] $ComputerName,
        [string[]] $Name,
        [System.Management.Automation.Runspaces.PSSession[]] $Session,
        [string] $ConfigurationName
    )

    $parameters = CreateParameters -ComputerName $ComputerName -Name $Name -Session $Session -ConfigurationName $ConfigurationName
    Connect-PSSession @parameters
}

function Get-PipePath {
    param (
        $PipeName
    )
    if ($IsWindows) {
        return "\\.\pipe\$PipeName"
    }
    "$([System.IO.Path]::GetTempPath())CoreFxPipe_$PipeName"
}

##
## SSH Remoting helper functions for writing remoting tests
##

function Get-WindowsOpenSSHLink
{
    # From the Win OpenSSH Wiki page (https://github.com/PowerShell/Win32-OpenSSH/wiki/How-to-retrieve-links-to-latest-packages)
    $origSecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    try
    {
        $url = 'https://github.com/PowerShell/Win32-OpenSSH/releases/latest/'
        $request = [System.Net.WebRequest]::Create($url)
        $request.AllowAutoRedirect = $false
        $response = & { $request.GetResponse() } 2>$null

        if ($null -ne $response)
        {
            $location = [string] $response.GetResponseHeader("Location")
            if (! [string]::IsNullOrEmpty($location))
            {
                return $location.Replace('tag', 'download') + '/OpenSSH-Win64.zip'
            }
        }
    }
    finally
    {
        [Net.ServicePointManager]::SecurityProtocol = $origSecurityProtocol
    }

    # Default to last known latest release
    Write-Warning "Unable to get latest OpenSSH release link. Using default release link."
    return 'https://github.com/PowerShell/Win32-OpenSSH/releases/download/v8.1.0.0p1-Beta/OpenSSH-Win64.zip'
}

function Install-WindowsOpenSSH
{
    param (
        [switch] $Force
    )

    $destPath = Join-Path -Path $env:ProgramFiles -ChildPath 'OpenSSH-Win64'
    if (Test-Path -Path $destPath)
    {
        if (! $Force)
        {
            Write-Verbose -Verbose "OpenSSH-Win64 already exists, skipping install step"
            return
        }

        Write-Verbose -Verbose "Force re-install OpenSSH-Win64 ..."
        Stop-Service -Name sshd -ErrorAction SilentlyContinue
        Remove-Item -Path $destPath -Recurse -Force
    }

    # Get link to latest OpenSSH release
    Write-Verbose -Verbose "Downloading latest OpenSSH-Win64 package link ..."
    $downLoadLink = Get-WindowsOpenSSHLink

    # Download and extract OpenSSH package
    Write-Verbose -Verbose "Downloading OpenSSH-Win64 zip package ..."
    $packageFilePath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'OpenSSH-Win64.zip'
    $oldProgressPreference = $ProgressPreference
    $ProgressPreference = 'SilentlyContinue'
    try
    {
        Invoke-WebRequest -Uri $downLoadLink -OutFile $packageFilePath
        Expand-Archive -Path $packageFilePath -DestinationPath $env:ProgramFiles
    }
    finally
    {
        $ProgressPreference = $oldProgressPreference
    }

    # Install and start SSHD service
    Push-Location $destPath
    try
    {
        Write-Verbose -Verbose "Running install-sshd.ps1 ..."
        .\install-sshd.ps1

        $netRule = Get-NetFirewallRule -Name sshd -ErrorAction SilentlyContinue
        if ($null -eq $netRule)
        {
            Write-Verbose -Verbose "Creating firewall rule for SSHD ..."
            New-NetFirewallRule -Name sshd -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
        }

        Write-Verbose -Verbose "Starting SSHD service ..."
        Restart-Service -Name sshd
    }
    finally
    {
        Pop-Location
    }

    # Current release of Windows OpenSSH configures SSHD to change AuthorizedKeyFiles for administrators
    # Comment it out so that normal key based authentication works per user as with Linux platforms.
    # Match Group administrators
    #       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
    $sshdFilePath = "$env:ProgramData\ssh\sshd_config"
    $sshdContent = Get-Content $sshdFilePath
    $sshdNewContent = [string[]] @()
    $modified = $false
    foreach ($item in $sshdContent)
    {
        if ($item.TrimStart().StartsWith('Match Group administrators') -or
            $item.TrimStart().StartsWith('AuthorizedKeysFile __PROGRAMDATA'))
        {
            if (!$modified) { $modified = $true }
            $sshdNewContent += "#" + $item
        }
        else
        {
            $sshdNewContent += $item
        }
    }
    if ($modified)
    {
        $sshdNewContent | Set-Content -Path $sshdFilePath -Force
    }
}

function Install-SSHRemotingOnWindows
{
    param (
        [Parameter(Mandatory=$true)]
        [string] $PowerShellPath
    )

    # Install sshd service
    if ($null -eq (Get-Command -Name sshd -ErrorAction SilentlyContinue))
    {
        Write-Verbose -Verbose "Installing SSHD service ..."
        Install-WindowsOpenSSH -Force
    }

    if (! (Test-Path -Path "$env:ProgramData\ssh\sshd_config"))
    {
        throw "Unable to install SSH service.  Config file $env:ProgramData\ssh\sshd_config does not exist."
    }

    # Configure SSH to authenticate with keys for this user.
    # PubkeyAuthentication should be enabled by default.
    # RSA keys should be enabled by default.

    # Create user .ssh directory.
    if (! (Test-Path -Path "$HOME\.ssh"))
    {
        Write-Verbose -Verbose "Creating $HOME\.ssh directory ..."
        New-Item -Path "$HOME\.ssh" -ItemType Directory -Force
    }

    # Create new rsa keys for current user.
    if ( !(Test-Path "$HOME\.ssh\id_rsa"))
    {
        Write-Verbose -Verbose "Creating rsa keys ..."
        cmd /c "ssh-keygen -t rsa -f $HOME\.ssh\id_rsa -q -N `"`""
    }
    if (! (Test-Path "$HOME\.ssh\id_rsa"))
    {
        throw "id_rsa private key file was not created."
    }
    if (! (Test-Path "$HOME\.ssh\id_rsa.pub"))
    {
        throw "id_rsa.pub public key file was not created."
    }

    # Create authorized keys file.
    Write-Verbose -Verbose "Creating authorized_keys ..."
    Get-Content -Path "$HOME\.ssh\id_rsa.pub" | Set-Content -Path "$HOME\.ssh\authorized_keys" -Force

    # Create known_hosts file for 'localhost' connection.
    Write-Verbose -Verbose "Creating known_hosts ..."
    ssh-keyscan -H localhost | Set-Content -Path "$HOME\.ssh\known_hosts" -Force

    # Install Microsoft.PowerShell.RemotingTools module.
    if ($null -eq (Get-Module -Name Microsoft.PowerShell.RemotingTools -ListAvailable))
    {
        Write-Verbose -Verbose "Installing Microsoft.PowerShell.RemotingTools ..."
        Install-Module -Name Microsoft.PowerShell.RemotingTools -Force -SkipPublisherCheck
    }

    # Add PowerShell endpoint to SSHD.
    Write-Verbose -Verbose "Running Enable-SSHRemoting ..."
    Enable-SSHRemoting -SSHDConfigFilePath "$env:ProgramData\ssh\sshd_config" -PowerShellFilePath $PowerShellPath -Force

    Write-Verbose -Verbose "Restarting sshd service ..."
    Restart-Service -Name sshd

    # Test SSH remoting.
    Write-Verbose -Verbose "Testing SSH remote connection ..."
    $session = New-PSSession -HostName localhost
    try
    {
        if ($null -eq $session)
        {
            throw "Could not successfully create SSH remoting connection."
        }
    }
    finally
    {
        Remove-PSSession $session
    }
}

function WriteVerboseSSHDStatus
{
    param (
        [string] $Msg = 'SSHD service status'
    )

    $sshdStatus = sudo service ssh status
    Write-Verbose -Verbose "${Msg}: $sshdStatus"
}

function DumpTextFile
{
    param (
        [string] $FilePath = '/etc/ssh/sshd_config'
    )

    $content = Get-Content -Path $FilePath -Raw
    Write-Verbose -Verbose $content
}

function Install-SSHRemotingOnLinux
{
    param (
        [Parameter(Mandatory=$true)]
        [string] $PowerShellPath
    )

    # Install ssh daemon.
    if (! (Test-Path -Path /etc/ssh/sshd_config))
    {
        Write-Verbose -Verbose "Installing openssh-server ..."
        sudo apt-get install --yes openssh-server

        Write-Verbose -Verbose "Restarting sshd service after install ..."
        WriteVerboseSSHDStatus "SSHD service status before restart"
        sudo service ssh restart
        WriteVerboseSSHDStatus "SSHD service status after restart"
    }
    if (! (Test-Path -Path /etc/ssh/sshd_config))
    {
        throw "Unable to install SSH daemon.  Config file /etc/ssh/sshd_config does not exist."
    }

    # Configure SSH to authenticate with keys for this user.
    # PubkeyAuthentication should be enabled by default.
    # RSA keys should be enabled by default.

    # Create user .ssh directory.
    if (! (Test-Path -Path "$HOME/.ssh"))
    {
        Write-Verbose -Verbose "Creating $HOME/.ssh directory ..."
        New-Item -Path "$HOME/.ssh" -ItemType Directory -Force
    }

    # Create new rsa keys for current user.
    if ( !(Test-Path "$HOME/.ssh/id_rsa"))
    {
        Write-Verbose -Verbose "Creating rsa keys ..."
        bash -c "ssh-keygen -t rsa -f $HOME/.ssh/id_rsa -q -N ''"
    }
    if (! (Test-Path "$HOME/.ssh/id_rsa"))
    {
        throw "id_rsa private key file was not created."
    }
    if (! (Test-Path "$HOME/.ssh/id_rsa.pub"))
    {
        throw "id_rsa.pub public key file was not created."
    }

    # Create authorized keys file.
    Write-Verbose -Verbose "Creating authorized_keys ..."
    Get-Content -Path "$HOME/.ssh/id_rsa.pub" | Set-Content -Path "$HOME/.ssh/authorized_keys" -Force

    # Create known_hosts file for 'localhost' connection.
    Write-Verbose -Verbose "Updating known_hosts ..."
    ssh-keyscan -H localhost | Set-Content -Path "$HOME/.ssh/known_hosts" -Force

    <#
    # Install Microsoft.PowerShell.RemotingTools module.
    if ($null -eq (Get-Module -Name Microsoft.PowerShell.RemotingTools -ListAvailable))
    {
        Write-Verbose -Verbose "Installing Microsoft.PowerShell.RemotingTools ..."
        Install-Module -Name Microsoft.PowerShell.RemotingTools -Force -SkipPublisherCheck
    }
    #>

    # Add PowerShell endpoint to SSHD.
    Write-Verbose -Verbose "Running Enable-SSHRemoting ..."
    Write-Verbose -Verbose "PSScriptRoot: $PSScriptRoot"
    $modulePath = "${PSScriptRoot}\..\Microsoft.PowerShell.RemotingTools\Microsoft.PowerShell.RemotingTools.psd1"
    $sshdFilePath = '/etc/ssh/sshd_config'

    # First create a default 'powershell' named endpoint.
    $cmdLine = "Import-Module ${modulePath}; Enable-SSHRemoting -SSHDConfigFilePath $sshdFilePath -PowerShellFilePath $PowerShellPath -Force"
    Write-Verbose -Verbose "CmdLine: $cmdLine"
    sudo pwsh -c $cmdLine

    # Next create a 'pwshconfig' named configured endpoint.
    # Configuration file:
    $configFilePath = Join-Path -Path "$env:HOME" -ChildPath 'PSTestConfig.pssc'
    '@{
        GUID = "4d667b90-25f8-47d5-9c90-619b27954748"
        Author = "Microsoft"
        Description = "Test local PowerShell session configuration"
        LanguageMode = "ConstrainedLanguage"
    }' | Out-File -FilePath $configFilePath
    $cmdLine = "Import-Module ${modulePath}; Enable-SSHRemoting -SSHDConfigFilePath $sshdFilePath -PowerShellFilePath $PowerShellPath -ConfigFilePath $configFilePath -SubsystemName 'pwshconfig' -Force"
    Write-Verbose -Verbose "CmdLine: $cmdLine"
    sudo pwsh -c $cmdLine

    # Finally create a 'pwshbroken' named configured endpoint.
    $cmdLine = "Import-Module ${modulePath}; Enable-SSHRemoting -SSHDConfigFilePath $sshdFilePath -PowerShellFilePath $PowerShellPath -ConfigFilePath '$HOME/NoSuch.pssc' -SubsystemName 'pwshbroken' -Force"
    Write-Verbose -Verbose "CmdLine: $cmdLine"
    sudo pwsh -c $cmdLine

    # Restart SSHD service for changes to take effect.
    Start-Sleep -Seconds 1
    WriteVerboseSSHDStatus "SSHD service status before restart"
    Write-Verbose -Verbose "Restarting sshd ..."
    sudo service ssh restart
    WriteVerboseSSHDStatus "SSHD service status after restart"

    # Try starting again if needed.
    $status = sudo service ssh status
    $result = $status | Where-Object { ($_ -like '*not running*') -or ($_ -like '*stopped*') }
    if ($null -ne $result)
    {
        Start-Sleep -Seconds 1
        Write-Verbose -Verbose "Starting sshd again ..."
        sudo service ssh start
        WriteVerboseSSHDStatus "SSHD service status after second start attempt"
    }

    # Test SSH remoting.
    Write-Verbose -Verbose "Testing SSH remote connection ..."
    $session = New-PSSession -HostName localhost
    try
    {
        if ($null -eq $session)
        {
            throw "Could not successfully create SSH remoting connection."
        }
        else
        {
            Write-Verbose -Verbose "SUCCESS: SSH remote connection"
        }
    }
    finally
    {
        Remove-PSSession $session
    }
}

<#
.Synopsis
    Installs and configures SSH components, and creates an SSH PowerShell remoting endpoint.
.Description
    This cmdlet assumes SSH client is installed on the machine, but will check for SSHD service and
    install it if needed.
    Next, it will configure SSHD for key based user authentication, for the current user context.
    Then it configures SSHD for a PowerShell endpoint based on the provided PowerShell file path.
    If no PowerShell file path is provided, the current PowerShell instance ($PSHOME) is used.
    Finally, it will test the new SSH remoting endpoint connection to ensure it works.
    Currently, only Ubuntu and Windows platforms are supported.
.Parameter PowerShellPath
    Specifies a PowerShell, pwsh(.exe), executable path that will be used for the remoting endpoint.
#>
function Install-SSHRemoting
{
    param (
        [string] $PowerShellFilePath
    )

    Write-Verbose -Verbose "Install-SSHRemoting called with PowerShell file path: $PowerShellFilePath"

    if ($IsWindows)
    {
        if ([string]::IsNullOrEmpty($PowerShellFilePath)) { $PowerShellFilePath = "$PSHOME/pwsh.exe" }
        Install-SSHRemotingOnWindows -PowerShellPath $PowerShellFilePath
        return
    }
    elseif ($IsLinux)
    {
        $LinuxInfo = Get-Content /etc/os-release -Raw | ConvertFrom-StringData
        if ($LinuxInfo.ID -match 'ubuntu')
        {
            if ([string]::IsNullOrEmpty($PowerShellFilePath)) { $PowerShellFilePath = "$PSHOME/pwsh" }
            Install-SSHRemotingOnLinux -PowerShellPath $PowerShellFilePath
            return
        }
    }

    Write-Error "Platform not supported.  Only Windows and Ubuntu plaforms are currently supported."
}