install-windows-openssh.ps1 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <#
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. #>
  6. if (-Not (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  7. Write-Host "Must run as administrator!"
  8. Sleep 5
  9. Exit
  10. }
  11. Write-Host "Setting up OpenSSH server/client"
  12. Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*' | Write-Host
  13. Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
  14. Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
  15. Write-Host "Starting OpenSSH service"
  16. Start-Service sshd
  17. Set-Service -Name sshd -StartupType 'Automatic'
  18. Write-Host "Opening firewall ruls for OpenSSH"
  19. if (!(Get-NetFirewallRule -Name "OpenSSH-Server" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
  20. Write-Output "Firewall Rule 'OpenSSH-Server' does not exist, creating it..."
  21. New-NetFirewallRule -Name 'OpenSSH-Server' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
  22. } else {
  23. Write-Output "Firewall rule 'OpenSSH-Server' has been created and exists."
  24. }
  25. Write-Host "Initializing administrators_authorized_keys"
  26. New-Item C:\ProgramData\ssh\administrators_authorized_keys
  27. Write-Host "Setting permissions"
  28. $acl = Get-Acl C:\ProgramData\ssh\administrators_authorized_keys
  29. $acl.SetAccessRuleProtection($true, $false)
  30. $adminRule = New-Object system.security.accesscontrol.filesystemaccessrule("Administrators", "FullControl", "Allow")
  31. $systemRule = New-Object system.security.accesscontrol.filesystemaccessrule("SYSTEM", "FullControl", "Allow")
  32. $acl.SetAccessRule($adminRule)
  33. $acl.SetAccessRule($systemRule)
  34. $acl | Set-Acl