|
| 1 | +function Invoke-WinUtilSSHServer { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Enables OpenSSH server to remote into your windows device |
| 5 | + #> |
| 6 | + |
| 7 | + # Get the latest version of OpenSSH Server |
| 8 | + $FeatureName = Get-WindowsCapability -Online | Where-Object { $_.Name -like "OpenSSH.Server*" } |
| 9 | + |
| 10 | + # Install the OpenSSH Server feature if not already installed |
| 11 | + if ($FeatureName.State -ne "Installed") { |
| 12 | + Write-Host "Enabling OpenSSH Server" |
| 13 | + Add-WindowsCapability -Online -Name $FeatureName.Name |
| 14 | + } |
| 15 | + |
| 16 | + # Sets up the OpenSSH Server service |
| 17 | + Write-Host "Starting the services" |
| 18 | + Start-Service -Name sshd |
| 19 | + Set-Service -Name sshd -StartupType Automatic |
| 20 | + |
| 21 | + # Sets up the ssh-agent service |
| 22 | + Start-Service 'ssh-agent' |
| 23 | + Set-Service -Name 'ssh-agent' -StartupType 'Automatic' |
| 24 | + |
| 25 | + # Confirm the required services are running |
| 26 | + $service = Get-Service -Name sshd |
| 27 | + $service1 = Get-Service -Name 'ssh-agent' |
| 28 | + if ($service.Status -eq 'Running') { |
| 29 | + Write-Host "OpenSSH Server is running." |
| 30 | + } else { |
| 31 | + try { |
| 32 | + Write-Host "OpenSSH Server is not running. Attempting to restart..." |
| 33 | + Restart-Service -Name sshd -Force |
| 34 | + Write-Host "OpenSSH Server has been restarted successfully." |
| 35 | + } catch { |
| 36 | + Write-Host "Failed to restart OpenSSH Server: $_" |
| 37 | + } |
| 38 | + } |
| 39 | + if ($service1.Status -eq 'Running') { |
| 40 | + Write-Host "ssh-agent is running." |
| 41 | + } else { |
| 42 | + try { |
| 43 | + Write-Host "ssh-agent is not running. Attempting to restart..." |
| 44 | + Restart-Service -Name sshd -Force |
| 45 | + Write-Host "ssh-agent has been restarted successfully." |
| 46 | + } catch { |
| 47 | + Write-Host "Failed to restart ssh-agent : $_" |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + #Adding Firewall rule for port 22 |
| 52 | + Write-Host "Setting up firewall rules" |
| 53 | + $firewallRule = (Get-NetFirewallRule -Name 'sshd').Enabled |
| 54 | + if ($firewallRule) { |
| 55 | + Write-Host "Firewall rule for OpenSSH Server (sshd) already exists." |
| 56 | + } else { |
| 57 | + New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 |
| 58 | + Write-Host "Firewall rule for OpenSSH Server created and enabled." |
| 59 | + } |
| 60 | + |
| 61 | + # Check for the authorized_keys file |
| 62 | + $sshFolderPath = "$env:HOMEPATH\ssh" |
| 63 | + $authorizedKeysPath = "$sshFolderPath\authorized_keys" |
| 64 | + |
| 65 | + if (-not (Test-Path -Path $sshFolderPath)) { |
| 66 | + Write-Host "Creating ssh directory..." |
| 67 | + New-Item -Path $sshFolderPath -ItemType Directory -Force |
| 68 | + } |
| 69 | + |
| 70 | + if (-not (Test-Path -Path $authorizedKeysPath)) { |
| 71 | + Write-Host "Creating authorized_keys file..." |
| 72 | + New-Item -Path $authorizedKeysPath -ItemType File -Force |
| 73 | + Write-Host "authorized_keys file created at $authorizedKeysPath." |
| 74 | + } else { |
| 75 | + Write-Host "authorized_keys file already exists at $authorizedKeysPath." |
| 76 | + } |
| 77 | + Write-Host "OpenSSH server was successfully enabled." |
| 78 | + Write-Host "The config file can be located at C:\ProgramData\ssh\sshd_config " |
| 79 | + Write-Host "Add your public keys to this file -> %HOMEPATH%\ssh\authorized_keys" |
| 80 | +} |
0 commit comments