ergosteur

ergosteur / Windows Scripts

Last active 1 month ago

Like 0

Various scripts for automating Windows setup

Revision fd8eb3d36764144083904fdeb1bcf6417bbbf29f

install-openssh.ps1 Raw
1<#
2.SYNOPSIS
3 Installs and configures OpenSSH Client and Server on Windows 10/11.
4
5.DESCRIPTION
6 Automates the installation of OpenSSH Client and Server Windows Capabilities, configures
7 the OpenSSH SSHD service to start automatically, starts the service, and verifies/creates
8 the necessary inbound firewall rule for Port 22.
9
10.EXAMPLE
11 irm https://gist.githubusercontent.com/raw/YOUR_GIST_ID/install-openssh.ps1 | iex
12 irm https://1qaz.ca/install-openssh-win | iex
13 Downloads and executes the OpenSSH setup remotely via an elevated PowerShell session.
14
15.EXAMPLE
16 .\install-openssh.ps1
17 Executes the script locally from an elevated PowerShell session.
18
19.NOTES
20 Prerequisites:
21 - Must run in an Elevated PowerShell Session (Run as Administrator).
22 - Requires Windows 10 (1809+) or Windows 11 with internet or WSUS access for Windows Capabilities.
23
24 Key Features:
25 - Idempotent Execution: Safely skips installation/configuration steps if components are already present.
26 - Automated Service Setup: Configures `sshd` startup type to 'Automatic' and ensures it is running.
27 - Firewall Provisioning: Verifies or generates the 'OpenSSH-Server-In-TCP' inbound firewall rule.
28
29.LINK
30 https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse
31#>
32
33# Requires Elevated PowerShell Session
34if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
35 throw "This script must be executed in a PowerShell session running as Administrator."
36}
37
38$ErrorActionPreference = 'Stop'
39
40# 1. Install OpenSSH Client
41Write-Host "Checking OpenSSH Client status..." -ForegroundColor Cyan
42$clientCapability = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*'
43
44if ($clientCapability.State -ne 'Installed') {
45 Write-Host "Installing OpenSSH Client..." -ForegroundColor Cyan
46 Add-WindowsCapability -Online -Name $clientCapability.Name | Out-Null
47 Write-Host "OpenSSH Client installed successfully." -ForegroundColor Green
48} else {
49 Write-Host "OpenSSH Client is already installed." -ForegroundColor Yellow
50}
51
52# 2. Install OpenSSH Server
53Write-Host "Checking OpenSSH Server status..." -ForegroundColor Cyan
54$serverCapability = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'
55
56if ($serverCapability.State -ne 'Installed') {
57 Write-Host "Installing OpenSSH Server..." -ForegroundColor Cyan
58 Add-WindowsCapability -Online -Name $serverCapability.Name | Out-Null
59 Write-Host "OpenSSH Server installed successfully." -ForegroundColor Green
60} else {
61 Write-Host "OpenSSH Server is already installed." -ForegroundColor Yellow
62}
63
64# 3. Configure and Start SSHD Service
65Write-Host "Configuring OpenSSH Server service (sshd)..." -ForegroundColor Cyan
66Set-Service -Name sshd -StartupType 'Automatic'
67
68$sshdService = Get-Service -Name sshd
69if ($sshdService.Status -ne 'Running') {
70 Start-Service sshd
71 Write-Host "OpenSSH Server service started." -ForegroundColor Green
72} else {
73 Write-Host "OpenSSH Server service is already running." -ForegroundColor Yellow
74}
75
76# 4. Verify/Create Firewall Rule
77Write-Host "Verifying Firewall Configuration..." -ForegroundColor Cyan
78if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
79 Write-Host "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." -ForegroundColor Cyan
80 New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null
81 Write-Host "Firewall rule created successfully." -ForegroundColor Green
82} else {
83 Write-Host "Firewall rule 'OpenSSH-Server-In-TCP' already exists." -ForegroundColor Yellow
84}
85
86Write-Host "`nOpenSSH Client and Server installation and configuration complete!" -ForegroundColor Green