# OpenSSh on Windows install script -- P. Lutus 2017.03.11 function display ($s) { Write-Host "$s" -ForegroundColor "Magenta" } # must run this script as administrator and override no-script execution policy display ("Bypassing no-script execution policy ...") if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } If ([Environment]::Is64BitOperatingSystem) { $osws = '64' } else { $osws = '32' } # create OpenSSH directory $destpath = "C:\Program Files" cd "$destpath" $destzip = "$home\OpenSSH-Win$osws.zip" # download OpenSSH-Win64/32 zip archive if (!(Test-Path $destzip)) { display("Downloading OpenSSH package ...") $url = 'https://github.com/PowerShell/Win32-OpenSSH/releases/latest/' $request = [System.Net.WebRequest]::Create($url) $request.AllowAutoRedirect=$false $response=$request.GetResponse() $url = ($([String]$response.GetResponseHeader("Location")).Replace('tag','download') + "/OpenSSH-Win$osws.zip") Invoke-WebRequest -Uri $url -OutFile $destzip } $newpath = $destpath + "\OpenSSH-Win$osws" if (!(Test-Path $newpath)) { New-Item -ItemType directory -Path $newpath display ("Installing OpenSSH ...") Expand-Archive $destzip -DestinationPath $destpath cd "$newpath" .\install-sshd.ps1 } Else { display ("OpenSSH already installed.") } cd "$newpath" # make SSH utilities conveniently accessible cp ssh.exe,scp.exe,sftp.exe,ssh-keygen.exe,ssh-agent.exe,ssh-add.exe \Windows # create host keys if (!(Test-Path ssh_host__ecdsa_key.pub)) { display ("Generating SSH host keys ...") .\ssh-keygen.exe -A } else { display ("SSH host keys already in place.") } # create client key if (!(Test-Path ~/.ssh/id_ed25519.pub)) { display("Generating SSH Client key ...") .\ssh-keygen.exe -t ed25519 } else { display("SSH Client key already generated.") } # open firewall for SSH display ("Permitting communications on the SSH port ...") New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH >$null display ("Enabling SSH server ...") Set-Service sshd -StartupType Automatic Set-Service ssh-agent -StartupType Automatic Restart-Service sshd $prop = (Get-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command).'(default)' If (-Not($prop -match ".*%\*$")) { display("Editing Registry to allow multiple Python program arguments.") $prop = ($prop + " %*") Set-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command "(Default)" -Value $prop } else { display("Registry already edited.") } if(-Not($env:PATHEXT -match '\.py')) { display ("Adding Python file suffix to system environment ...") $env:PATHEXT += ';.py' } else { display ("System environment already has Python file suffix entry.") } Read-Host -Prompt "Press Enter to close this window"