Skip to content

Commit e055299

Browse files
authored
Create Install-SSHServer.ps1
1 parent abeead7 commit e055299

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Install-SSHServer.ps1

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Set execution policy for the current process without confirmation prompts
2+
Set-ExecutionPolicy Bypass -Scope Process -Force
3+
4+
# Ensure TLS 1.2 is enabled for secure web communications
5+
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
6+
7+
# Install Chocolatey using their community script
8+
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
9+
10+
# Define a function to comment lines containing a specified string in a file
11+
function Comment-LinesWithSearchString {
12+
param(
13+
[Parameter(Mandatory)][string]$FilePath, # Path to the file
14+
[Parameter(Mandatory)][string]$SearchString # String to search for in the file
15+
)
16+
# Read, process, and overwrite the file
17+
(Get-Content $FilePath) | ForEach-Object {
18+
if ($_ -match $SearchString) {
19+
"#$_" # Prefix line with '#' if it contains the search string
20+
} else {
21+
$_ # Leave other lines unchanged
22+
}
23+
} | Set-Content $FilePath # Write the changes back to the same file
24+
Write-Host "File updated: $FilePath"
25+
}
26+
27+
# Install OpenSSH capabilities if they're not already installed
28+
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*' | ForEach-Object {
29+
Add-WindowsCapability -Online -Name $_.name
30+
}
31+
32+
# Configure SSH services to start automatically and start them
33+
Get-Service | Where-Object Name -Like '*ssh*' | ForEach-Object {
34+
Set-Service -Name $_.name -StartupType Automatic
35+
Start-Service -Name $_.name
36+
}
37+
38+
# Create the .ssh directory and authorized_keys file, set permissions
39+
mkdir .ssh | Out-Null
40+
New-Item -Value "" -Path .ssh/authorized_keys
41+
icacls.exe .ssh /inheritance:r /grant "`"$env:username`:f`" /grant "SYSTEM:f"
42+
43+
# Comment out specific lines in the sshd_config file
44+
"Match Group admin", "administrators_authorized_keys" | ForEach-Object {
45+
Comment-LinesWithSearchString -FilePath 'C:\ProgramData\ssh\sshd_config' -SearchString $_
46+
}
47+
48+
# Install Nano editor using Chocolatey and exit
49+
powershell choco install nano-win -y ; exit;
50+
51+
Get-Service | Where-Object Name -Like '*ssh*' | ForEach-Object {
52+
ReStart-Service -Name $_.name
53+
}

0 commit comments

Comments
 (0)