<# .SYNOPSIS Windows-Installer für den Newt-Client. Nutzt Winget für das Paketmanagement von NSSM. #> param([string]$mode = "install") [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $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://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1" function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color } # 1. Winget & NSSM sicherstellen function Prepare-Environment { if (!(Get-Command winget -ErrorAction SilentlyContinue)) { Write-Log "Winget fehlt. Installiere Winget via winget.pro..." "Cyan" try { irm winget.pro | iex $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 installiert..." "Cyan" winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null # Pfad aktualisieren, falls Winget nssm gerade erst hinzugefügt hat $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 } } 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 } Write-Log "Download Newt $VersionOnly..." "Cyan" try { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing -ErrorAction Stop Copy-Item -Path $Target -Destination $Symlink -Force } catch { Write-Log "FEHLER beim Newt-Download: $_" "Red"; exit 1 } } 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" Write-Log "Erstelle Dienst mit NSSM..." "Cyan" # Da NSSM nun im PATH liegt, rufen wir es direkt auf nssm install $ServiceName "$Symlink" $ArgList nssm set $ServiceName Description "MAIEREDV Managed Site Client" nssm set $ServiceName AppExit Default Restart nssm set $ServiceName AppRestartDelay 5000 nssm set $ServiceName AppStdout "$InstallDir\newt_service.log" nssm set $ServiceName AppStderr "$InstallDir\newt_service.log" Start-Service $ServiceName Write-Log "Dienst erfolgreich gestartet." "Green" } else { Write-Log "Dienst wird aktualisiert..." "Yellow" Restart-Service $ServiceName } } function Setup-UpdaterTask { $ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl')) -mode update`"" $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $ActionCommand $Trigger = New-ScheduledTaskTrigger -Daily -At 3am Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $UpdaterTaskName -User "SYSTEM" -Force | Out-Null } # --- Main --- Prepare-Environment switch ($mode) { "install" { $v = Get-LatestVersion Download-Newt $v Setup-Service Setup-UpdaterTask Write-Log "Installation erfolgreich abgeschlossen!" "Green" } "update" { $v = Get-LatestVersion $vOnly = $v.TrimStart('v') if (Test-Path "$InstallDir\newt_$vOnly.exe") { Write-Log "System ist aktuell." "Cyan" } else { Download-Newt $v Restart-Service $ServiceName Write-Log "Update auf $v durchgefuehrt." "Green" } } "uninstall" { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { Stop-Service $ServiceName -Force nssm remove $ServiceName confirm } Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue Write-Log "Deinstallation fertig." "Green" } }