Skip to content

Commit ade4142

Browse files
committed
Added option to enable ssh server under config tab
1 parent f7ba201 commit ade4142

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

Diff for: config/feature.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,13 @@
313313
"Order": "a083_",
314314
"Type": "Button",
315315
"ButtonWidth": "300"
316-
}
316+
},
317+
"WPFWinUtilSSHServer": {
318+
"Content": "Enable OpenSSH Server",
319+
"category": "Remote Access",
320+
"panel": "2",
321+
"Order": "a084_",
322+
"Type": "Button",
323+
"ButtonWidth": "300"
324+
},
317325
}

Diff for: functions/private/Invoke-WinUtilSSHServer.ps1

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

Diff for: functions/public/Invoke-WPFButton.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ function Invoke-WPFButton {
5959
"WPFCloseButton" {Invoke-WPFCloseButton}
6060
"MicrowinScratchDirBT" {Invoke-ScratchDialog}
6161
"WPFWinUtilPSProfile" {Invoke-WinUtilpsProfile}
62+
"WPFWinUtilSSHServer" {Invoke-WinUtilSSHServer}
6263
}
6364
}

0 commit comments

Comments
 (0)