|
| 1 | +#!/usr/bin/env pwsh |
| 2 | + |
| 3 | +$Esc = [char]27 |
| 4 | +$Warn = "${Esc}[1m[33m" |
| 5 | +$ResetAll = "${Esc}[0m" |
| 6 | + |
| 7 | +# See |
| 8 | +# - <https://gist.github.com/HacDan/026fa8d7d4130fbbc2409d84c2d04143#load-public-keys> |
| 9 | +# - <https://techcommunity.microsoft.com/t5/itops-talk-blog/installing-and-configuring-openssh-on-windows-server-2019/ba-p/309540> |
| 10 | +# - <https://learn.microsoft.com/windows-server/administration/openssh/openssh_install_firstuse> |
| 11 | + |
| 12 | +function InstallOpenSSHServer { |
| 13 | + $OpenSSHServer = Get-WindowsCapability -Online | ` |
| 14 | + Where-Object -Property Name -Like "OpenSSH.Server*" |
| 15 | + IF (-Not ($OpenSSHServer.State -eq "Installed")) { |
| 16 | + Add-WindowsCapability -Online -Name $sshd.Name |
| 17 | + } |
| 18 | + |
| 19 | + $Sshd = Get-Service -Name "sshd" |
| 20 | + IF (-Not ($Sshd.Status -eq "Running")) { |
| 21 | + Start-Service "sshd" |
| 22 | + } |
| 23 | + IF (-Not ($Sshd.StartupType -eq "Automatic")) { |
| 24 | + Set-Service -Name "sshd" -StartupType "Automatic" |
| 25 | + } |
| 26 | + |
| 27 | + $SshAgent = Get-Service -Name "ssh-agent" |
| 28 | + IF (-Not ($SshAgent.Status -eq "Running")) { |
| 29 | + Start-Service "ssh-agent" |
| 30 | + } |
| 31 | + IF (-Not ($SshAgent.StartupType -eq "Automatic")) { |
| 32 | + Set-Service -Name "ssh-agent" -StartupType "Automatic" |
| 33 | + } |
| 34 | + |
| 35 | + Install-Module -Force OpenSSHUtils -Scope AllUsers |
| 36 | +} |
| 37 | + |
| 38 | +function SelfElevate { |
| 39 | + Write-Host "${Warn}Installing 'sshd' requires Admin privileges${ResetAll}" |
| 40 | + Write-Host "Install will continue automatically in 5 seconds..." |
| 41 | + Sleep 5.0 |
| 42 | + |
| 43 | + # Self-elevate the script if required |
| 44 | + $CurUser = New-Object Security.Principal.WindowsPrincipal( |
| 45 | + [Security.Principal.WindowsIdentity]::GetCurrent() |
| 46 | + ) |
| 47 | + $IsAdmin = $CurUser.IsInRole( |
| 48 | + [Security.Principal.WindowsBuiltInRole]::Administrator |
| 49 | + ) |
| 50 | + if ($IsAdmin) { |
| 51 | + Return 0 |
| 52 | + } |
| 53 | + |
| 54 | + $CurLoc = Get-Location |
| 55 | + $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments |
| 56 | + Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine |
| 57 | + Set-Location $CurLoc |
| 58 | + Exit 0 |
| 59 | +} |
| 60 | + |
| 61 | +SelfElevate |
| 62 | +InstallOpenSSHServer |
0 commit comments