From 0a5a071f12e4964bb40a004da54df4a3cd4ed7e0 Mon Sep 17 00:00:00 2001 From: "manuel.maier" Date: Mon, 16 Mar 2026 15:44:47 +0100 Subject: [PATCH] install_newt-msp-site-win_v2.ps1 aktualisiert --- install_newt-msp-site-win_v2.ps1 | 73 +++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/install_newt-msp-site-win_v2.ps1 b/install_newt-msp-site-win_v2.ps1 index ebb0cbf..5e507e2 100644 --- a/install_newt-msp-site-win_v2.ps1 +++ b/install_newt-msp-site-win_v2.ps1 @@ -1,7 +1,7 @@ <# .SYNOPSIS Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client). - Features: Winget.pro integration, NSSM-Service, BITS-Turbo, 10MB Log-Rotation. + Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation & Safe-Update. #> param([string]$mode = "install") @@ -59,14 +59,38 @@ function Download-Newt { if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } - Write-Log "Download von $VersionOnly..." "Cyan" + Write-Log "Prüfe/Downloade Version $VersionOnly..." "Cyan" + + # Download nur wenn Datei noch nicht existiert + if (!(Test-Path $Target)) { + try { + Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop + Write-Log "Download erfolgreich." "Green" + } catch { + Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow" + Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing + } + } + + # Datei-Update (Safe-Swap) + $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 { - Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop Copy-Item -Path $Target -Destination $Symlink -Force + Write-Log "newt_latest.exe wurde auf Stand $VersionOnly aktualisiert." "Green" } catch { - Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow" - Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing - Copy-Item -Path $Target -Destination $Symlink -Force + Write-Log "FEHLER: Konnte Symlink nicht überschreiben. Datei evtl. blockiert." "Red" + } + + if ($WasRunning) { + Start-Service $ServiceName + Write-Log "Dienst wieder gestartet." "Green" } } @@ -77,11 +101,11 @@ function Setup-Service { $PangolinSecret = Read-Host "Bitte Secret eingeben" $PangolinEndpoint = Read-Host "Bitte Endpoint eingeben" - if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID leer!" "Red"; exit 1 } + if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID darf nicht leer sein!" "Red"; exit 1 } $ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}" - Write-Log "Erstelle Dienst mit NSSM (inkl. Log-Rotation)..." "Cyan" + Write-Log "Erstelle Dienst mit NSSM..." "Cyan" & nssm install $ServiceName "$Symlink" $ArgList & nssm set $ServiceName Description "MAIEREDV Managed Site Client" & nssm set $ServiceName AppExit Default Restart @@ -97,20 +121,16 @@ function Setup-Service { Start-Service $ServiceName Write-Log "Dienst erfolgreich gestartet." "Green" } else { - Write-Log "Dienst vorhanden. Starte neu..." "Yellow" - Restart-Service $ServiceName + Write-Log "Dienst vorhanden. Führe Update-Check aus..." "Yellow" } } function Setup-UpdaterTask { - # HIER IST DIE ANPASSUNG: Nutzt nun den ScriptBlock-Syntax für den täglichen Task $ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; & ([scriptblock]::Create((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 - Write-Log "Täglicher Update-Task (03:00 Uhr) registriert." "Green" + Write-Log "Update-Task registriert." "Green" } # --- Main --- @@ -122,17 +142,32 @@ switch ($mode) { Download-Newt $v Setup-Service Setup-UpdaterTask - Write-Log "Installation fertig!" "Green" + Write-Log "🚀 Installation abgeschlossen!" "Green" } "update" { $v = Get-LatestVersion $vOnly = $v.TrimStart('v') - if (Test-Path "${InstallDir}\newt_${vOnly}.exe") { - Write-Log "System ist aktuell ($vOnly)." "Cyan" + $TargetVersionPath = "${InstallDir}\newt_${vOnly}.exe" + + $NeedsUpdate = $false + if (!(Test-Path $TargetVersionPath)) { + $NeedsUpdate = $true + } elseif (Test-Path $Symlink) { + # Hash-Vergleich um sicherzugehen, dass latest.exe wirklich die neue Version ist + $HashLatest = (Get-FileHash $Symlink).Hash + $HashTarget = (Get-FileHash $TargetVersionPath).Hash + if ($HashLatest -ne $HashTarget) { $NeedsUpdate = $true } } else { + $NeedsUpdate = $true + } + + if ($NeedsUpdate) { Download-Newt $v - Restart-Service $ServiceName - Write-Log "Update auf $v abgeschlossen." "Green" + Write-Log "🚀 Update auf $v durchgeführt." "Green" + } else { + Write-Log "System ist bereits aktuell ($vOnly)." "Cyan" + # Falls der Dienst aus irgendeinem Grund steht, starten wir ihn hier + if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName } } } "uninstall" {