the currently running PowerShell file path is used.
The end point is enabled by adding a 'powershell' subsystem entry to the SSHD configuration, using
the provided or current PowerShell file path.
Both the SSH client and SSHD server components are detected and if not found a terminating
error is emitted, asking the user to install the components.
Then the sshd_config is parsed, and if a new 'powershell' subsystem entry is added.
.Parameter SSHDConfigFilePath
File path to the SSHD service configuration file. This file will be updated to include a
'powershell' subsystem entry to define a PowerShell SSH remoting endpoint, so current credentials
must have write access to the file.
.Parameter PowerShellFilePath
Specifies the file path to the PowerShell command used to host the SSH remoting PowerShell
endpoint. If no value is specified then the currently running PowerShell executable path is used
in the subsytem command.
.Parameter Force
When true, this cmdlet will update the sshd_config configuration file without prompting.
#>
functionEnable-SSHRemoting
{
[CmdletBinding()]
param(
[string]$SSHDConfigFilePath,
[string]$PowerShellFilePath,
[switch]$Force
)
# Detect platform
$platformInfo=[PlatformInfo]::new()
DetectPlatform$platformInfo
Write-Verbose"Platform information"
Write-Verbose"$($platformInfo|Out-String)"
# Non-Windows platforms must run this cmdlet as 'root'
if(!$platformInfo.isWindows)
{
$user=whoami
if($user-ne'root')
{
if(!$PSCmdlet.ShouldContinue("This cmdlet must be run as 'root'. If you continue, PowerShell will restart under 'root'. Do you wish to continue?","Enable-SSHRemoting"))
{
return
}
# Spawn new PowerShell with sudo and exit this session.
Write-Warning"SSH client is not installed or not discoverable on this machine. SSH client must be installed before PowerShell SSH based remoting can be enabled."
Write-Warning"SSHD service is not found on this machine. SSHD service must be installed and running before PowerShell SSH based remoting can be enabled."
}
# Validate a SSHD configuration file path
if([string]::IsNullOrEmpty($SSHDConfigFilePath))
{
Write-Warning"-SSHDConfigFilePath not provided. Using default configuration file location."
throw"Current running PowerShell version is not valid for SSH remoting endpoint. SSH remoting is only supported for PowerShell version 6.0 and higher. Specify a valid PowerShell 6.0+ file path with the -PowerShellFilePath parameter."
}
}
# SSHD configuration file uses the space character as a delimiter.
# Consequently, the configuration Subsystem entry will not allow argument paths containing space characters.
# For Windows platforms, we can a short cut path.
# But for non-Windows platforms, we currently throw an error.
# One possible solution is to crete a symbolic link
throw"The PowerShell executable (pwsh) selected for hosting the remoting endpoint has a file path containing space characters, which cannot be used with SSHD configuration."
}
}
WriteLine"Using PowerShell at this path for SSH remoting endpoint:"
WriteLine"$PowerShellToUse"-AppendLines1
# Validate the SSHD configuration file path
if(!(Test-Path-Path$SSHDConfigFilePath))
{
throw"The provided SSHDConfigFilePath parameter, $SSHDConfigFilePath, is not a valid path."
}
WriteLine"Modifying SSHD configuration file at this location:"
WriteLine"The following PowerShell subsystems were found in the sshd_config file:"
foreach($entryin$sshdConfig.psSubSystemEntries)
{
WriteLine$entry.subSystemLine
}
Writeline"Continuing will overwrite any existing PowerShell subsystem entries with the new subsystem."-PrependLines1
WriteLine"The new SSH remoting endpoint will use this PowerShell executable path:"
WriteLine"$PowerShellToUse"-AppendLines1
}
$shouldContinue=$Force
if(!$shouldContinue)
{
$shouldContinue=$PSCmdlet.ShouldContinue("The SSHD service configuration file (sshd_config) will now be updated to enable PowerShell remoting over SSH. Do you wish to continue?","Enable-SSHRemoting")