Advertisement
Guest User

Powershell Install OpenSSH

a guest
Feb 5th, 2021
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
  2. {
  3. #Relaunch as an elevated process:
  4. write-host "Relaunching as an elevated process. Press any key to continue...";
  5. [void][System.Console]::ReadKey($false);
  6. Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs;
  7. exit;
  8. }
  9.  
  10. if (-not (Test-Path C:\tools)){
  11. mkdir C:\tools;
  12. }
  13.  
  14. if ($null -ne (Get-Service sshd)){
  15. write-host "OpenSSH is already installed. Press any key to exit...";
  16. [void][System.Console]::ReadKey($false);
  17. exit;
  18. }
  19.  
  20. write-host "Checking OpenSSH Windows Feature support...";
  21. $ssh = (Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*');
  22.  
  23. if ($null -eq $ssh){
  24. write-host "Windows Feature not supported...";
  25. write-host "Downloading OpenSSH...";
  26. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
  27. Invoke-WebRequest -Uri "https://github.com/PowerShell/Win32-OpenSSH/releases/download/v8.1.0.0p1-Beta/OpenSSH-Win64.zip" -OutFile "C:\tools\OpenSSH-Win64.zip";
  28. Add-Type -assembly "system.io.compression.filesystem";
  29. write-host "Decompressing OpenSSH...";
  30. [io.compression.zipfile]::ExtractToDirectory( 'C:\tools\OpenSSH-Win64.zip','C:\tools' );
  31. write-host "Installing OpenSSH...";
  32. C:\tools\OpenSSH-Win64\install-sshd.ps1;
  33. } else {
  34. if ($ssh.State -ne "Installed"){
  35. write-host "Windows Feature supported...";
  36. write-host "Installing OpenSSH...";
  37. Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0;
  38. }
  39. }
  40.  
  41. $svc = (Get-Service sshd);
  42.  
  43. if ($svc.Status -ne "Running"){
  44. write-host "Starting SSH Service...";
  45. Start-Service sshd;
  46. }
  47.  
  48. if ($svc.StartType -ne "Automatic"){
  49. write-host "Configuring SSH Service for Automatic Startup...";
  50. Set-Service -Name sshd -StartupType 'Automatic';
  51. }
  52.  
  53. if ($null -eq (Get-NetFirewallRule | ? Name -eq sshd)){
  54. write-host "Adding SSH Service Inbound Firewall Exception...";
  55. New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement