<# .SYNOPSIS Windows-Pendant zum Newt-Installer. #> param([string]$mode = "install") # TLS 1.2 erzwingen [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Repo = "fosrl/newt" $InstallDir = "C:\Program Files\me-msp-newt" $ServiceName = "MAIEREDV-Managed-Site-Client" $Symlink = "$InstallDir\newt_latest.exe" $TaskName = "Newt-Updater" function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green } function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow } function Write-ErrorMsg($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 } function Get-LatestVersion { try { $url = "https://api.github.com/repos/$Repo/releases/latest" $json = Invoke-RestMethod -Uri $url -UseBasicParsing # Wir speichern die Version EXAKT so wie GitHub sie ausgibt (meist mit v) return $json.tag_name } catch { Write-ErrorMsg "Konnte Version nicht von GitHub abrufen." } } function Download-Newt { param($Version) # Architektur-Mapping $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } # WICHTIG: Prüfen ob das 'v' schon im Tag ist $Tag = if ($Version.StartsWith("v")) { $Version } else { "v$Version" } # Die Datei auf GitHub heißt oft einfach 'newt_windows_amd64.exe' (ohne Version im Dateinamen) $Url = "https://github.com/$Repo/releases/download/$Tag/newt_$ArchSuffix" $Target = "$InstallDir\newt_$($Tag).exe" if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } Write-Info "⬇️ Versuche Download von: $Url" try { Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop Copy-Item -Path $Target -Destination $Symlink -Force Write-Info "✅ Installiert: $Tag" } catch { Write-ErrorMsg "Download fehlgeschlagen (404?). Prüfe ob die Datei unter $Url existiert. Fehler: $_" } } function Setup-Service { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { $PangolinID = Read-Host "🆔 Pangolin ID" $PangolinSecret = Read-Host "🔑 Secret" $PangolinEndpoint = Read-Host "🌐 Endpoint" $ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint" $BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`"" & sc.exe create $ServiceName binPath= $BinaryPath start= auto DisplayName= "MAIEREDV Managed Site Client" Start-Service $ServiceName } else { Restart-Service $ServiceName } } function Setup-UpdateTask { # Hier deine Gitea URL eintragen $GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1" $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 $TaskName -User "SYSTEM" -Force | Out-Null Write-Info "✅ Update-Task registriert." } # --- Ausführung --- switch ($mode) { "install" { $v = Get-LatestVersion Download-Newt $v Setup-Service Setup-UpdateTask } "update" { $v = Get-LatestVersion $Tag = if ($v.StartsWith("v")) { $v } else { "v$v" } if (Test-Path "$InstallDir\newt_$Tag.exe") { Write-Info "Schon aktuell." } else { Download-Newt $v Restart-Service $ServiceName } } "uninstall" { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { Stop-Service $ServiceName & sc.exe delete $ServiceName } Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false } }