diff --git a/install_newt-msp-site-win_v2.ps1 b/install_newt-msp-site-win_v2.ps1 index 5e507e2..a7837e6 100644 --- a/install_newt-msp-site-win_v2.ps1 +++ b/install_newt-msp-site-win_v2.ps1 @@ -1,15 +1,14 @@ <# .SYNOPSIS - Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client). - Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation & Safe-Update. + Windows-Installer für den Newt-Client. + Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation, Safe-Update & Auto-Cleanup. #> param([string]$mode = "install") -# 1. Stabilität & Encoding [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 -# 2. Konfiguration +# --- Konfiguration --- $Repo = "fosrl/newt" $InstallDir = "C:\Program Files\me-msp-newt" $ServiceName = "MAIEREDV-Managed-Site-Client" @@ -21,7 +20,7 @@ function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color } -# 3. Umgebung vorbereiten (Winget & NSSM) +# --- Umgebung vorbereiten --- function Prepare-Environment { if (!(Get-Command winget -ErrorAction SilentlyContinue)) { Write-Log "Winget fehlt. Installiere via winget.pro..." "Cyan" @@ -50,6 +49,7 @@ function Get-LatestVersion { } } +# --- Download & Cleanup --- function Download-Newt { param($FullVersion) $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } @@ -59,20 +59,18 @@ function Download-Newt { if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } - Write-Log "Prüfe/Downloade Version $VersionOnly..." "Cyan" - - # Download nur wenn Datei noch nicht existiert + # 1. Download if (!(Test-Path $Target)) { + Write-Log "Downloade Version $VersionOnly..." "Cyan" 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) + # 2. Safe-Swap (Dienst stoppen/starten) $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue $WasRunning = $Service -and $Service.Status -eq 'Running' @@ -83,15 +81,28 @@ function Download-Newt { try { Copy-Item -Path $Target -Destination $Symlink -Force - Write-Log "newt_latest.exe wurde auf Stand $VersionOnly aktualisiert." "Green" + Write-Log "newt_latest.exe auf Stand $VersionOnly aktualisiert." "Green" } catch { - Write-Log "FEHLER: Konnte Symlink nicht überschreiben. Datei evtl. blockiert." "Red" + Write-Log "FEHLER: Datei blockiert." "Red" } if ($WasRunning) { Start-Service $ServiceName Write-Log "Dienst wieder gestartet." "Green" } + + # 3. AUFRÄUMEN (Cleanup) + # Wir behalten die newt_latest.exe und die letzten 2 Versions-Exen + Write-Log "Räume alte Versionen auf..." "Yellow" + $OldVersions = Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" | + Where-Object { $_.Name -ne "newt_latest.exe" } | + Sort-Object LastWriteTime -Descending | + Select-Object -Skip 2 # Die neuesten 2 bleiben zur Sicherheit da + + foreach ($file in $OldVersions) { + Write-Log "Lösche alte Version: $($file.Name)" "Gray" + Remove-Item $file.FullName -Force + } } function Setup-Service { @@ -101,11 +112,8 @@ function Setup-Service { $PangolinSecret = Read-Host "Bitte Secret eingeben" $PangolinEndpoint = Read-Host "Bitte Endpoint eingeben" - 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..." "Cyan" & nssm install $ServiceName "$Symlink" $ArgList & nssm set $ServiceName Description "MAIEREDV Managed Site Client" & nssm set $ServiceName AppExit Default Restart @@ -119,9 +127,7 @@ function Setup-Service { & nssm set $ServiceName AppRotateBytes 10485760 Start-Service $ServiceName - Write-Log "Dienst erfolgreich gestartet." "Green" - } else { - Write-Log "Dienst vorhanden. Führe Update-Check aus..." "Yellow" + Write-Log "Dienst gestartet." "Green" } } @@ -153,7 +159,6 @@ switch ($mode) { 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 } @@ -165,17 +170,8 @@ switch ($mode) { Download-Newt $v 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 + Write-Log "System ist aktuell ($vOnly)." "Cyan" if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName } } } - "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 "Deinstalliert." "Green" - } -} \ No newline at end of file + "uninstall \ No newline at end of file