Skip to content

Instantly share code, notes, and snippets.

@ciekawy
Created March 2, 2026 13:48
Show Gist options
  • Select an option

  • Save ciekawy/d0281314fc60e099aedb63d2c92e219e to your computer and use it in GitHub Desktop.

Select an option

Save ciekawy/d0281314fc60e099aedb63d2c92e219e to your computer and use it in GitHub Desktop.

Revisions

  1. ciekawy created this gist Mar 2, 2026.
    62 changes: 62 additions & 0 deletions windows-setup-fixed.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    # Windows Server 2022 Setup Script - WEB-9 (FIXED)
    # Handles PATH refresh and service startup correctly

    Set-ExecutionPolicy Bypass -Scope Process -Force
    [System.Net.ServicePointManager]::SecurityProtocol = 3072

    Write-Host "Installing Chocolatey..." -ForegroundColor Cyan
    iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

    Write-Host "Installing packages..." -ForegroundColor Cyan
    choco install -y openssh powershell-core dotnet-8.0-sdk git

    Write-Host "Refreshing environment..." -ForegroundColor Cyan
    $env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
    refreshenv

    Write-Host "Starting SSH service..." -ForegroundColor Cyan
    # OpenSSH from Chocolatey uses 'sshd' service name
    Get-Service sshd -ErrorAction SilentlyContinue | Start-Service
    Get-Service sshd -ErrorAction SilentlyContinue | Set-Service -StartupType Automatic

    # If Chocolatey OpenSSH didn't install service, use Windows capability
    if (-not (Get-Service sshd -ErrorAction SilentlyContinue)) {
    Write-Host "Installing OpenSSH via Windows capability..." -ForegroundColor Yellow
    Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
    Start-Service sshd
    Set-Service -Name sshd -StartupType Automatic
    }

    New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 -ErrorAction SilentlyContinue

    Write-Host "Configuring PowerShell subsystem..." -ForegroundColor Cyan
    $sshdConfig = "C:\ProgramData\ssh\sshd_config"
    if (Test-Path $sshdConfig) {
    Add-Content -Path $sshdConfig -Value "`nSubsystem powershell C:/Progra~1/PowerShell/7/pwsh.exe -sshs -NoLogo"
    Restart-Service sshd
    }

    Write-Host "Creating test project..." -ForegroundColor Cyan
    New-Item -ItemType Directory -Path C:\Projects -Force | Out-Null
    Set-Location C:\Projects

    # Refresh PATH again before using dotnet
    $env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

    & "C:\Program Files\dotnet\dotnet.exe" new console -n HelloWindows -o HelloWindows
    Set-Location HelloWindows
    & "C:\Program Files\dotnet\dotnet.exe" build

    Write-Host "Adding SSH key..." -ForegroundColor Cyan
    $authorizedKeysFile = "C:\ProgramData\ssh\administrators_authorized_keys"
    $key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO9VR+9HrxMs+2NxN2h8Td6rCDP8eupti1BtqFK1pSdX coder@myfirst"
    New-Item -Path $authorizedKeysFile -ItemType File -Force -Value $key | Out-Null
    icacls.exe $authorizedKeysFile /inheritance:r | Out-Null
    icacls.exe $authorizedKeysFile /grant "SYSTEM:(F)" | Out-Null
    icacls.exe $authorizedKeysFile /grant "BUILTIN\Administrators:(F)" | Out-Null
    Restart-Service sshd -ErrorAction SilentlyContinue

    Write-Host ""
    Write-Host "✅ Setup complete!" -ForegroundColor Green
    Write-Host "SSH: ssh Administrator@46.224.201.14" -ForegroundColor Cyan
    Write-Host "Test .NET: cd C:\Projects\HelloWindows; dotnet run" -ForegroundColor Cyan