github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/e2e/terraform/packer/windows/install-openssh.ps1 (about)

     1  Set-StrictMode -Version latest
     2  $ErrorActionPreference = "Stop"
     3  
     4  $RunningAsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
     5  if (!$RunningAsAdmin) {
     6    Write-Error "Must be executed in Administrator level shell."
     7    exit 1
     8  }
     9  
    10  # Force TLS1.2
    11  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    12  
    13  Try {
    14  
    15      # install portable SSH instead of the Windows feature because we
    16      # need to target 2016
    17      $repo = "https://github.com/PowerShell/Win32-OpenSSH"
    18      $version = "v8.0.0.0p1-Beta"
    19      $url = "${repo}/releases/download/${version}/OpenSSH-Win64.zip"
    20  
    21      # TODO: check sha!
    22      Write-Output "Downloading OpenSSH from: $url"
    23      Invoke-WebRequest -Uri $url -Outfile "OpenSSH-Win64.zip"
    24      Expand-Archive ".\OpenSSH-Win64.zip" "C:\Program Files"
    25      Rename-Item -Path "C:\Program Files\OpenSSH-Win64" -NewName "OpenSSH"
    26  
    27      & "C:\Program Files\OpenSSH\install-sshd.ps1"
    28  
    29      # Start the service
    30      Start-Service sshd
    31      Set-Service -Name sshd -StartupType 'Automatic'
    32  
    33      Start-Service ssh-agent
    34      Set-Service -Name ssh-agent -StartupType 'Automatic'
    35  
    36      # Enable host firewall rule if it doesn't exist
    37      New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' `
    38        -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
    39  
    40      # Set powershell as the OpenSSH login shell
    41      New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" `
    42        -Name DefaultShell `
    43        -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
    44        -PropertyType String -Force
    45  
    46  
    47  } Catch {
    48      Write-Error "Failed to install OpenSSH."
    49      $host.SetShouldExit(-1)
    50      throw
    51  }
    52  
    53  Write-Output "Installed OpenSSH."