Skip to content

Instantly share code, notes, and snippets.

@gravcat
Last active September 22, 2017 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravcat/4a3a76e464bf6ee24a901f17bb364b99 to your computer and use it in GitHub Desktop.
Save gravcat/4a3a76e464bf6ee24a901f17bb364b99 to your computer and use it in GitHub Desktop.
as basic as possible to encapsulate instructions from https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH
param (
[String]
$SSHPort = "22",
[String]
$version = "v0.0.20.0",
[String]
$packageName = "OpenSSH-Win64",
[String]
$githubReleaseUrl = "https://github.com/PowerShell/Win32-OpenSSH/releases/download/$version/$packageName.zip",
[String]
$dlDir = "C:\_maintainer",
[String]
$installDir = "C:\Program Files\" # will extract out as OpenSSH-Win64
)
$ErrorActionPreference = 'Stop'
try {
Write-Output "Ensuring download directory exists at $dlDir"
if (!(Test-Path $dlDir)) { New-Item -ItemType Directory -Path $dlDir > $null }
}
catch {
Write-Error "Creating download directory $dlDir failed: $_"
}
try {
Write-Output "Downloading $packageName.zip"
Invoke-WebRequest -Uri $githubReleaseUrl -OutFile "$dlDir\$packageName.zip"
$downloadedPackage = "$dlDir\$packageName.zip"
}
catch {
Write-Error "$packageName.zip download failed: $_"
}
try {
Write-Output "Ensuring install directory exists at $installDir"
if (!(Test-Path $installDir)) { New-Item -ItemType Directory -Path $installDir > $null }
}
catch {
Write-Error "Creating install directory $installDir failed: $_"
}
try {
if (!(Test-Path $installDir\$packageName)) {
Write-Output "Extracting $packageName to $installDir"
Add-Type -Assembly "System.IO.Compression.FileSystem"
[IO.Compression.ZipFile]::ExtractToDirectory($downloadedPackage, $installDir)
}
}
catch {
Write-Error "$packageName extract to $installDir failed: $_"
}
try {
Write-Output "Modifying install-sshd to fix incompatible if statement"
Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/gravcat/fc008df03d6c33cb2bdb0f8d8aaf9877/raw/87e5dbca6d7b67c54d7a78e6cdc6cacd6c2f0784/modified-install-sshd.ps1' -OutFile "$installDir\$packageName\install-sshd.ps1"
}
catch {
Write-Error "Modifying install_sshd.ps1 failed: $_"
}
try {
Write-Output "Switching to $installDir and installing sshd/ssh-agent services"
cd $installDir\$packageName
if (!(Test-Path $installDir\$packageName\install-$version.complete)) {
.\install-sshd.ps1
New-Item -ItemType File "install-$version.complete"
}
}
catch {
Write-Error "Executing install-sshd.ps1 failed: $_"
}
try {
Write-Output "Generating host keys and ensuring proper permissions"
.\ssh-keygen.exe -A
.\FixHostFilePermissions.ps1 -Confirm:$false
}
catch {
Write-Error "Generating host keys failed: $_"
}
<#try {
Write-Output "Adding host keys to ssh-agent"
cd $installDir\$packageName
.\ssh-add.exe ssh_host_dsa_key -ErrorAction SilentlyContinue
.\ssh-add.exe ssh_host_rsa_key -ErrorAction SilentlyContinue
.\ssh-add.exe ssh_host_ecdsa_key -ErrorAction SilentlyContinue
.\ssh-add.exe ssh_host_ed25519_key -ErrorAction SilentlyContinue
Write-Output "Todo: Delete private keys as they are now loaded into ssh-agent"
}
catch {
Write-Error "Adding host keys to ssh-agent failed: $_"
}#>
try {
Write-Output "Creating firewall exception for $SSHPort/ssh"
New-NetFirewallRule -Protocol TCP -LocalPort $SSHPort -Direction Inbound -Action Allow -DisplayName SSH
}
catch {
Write-Error "Creating firewall exception for $SSHPort failed: $_"
}
try {
Write-Output "Setting SSH services to auto-start"
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
}
catch {
Write-Error "Service manipulation, auto-start failed: $_"
}
try {
Write-Output "Starting sshd"
Restart-Service sshd
}
catch {
Write-Error "(re)Starting SSH service failed."
}
@gravcat
Copy link
Author

gravcat commented Sep 18, 2017

trycatch spam because i want to get things returned in a concise way if it fails via Azure's "Custom Script Extension", sue me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment