Skip to content

Commit 550584a

Browse files
authored
Create setup-ssh.ps1
1 parent afcaa53 commit 550584a

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

images/setup-ssh.ps1

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<powershell>
2+
# Credit to : https://github.com/chorrell/packer-aws-windows-openssh/blob/main/files/SetupSsh.ps1
3+
4+
# Don't display progress bars
5+
# See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.3#progresspreference
6+
$ProgressPreference = 'SilentlyContinue'
7+
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore
8+
# Don't set this before Set-ExecutionPolicy as it throws an error
9+
$ErrorActionPreference = "stop"
10+
# Install OpenSSH using Add-WindowsCapability
11+
# See: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse?tabs=powershell#install-openssh-for-windows
12+
13+
Write-Output 'Installing and starting ssh-agent'
14+
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
15+
Set-Service -Name ssh-agent -StartupType Automatic
16+
Start-Service ssh-agent
17+
18+
Write-Output 'Installing and starting sshd'
19+
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
20+
Set-Service -Name sshd -StartupType Automatic
21+
Start-Service sshd
22+
23+
# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
24+
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
25+
Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
26+
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
27+
} else {
28+
Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
29+
}
30+
31+
# Set default shell to Powershell
32+
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
33+
34+
$keyDownloadScript = Join-Path $env:ProgramData 'ssh\download-key.ps1'
35+
36+
@'
37+
# Download private key to $env:ProgramData\ssh\administrators_authorized_keys
38+
$openSSHAuthorizedKeys = Join-Path $env:ProgramData 'ssh\administrators_authorized_keys'
39+
40+
$keyUrl = "http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key"
41+
Invoke-WebRequest $keyUrl -OutFile $openSSHAuthorizedKeys
42+
43+
# Ensure ACL for administrators_authorized_keys is correct
44+
# See https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration#authorizedkeysfile
45+
icacls.exe $openSSHAuthorizedKeys /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
46+
'@ | Out-File $keyDownloadScript
47+
48+
# Create Task
49+
$taskName = "DownloadKey"
50+
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
51+
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-NoProfile -File ""$keyDownloadScript"""
52+
$trigger = New-ScheduledTaskTrigger -AtStartup
53+
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName $taskName -Description $taskName
54+
55+
# Fetch key via $keyDownloadScript
56+
& Powershell.exe -ExecutionPolicy Bypass -File $keyDownloadScript
57+
58+
</powershell>

0 commit comments

Comments
 (0)