@@ -4,63 +4,29 @@ function Invoke-WinUtilSSHServer {
44 Enables OpenSSH server to remote into your windows device
55 #>
66
7- # Get the latest version of OpenSSH Server
8- $FeatureName = Get-WindowsCapability - Online | Where-Object { $_.Name -like " OpenSSH.Server*" }
9-
107 # 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
8+ if (( Get-WindowsCapability - Name OpenSSH.Server - Online) .State -ne " Installed" ) {
9+ Write-Host " Enabling OpenSSH Server... This will take a long time "
10+ Add-WindowsCapability - Name OpenSSH.Server - Online
1411 }
1512
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'
13+ Write-Host " Starting the services"
2414
25- # Confirm the required services are running
26- $SSHDaemonService = Get-Service - Name sshd
27- $SSHAgentService = Get-Service - Name ' ssh-agent'
15+ Set-Service - Name sshd - StartupType Automatic
16+ Start-Service - Name sshd
2817
29- if ($SSHDaemonService.Status -eq ' Running' ) {
30- Write-Host " OpenSSH Server is running."
31- } else {
32- try {
33- Write-Host " OpenSSH Server is not running. Attempting to restart..."
34- Restart-Service - Name sshd - Force
35- Write-Host " OpenSSH Server has been restarted successfully."
36- } catch {
37- Write-Host " Failed to restart OpenSSH Server: $_ "
38- }
39- }
40- if ($SSHAgentService.Status -eq ' Running' ) {
41- Write-Host " ssh-agent is running."
42- } else {
43- try {
44- Write-Host " ssh-agent is not running. Attempting to restart..."
45- Restart-Service - Name sshd - Force
46- Write-Host " ssh-agent has been restarted successfully."
47- } catch {
48- Write-Host " Failed to restart ssh-agent : $_ "
49- }
50- }
18+ Set-Service - Name ssh- agent - StartupType Automatic
19+ Start-Service - Name ssh- agent
5120
5221 # Adding Firewall rule for port 22
53- Write-Host " Setting up firewall rules..."
54- $firewallRule = (Get-NetFirewallRule - Name ' sshd' ).Enabled
55- if ($firewallRule ) {
56- Write-Host " Firewall rule for OpenSSH Server (sshd) already exists."
57- } else {
22+ Write-Host " Setting up firewall rules"
23+ if (-not ((Get-NetFirewallRule - Name ' sshd' ).Enabled)) {
5824 New-NetFirewallRule - Name sshd - DisplayName ' OpenSSH Server (sshd)' - Enabled True - Direction Inbound - Protocol TCP - Action Allow - LocalPort 22
5925 Write-Host " Firewall rule for OpenSSH Server created and enabled."
6026 }
6127
6228 # Check for the authorized_keys file
63- $sshFolderPath = " $env: HOMEDRIVE \ $ env: HOMEPATH \.ssh"
29+ $sshFolderPath = " $Home \.ssh"
6430 $authorizedKeysPath = " $sshFolderPath \authorized_keys"
6531
6632 if (-not (Test-Path - Path $sshFolderPath )) {
@@ -72,10 +38,23 @@ function Invoke-WinUtilSSHServer {
7238 Write-Host " Creating authorized_keys file..."
7339 New-Item - Path $authorizedKeysPath - ItemType File - Force
7440 Write-Host " authorized_keys file created at $authorizedKeysPath ."
75- } else {
76- Write-Host " authorized_keys file already exists at $authorizedKeysPath ."
7741 }
42+
43+ Write-Host " Configuring sshd_config for standard authorized_keys behavior..."
44+ $sshdConfigPath = " C:\ProgramData\ssh\sshd_config"
45+
46+ $configContent = Get-Content - Path $sshdConfigPath - Raw
47+
48+ $updatedContent = $configContent -replace ' (?m)^(Match Group administrators)$' , ' # $1'
49+ $updatedContent = $updatedContent -replace ' (?m)^(\s+AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys)$' , ' # $1'
50+
51+ if ($updatedContent -ne $configContent ) {
52+ Set-Content - Path $sshdConfigPath - Value $updatedContent - Force
53+ Write-Host " Commented out administrator-specific SSH key configuration in sshd_config"
54+ Restart-Service - Name sshd - Force
55+ }
56+
7857 Write-Host " OpenSSH server was successfully enabled."
79- Write-Host " The config file can be located at C:\ProgramData\ssh\sshd_config "
58+ Write-Host " The config file can be located at C:\ProgramData\ssh\sshd_config"
8059 Write-Host " Add your public keys to this file -> $authorizedKeysPath "
8160}
0 commit comments