From 5544f829634f1c0b444830505800d56dde784d1d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 17 Oct 2026 10:12:30 +0200 Subject: [PATCH] Add fast temporary SSH installation path --- tools/TemporarySsh.ps1 | 43 +++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/tools/TemporarySsh.ps1 b/tools/TemporarySsh.ps1 index bde97d0..49b5dd7 100644 --- a/tools/TemporarySsh.ps1 +++ b/tools/TemporarySsh.ps1 @@ -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) {