Skip to content

Instantly share code, notes, and snippets.

@jsturtevant
Last active February 26, 2019 01:02
Show Gist options
  • Save jsturtevant/c46ac01bb5668c6632ee6a5e8c300520 to your computer and use it in GitHub Desktop.
Save jsturtevant/c46ac01bb5668c6632ee6a5e8c300520 to your computer and use it in GitHub Desktop.
Installs open ssh and sets the key as an admin on the machine
param(
[string] $key
)
$adminpath = "c:\ProgramData\ssh"
$adminfile = "administrators_authorized_keys"
Write-Host "Installing OpenSSH"
$isAvailable = Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
if (!$isAvailable) {
Write-Error "OpenSSH is not avaliable on this machine"
exit 1
}
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
if (!(Test-Path "$adminpath")) {
Write-Host "Created new file and text content added"
New-Item -path $adminpath -name $adminfile -type "file" -value ""
}
Write-Host "$adminpath found."
Write-Host "Adding key to: $adminpath\$adminfile ..."
Add-Content $adminpath\$adminfile $key
Write-Host "Setting required permissions..."
icacls $adminpath\$adminfile /remove "NT AUTHORITY\Authenticated Users"
icacls $adminpath\$adminfile /inheritance:r
Write-Host "Restarting sshd service..."
Restart-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the Firewall rule is configured. It should be created automatically by setup.
$firewall = Get-NetFirewallRule -Name *ssh*
if (!$firewall) {
Write-Error "OpenSSH is firewall is not configured properly"
exit 1
}
Write-Host "OpenSSH installed and configured successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment