Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
- {
- #Relaunch as an elevated process:
- write-host "Relaunching as an elevated process. Press any key to continue...";
- [void][System.Console]::ReadKey($false);
- Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs;
- exit;
- }
- if (-not (Test-Path C:\tools)){
- mkdir C:\tools;
- }
- if ($null -ne (Get-Service sshd)){
- write-host "OpenSSH is already installed. Press any key to exit...";
- [void][System.Console]::ReadKey($false);
- exit;
- }
- write-host "Checking OpenSSH Windows Feature support...";
- $ssh = (Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*');
- if ($null -eq $ssh){
- write-host "Windows Feature not supported...";
- write-host "Downloading OpenSSH...";
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
- 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";
- Add-Type -assembly "system.io.compression.filesystem";
- write-host "Decompressing OpenSSH...";
- [io.compression.zipfile]::ExtractToDirectory( 'C:\tools\OpenSSH-Win64.zip','C:\tools' );
- write-host "Installing OpenSSH...";
- C:\tools\OpenSSH-Win64\install-sshd.ps1;
- } else {
- if ($ssh.State -ne "Installed"){
- write-host "Windows Feature supported...";
- write-host "Installing OpenSSH...";
- Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0;
- }
- }
- $svc = (Get-Service sshd);
- if ($svc.Status -ne "Running"){
- write-host "Starting SSH Service...";
- Start-Service sshd;
- }
- if ($svc.StartType -ne "Automatic"){
- write-host "Configuring SSH Service for Automatic Startup...";
- Set-Service -Name sshd -StartupType 'Automatic';
- }
- if ($null -eq (Get-NetFirewallRule | ? Name -eq sshd)){
- write-host "Adding SSH Service Inbound Firewall Exception...";
- New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement