Add fast temporary SSH installation path

This commit is contained in:
Dandaedre 2026-10-17 10:12:30 +02:00
parent 1e8be079d0
commit 5544f82963
1 changed files with 36 additions and 7 deletions

View File

@ -1,7 +1,8 @@
#requires -Version 5.1
[CmdletBinding()]
param(
[ValidateSet('Enable', 'Disable')][string]$Action = $(if ($env:CONFIG_CENTRAL_TEMP_SSH -eq 'disable') { 'Disable' } else { 'Enable' })
[ValidateSet('Enable', 'Disable')][string]$Action = $(if ($env:CONFIG_CENTRAL_TEMP_SSH -eq 'disable') { 'Disable' } else { 'Enable' }),
[string]$InstallMethod = $env:CONFIG_CENTRAL_TEMP_SSH_INSTALL
)
Set-StrictMode -Version 2.0
@ -10,6 +11,8 @@ $ErrorActionPreference = 'Stop'
$scriptUrl = 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/tools/TemporarySsh.ps1'
$temporaryFirewallRule = 'ConfigCentral-Temporary-SSH'
if ([string]::IsNullOrWhiteSpace($InstallMethod)) { $InstallMethod = 'Fast' }
if ($InstallMethod -notin @('Fast', 'Inbox')) { throw "Unknown SSH install method '$InstallMethod'. Use Fast or Inbox." }
function Test-Administrator {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
@ -21,7 +24,7 @@ function Invoke-ElevatedCopy {
$temporaryScript = Join-Path $env:TEMP "config-central-temporary-ssh-$([guid]::NewGuid().ToString('N')).ps1"
try {
Invoke-RestMethod -Uri $scriptUrl -OutFile $temporaryScript -UseBasicParsing
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$temporaryScript`" -Action $Action"
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$temporaryScript`" -Action $Action -InstallMethod $InstallMethod"
try {
$process = Start-Process -FilePath 'powershell.exe' -Verb RunAs -ArgumentList $arguments -Wait -PassThru
}
@ -31,6 +34,36 @@ function Invoke-ElevatedCopy {
finally { Remove-Item -LiteralPath $temporaryScript -Force -ErrorAction SilentlyContinue }
}
function Install-OpenSshServer {
if (Get-Service -Name sshd -ErrorAction SilentlyContinue) { return }
if ($InstallMethod -eq 'Fast' -and (Get-Command winget.exe -ErrorAction SilentlyContinue)) {
Write-Host 'Installing the Microsoft OpenSSH Preview server through WinGet (fast path)...'
& winget.exe install `
--id Microsoft.OpenSSH.Preview `
--exact `
--source winget `
--scope machine `
--silent `
--disable-interactivity `
--accept-package-agreements `
--accept-source-agreements `
--override 'REMOVE=Client'
if ($LASTEXITCODE -eq 0 -and (Get-Service -Name sshd -ErrorAction SilentlyContinue)) { return }
Write-Warning "The fast OpenSSH install did not produce the sshd service (WinGet exit $LASTEXITCODE); falling back to the Windows capability."
}
$capability = Get-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0'
if ($capability.State -ne 'Installed') {
Write-Host 'Installing the supported Windows OpenSSH capability through DISM; Windows servicing can take several minutes.'
& dism.exe /Online /Add-Capability /CapabilityName:OpenSSH.Server~~~~0.0.1.0 /NoRestart
if ($LASTEXITCODE -ne 0) { throw "DISM failed to install OpenSSH Server (exit $LASTEXITCODE)." }
}
if (-not (Get-Service -Name sshd -ErrorAction SilentlyContinue)) {
throw 'OpenSSH installation completed without creating the sshd service.'
}
}
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
throw 'Temporary SSH setup runs only on Windows.'
}
@ -90,11 +123,7 @@ if ($null -eq $existingState) {
}
else { $state = $existingState }
$capability = Get-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0'
if ($capability.State -ne 'Installed') {
Write-Host 'Installing the Windows OpenSSH Server capability...'
Add-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0' | Out-Null
}
Install-OpenSshServer
$standardRule = Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction SilentlyContinue
if ($standardRule -and -not $state.standardRuleEnabled) {