#Requires -RunAsAdministrator <# .SYNOPSIS Richtet den IIS als HTTPS-Reverse-Proxy vor Alpenwerk HR ein. .DESCRIPTION Voraussetzungen, die dieses Skript NICHT installiert (Downloads von iis.net): - URL Rewrite 2.1 - Application Request Routing 3.0 Den IIS selbst schaltet es ein, falls er fehlt. Das Zertifikat muss im Speicher "Lokaler Computer\Eigene Zertifikate" liegen (Cert:\LocalMachine\My), ausgestellt auf den Hostnamen -- in der Regel von der internen Zertifizierungsstelle. Fingerabdruck anzeigen: Get-ChildItem Cert:\LocalMachine\My | Format-List Subject, Thumbprint, NotAfter Aufruf: .\iis-einrichten.ps1 -HostName hr.firma.at -Zertifikat #> param( [Parameter(Mandatory = $true)][string]$HostName, [Parameter(Mandatory = $true)][string]$Zertifikat, [string]$Ziel = 'C:\AlpenwerkHR', [string]$SiteName = 'Alpenwerk HR' ) . (Join-Path $PSScriptRoot 'gemeinsam.ps1') $app = Read-EnvDatei "$Ziel\app.env" $port = $app['PORT'] if (-not $port) { throw "PORT fehlt in $Ziel\app.env -- zuerst installieren.ps1." } $erwartet = "https://$HostName" if ($app['AUTH_URL'] -and $app['AUTH_URL'].TrimEnd('/') -ne $erwartet) { Write-Host "Achtung: AUTH_URL in app.env ist $($app['AUTH_URL']), erwartet $erwartet." -ForegroundColor Yellow } $Zertifikat = $Zertifikat -replace '\s', '' if (-not (Test-Path -LiteralPath "Cert:\LocalMachine\My\$Zertifikat")) { throw "Zertifikat $Zertifikat nicht in Cert:\LocalMachine\My gefunden." } Write-Host '== IIS' if ((Get-WindowsFeature -Name Web-Server).InstallState -ne 'Installed') { Install-WindowsFeature -Name Web-Server -IncludeManagementTools | Out-Null Write-Host 'IIS installiert.' } $inetsrv = Join-Path $env:SystemRoot 'System32\inetsrv' if (-not (Test-Path -LiteralPath (Join-Path $inetsrv 'rewrite.dll'))) { throw 'Das Modul "URL Rewrite" fehlt: https://www.iis.net/downloads/microsoft/url-rewrite' } if (-not (Test-Path -LiteralPath (Join-Path $env:ProgramFiles 'IIS\Application Request Routing'))) { throw 'Das Modul "Application Request Routing" fehlt: https://www.iis.net/downloads/microsoft/application-request-routing' } Import-Module WebAdministration # -- Serverweite Einstellungen ------------------------------------------ $apphost = 'MACHINE/WEBROOT/APPHOST' # Proxy einschalten. preserveHostHeader: die Anwendung sieht den Namen, den # der Browser benutzt hat. responseBufferLimit 0: Next.js streamt Seiten, # ARR soll sie nicht erst vollstaendig sammeln. Zeitlimit grosszuegig fuer # Excel-Importe. Set-WebConfigurationProperty -PSPath $apphost -Filter 'system.webServer/proxy' -Name 'enabled' -Value $true Set-WebConfigurationProperty -PSPath $apphost -Filter 'system.webServer/proxy' -Name 'preserveHostHeader' -Value $true Set-WebConfigurationProperty -PSPath $apphost -Filter 'system.webServer/proxy' -Name 'responseBufferLimit' -Value 0 Set-WebConfigurationProperty -PSPath $apphost -Filter 'system.webServer/proxy' -Name 'timeout' -Value '00:05:00' $erlaubt = @(Get-WebConfigurationProperty -PSPath $apphost -Filter 'system.webServer/rewrite/allowedServerVariables' -Name 'collection' | ForEach-Object { $_.name }) foreach ($variable in @('HTTP_X_FORWARDED_PROTO', 'HTTP_X_FORWARDED_HOST')) { if ($erlaubt -notcontains $variable) { Add-WebConfigurationProperty -PSPath $apphost -Filter 'system.webServer/rewrite/allowedServerVariables' -Name '.' -Value @{ name = $variable } } } Write-Host 'Proxy und Servervariablen eingestellt.' # -- Website ------------------------------------------------------------ $wurzel = "$Ziel\iis" New-Item -ItemType Directory -Force -Path $wurzel | Out-Null $config = (Get-Content -LiteralPath (Join-Path $PSScriptRoot 'web.config') -Raw -Encoding UTF8).Replace('__PORT__', $port) [System.IO.File]::WriteAllText("$wurzel\web.config", $config, (New-Object System.Text.UTF8Encoding $false)) # Der IIS-Arbeitsprozess muss die web.config lesen koennen -- das # Installationsverzeichnis erbt nichts mehr von C:\. Invoke-Icacls @($wurzel, '/grant', "${SidIisIusrs}:(OI)(CI)RX") if (Get-Website -Name $SiteName) { throw "Die Website '$SiteName' besteht bereits. Im IIS-Manager entfernen oder -SiteName angeben." } $pool = 'AlpenwerkHR' if (-not (Test-Path "IIS:\AppPools\$pool")) { New-WebAppPool -Name $pool | Out-Null # Kein .NET: der Pool reicht nur weiter. Set-ItemProperty "IIS:\AppPools\$pool" -Name managedRuntimeVersion -Value '' } New-Website -Name $SiteName -PhysicalPath $wurzel -ApplicationPool $pool -HostHeader $HostName -Port 443 -Ssl -SslFlags 1 | Out-Null (Get-WebBinding -Name $SiteName -Protocol 'https').AddSslCertificate($Zertifikat, 'My') New-WebBinding -Name $SiteName -Protocol 'http' -Port 80 -HostHeader $HostName Write-Host "Website '$SiteName' fuer $HostName angelegt (443 mit Zertifikat, 80 leitet um)." Write-Host '' Write-Host "Fertig. Test im Browser: https://$HostName" -ForegroundColor Green Write-Host 'DNS-Eintrag und Firewall (443 eingehend aus dem Firmennetz) nicht vergessen.'