ergosteur

ergosteur / Windows Scripts

Last active 1 month ago

Like 0

Various scripts for automating Windows setup

Revision 0ce7633cc075941da79a90cb6c413788d9ee34c5

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
install-rsat.ps1 Raw
1<#
2.SYNOPSIS
3 Installs the complete Remote Server Administration Tools (RSAT) suite on Windows 10/11.
4
5.DESCRIPTION
6 Automates the discovery and installation of all available Remote Server Administration Tools
7 (RSAT) Windows Capabilities on Windows 10 (1809+) and Windows 11.
8
9 The script queries all RSAT features available on demand via Windows Update/WSUS, identifies
10 those not currently enabled, and sequentially provisions every missing administration tool.
11
12.EXAMPLE
13 irm https://gist.githubusercontent.com/raw/YOUR_GIST_ID/install-rsat.ps1 | iex
14 Downloads and executes the full RSAT setup remotely via an elevated PowerShell session.
15
16.EXAMPLE
17 .\install-rsat.ps1
18 Executes the script locally from an elevated PowerShell session.
19
20.NOTES
21 Prerequisites:
22 - Must run in an Elevated PowerShell Session (Run as Administrator).
23 - Requires Windows 10 Pro/Enterprise/Education (1809+) or Windows 11 Pro/Enterprise/Education.
24 - Requires active internet connectivity or local access to Features on Demand (FOD).
25
26 Key Features:
27 - Idempotent Execution: Scans existing system capabilities and skips tools already installed.
28 - Full Suite Provisioning: Targets all RSAT components (AD DS, DNS, DHCP, Group Policy, Server Manager, WSUS, etc.).
29 - Granular Tracking: Provides real-time progress for each capability installation.
30
31.LINK
32 https://learn.microsoft.com/en-us/windows-server/administration/install-remote-server-administration-tools
33#>
34
35# Requires Elevated PowerShell Session
36if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
37 throw "This script must be executed in a PowerShell session running as Administrator."
38}
39
40$ErrorActionPreference = 'Stop'
41
42Write-Host "Querying available RSAT capabilities..." -ForegroundColor Cyan
43$rsatCapabilities = Get-WindowsCapability -Name "Rsat*" -Online
44
45$missingCapabilities = $rsatCapabilities | Where-Object State -ne 'Installed'
46$installedCount = ($rsatCapabilities | Where-Object State -eq 'Installed').Count
47$missingCount = $missingCapabilities.Count
48
49Write-Host "Found $($rsatCapabilities.Count) total RSAT capabilities ($installedCount already installed, $missingCount missing)." -ForegroundColor Yellow
50
51if ($missingCount -eq 0) {
52 Write-Host "All RSAT tools are already installed on this machine." -ForegroundColor Green
53 exit
54}
55
56Write-Host "Beginning installation of $missingCount missing RSAT tool(s)...`n" -ForegroundColor Cyan
57
58foreach ($tool in $missingCapabilities) {
59 # Parse readable module name from package ID
60 $displayName = $tool.Name.Split("~")[0].Replace("Rsat.", "").Replace(".Tools", "")
61 Write-Host "Installing: $displayName ($($tool.Name))..." -ForegroundColor Cyan
62
63 try {
64 Add-WindowsCapability -Online -Name $tool.Name | Out-Null
65 Write-Host "Successfully installed $displayName." -ForegroundColor Green
66 }
67 catch {
68 Write-Host "Failed to install $displayName: $_" -ForegroundColor Red
69 }
70}
71
72Write-Host "`nFull RSAT installation process complete!" -ForegroundColor Green