<# .SYNOPSIS Windows-Installer für den Newt-Client. Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation, Auto-Cleanup. #> param([string]$mode = "install") # 1. Stabilität & Encoding [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 # 2. Konfiguration $Repo = "fosrl/newt" $InstallDir = "C:\Program Files\me-msp-newt" $ServiceName = "MAIEREDV-Managed-Site-Client" $Symlink = "$InstallDir\newt_latest.exe" $UpdaterTaskName = "MAIEREDV-Newt-Updater" $GiteaUrl = "https://me-gitea.maieredv.cloud/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site-win_v2.1.ps1" function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color } # 3. Umgebung vorbereiten function Prepare-Environment { if (!(Get-Command winget -ErrorAction SilentlyContinue)) { Write-Log "Winget fehlt. Installiere via winget.pro..." "Cyan" try { Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") } catch { Write-Log "FEHLER: Winget Installation fehlgeschlagen." "Red"; exit 1 } } if (!(Get-Command nssm -ErrorAction SilentlyContinue)) { Write-Log "NSSM wird via Winget bereitgestellt..." "Cyan" winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") } } function Get-LatestVersion { try { $url = "https://api.github.com/repos/${Repo}/releases/latest" $json = Invoke-RestMethod -Uri $url -UseBasicParsing return $json.tag_name } catch { Write-Log "FEHLER: GitHub API nicht erreichbar." "Red"; exit 1 } } # 4. Download & Cleanup function Download-Newt { param($FullVersion) $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } $VersionOnly = $FullVersion.TrimStart('v') $Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}" $Target = "${InstallDir}\newt_${VersionOnly}.exe" if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } if (!(Test-Path $Target)) { Write-Log "Downloade Version $VersionOnly..." "Cyan" try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop } catch { Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow" Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing } } $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue $WasRunning = $Service -and $Service.Status -eq 'Running' if ($WasRunning) { Write-Log "Stoppe Dienst für Datei-Update..." "Yellow" Stop-Service $ServiceName -Force } try { Copy-Item -Path $Target -Destination $Symlink -Force Write-Log "newt_latest.exe aktualisiert." "Green" } catch { Write-Log "FEHLER: Datei blockiert." "Red" } if ($WasRunning) { Start-Service $ServiceName Write-Log "Dienst wieder gestartet." "Green" } # Cleanup: Behalte die neuesten 2 Versionen $OldVersions = Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" | Where-Object { $_.Name -ne "newt_latest.exe" } | Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 foreach ($file in $OldVersions) { Remove-Item $file.FullName -Force } } function Setup-Service { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { Write-Log "--- Dienst-Konfiguration ---" "Yellow" $PangolinID = Read-Host "Bitte Pangolin ID eingeben" $PangolinSecret = Read-Host "Bitte Secret eingeben" $PangolinEndpoint = Read-Host "Bitte Endpoint eingeben" $ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}" & nssm install $ServiceName "$Symlink" $ArgList & nssm set $ServiceName Description "MAIEREDV Managed Site Client" & nssm set $ServiceName AppExit Default Restart & nssm set $ServiceName AppRestartDelay 5000 $LogFile = "${InstallDir}\newt_service.log" & nssm set $ServiceName AppStdout "$LogFile" & nssm set $ServiceName AppStderr "$LogFile" & nssm set $ServiceName AppRotateFiles 1 & nssm set $ServiceName AppRotateOnline 1 & nssm set $ServiceName AppRotateBytes 10485760 Start-Service $ServiceName Write-Log "Dienst gestartet." "Green" } } function Setup-UpdaterTask { $ActionScript = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; `$s = (New-Object System.Net.WebClient).DownloadString('$GiteaUrl'); `$b = [scriptblock]::Create(`$s); & `$b -mode update" $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$ActionScript`"" $Trigger = New-ScheduledTaskTrigger -Daily -At 3am $Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $UpdaterTaskName -Force | Out-Null Write-Log "Update-Task (03:00 Uhr) registriert." "Green" } # --- Main --- Prepare-Environment switch ($mode) { "install" { $v = Get-LatestVersion Download-Newt $v Setup-Service Setup-UpdaterTask Write-Log "Installation abgeschlossen!" "Green" } "update" { $v = Get-LatestVersion $vOnly = $v.TrimStart('v') $TargetVersionPath = "${InstallDir}\newt_${vOnly}.exe" $NeedsUpdate = $false if (!(Test-Path $TargetVersionPath)) { $NeedsUpdate = $true } elseif (Test-Path $Symlink) { $HashLatest = (Get-FileHash $Symlink).Hash $HashTarget = (Get-FileHash $TargetVersionPath).Hash if ($HashLatest -ne $HashTarget) { $NeedsUpdate = $true } } else { $NeedsUpdate = $true } if ($NeedsUpdate) { Download-Newt $v Write-Log "Update auf $v erledigt." "Green" } else { Write-Log "System aktuell ($vOnly)." "Cyan" if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName } } } "uninstall" { Write-Log "Deinstallation..." "Yellow" if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue & nssm remove $ServiceName confirm } Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue Write-Log "Alles entfernt." "Green" } }