|
1 | 1 | #!/usr/bin/env pwsh |
2 | 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 | | - } |
| 3 | +$ErrorActionPreference = 'stop' |
18 | 4 |
|
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 | | - } |
| 5 | +function Repair-MissingCommand { |
| 6 | + Param( |
| 7 | + [string]$Name, |
| 8 | + [string]$Package, |
| 9 | + [string]$Command |
| 10 | + ) |
26 | 11 |
|
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" |
| 12 | + Write-Host " Checking for $Name ..." |
| 13 | + $HasCommand = Get-Command -Name $Command -ErrorAction Silent |
| 14 | + IF ($HasCommand) { |
| 15 | + Return |
33 | 16 | } |
34 | 17 |
|
35 | | - Install-Module -Force OpenSSHUtils -Scope AllUsers |
| 18 | + & $HOME\.local\bin\webi-pwsh.ps1 $Package |
| 19 | + $null = Sync-EnvPath |
36 | 20 | } |
37 | 21 |
|
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() |
| 22 | +function Install-WebiHostedScript () { |
| 23 | + Param( |
| 24 | + [string]$Package, |
| 25 | + [string]$ScriptName |
46 | 26 | ) |
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 |
| 27 | + $PwshName = "_${ScriptName}.ps1" |
| 28 | + $PwshUrl = "${Env:WEBI_HOST}/packages/${Package}/${ScriptName}.ps1" |
| 29 | + $PwshPath = "$HOME\.local\bin\${PwshName}" |
| 30 | + $OldPath = "$HOME\.local\bin\${ScriptName}.ps1" |
| 31 | + |
| 32 | + $BatPath = "$HOME\.local\bin\${ScriptName}.bat" |
| 33 | + $PwshExec = "powershell -ExecutionPolicy Bypass" |
| 34 | + $Bat = "@echo off`r`n$PwshExec %USERPROFILE%\.local\bin\${PwshName} %*" |
| 35 | + |
| 36 | + Invoke-DownloadUrl -Force -URL $PwshUrl -Path $PwshPath |
| 37 | + Set-Content -Path $BatPath -Value $Bat |
| 38 | + Write-Host " Created alias ${BatPath}" |
| 39 | + Write-Host " to run ${PwshPath}" |
| 40 | + |
| 41 | + # fix for old installs |
| 42 | + Remove-Item -Path $OldPath -Force -ErrorAction Ignore |
59 | 43 | } |
60 | 44 |
|
61 | | -SelfElevate |
62 | | -InstallOpenSSHServer |
| 45 | + |
| 46 | +Repair-MissingCommand -Name "sudo (RunAs alias)" -Package "sudo" -Command "sudo" |
| 47 | +Install-WebiHostedScript -Package "sshd" -ScriptName "sshd-service-install" |
| 48 | + |
| 49 | +Write-Output "" |
| 50 | +Write-Output "${TTask}Copy, paste, and run${TReset} the following to install sshd as a system service" |
| 51 | +Write-Output " ${TCmd}sshd-service-install${TReset}" |
| 52 | +Write-Output "" |
0 commit comments