install_newt-msp-site-win_v2.ps1 aktualisiert

This commit is contained in:
2026-03-30 10:28:18 +02:00
parent 85de74d691
commit 6ef95eca2d
+34 -31
View File
@@ -1,7 +1,7 @@
<#
.SYNOPSIS
Windows-Installer für den Newt-Client (MAIEREDV).
Features: Winget.pro, NSSM, Turbo-BITS, 10MB Log-Rotation, Auto-Cleanup & Hard-Kill.
Features: Winget.pro, NSSM, Hard-Kill (Stopping-Fix), 10MB Log-Rotation, Auto-Cleanup.
#>
param([string]$mode = "install")
@@ -38,7 +38,7 @@ function Prepare-Environment {
}
}
# 4. Download & Hard-Kill Update
# 4. Download & Verbesserter Hard-Kill (Stopping-Fix)
function Download-Newt {
param($FullVersion)
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
@@ -54,33 +54,40 @@ function Download-Newt {
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
}
# --- Dienst-Stopp mit Hard-Kill Fallback ---
# --- Dienst-Stopp mit verbessertem Hard-Kill ---
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
$WasRunning = $Svc -and $Svc.Status -eq 'Running'
if ($Svc) {
$WasRunning = $true # Wir gehen davon aus, dass wir ihn nachher wieder starten wollen
if ($WasRunning) {
Write-Log "Beende Dienst $ServiceName (Warte max 30s)..." "Yellow"
Stop-Service $ServiceName -Force
if ($Svc.Status -ne 'Stopped') {
Write-Log "Dienst $ServiceName ist im Status '$($Svc.Status)'. Versuche Stop..." "Yellow"
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
$timeout = 30
$timer = [System.Diagnostics.Stopwatch]::StartNew()
while (((Get-Service $ServiceName).Status -ne 'Stopped') -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
Start-Sleep -Seconds 2
$timeout = 30
$timer = [System.Diagnostics.Stopwatch]::StartNew()
while (((Get-Service $ServiceName).Status -ne 'Stopped') -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
Start-Sleep -Seconds 2
}
$timer.Stop()
}
$timer.Stop()
# Wenn er immer noch nicht Stopped ist (z.B. hängt in 'Stopping') -> TASKKILL
if ((Get-Service $ServiceName).Status -ne 'Stopped') {
Write-Log "Dienst klemmt! Erzeinge Abbruch (Hard-Kill)..." "Red"
Stop-Process -Name "newt*" -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Write-Log "Dienst reagiert nicht (Status: $((Get-Service $ServiceName).Status)). Erzeuge Hard-Kill!" "Red"
$NewtProcs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
if ($NewtProcs) {
$NewtProcs | Stop-Process -Force -ErrorAction SilentlyContinue
Write-Log "Prozesse hart beendet." "Yellow"
Start-Sleep -Seconds 2
}
}
}
} else { $WasRunning = $false }
try {
Copy-Item -Path $Target -Destination $Symlink -Force
Write-Log "Datei erfolgreich auf $VersionOnly getauscht." "Green"
} catch {
Write-Log "FEHLER: Datei $Symlink ist gesperrt!" "Red"
Write-Log "FEHLER: Datei $Symlink ist trotz Kill gesperrt! Eventuell manueller Zugriff?" "Red"
}
if ($WasRunning) {
@@ -88,7 +95,7 @@ function Download-Newt {
Write-Log "Dienst wurde neu gestartet." "Green"
}
# Cleanup
# Cleanup: Behalte die neuesten 2 Versionen
Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
Where-Object { $_.Name -ne "newt_latest.exe" } |
Sort-Object LastWriteTime -Descending |
@@ -103,23 +110,16 @@ function Setup-Service {
$PangolinEndpoint = Read-Host "Endpoint"
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
Write-Log "Erstelle Dienst und setze Log-Pfade..." "Cyan"
& nssm install $ServiceName "$Symlink" $ArgList
# Log-Pfade setzen
& nssm set $ServiceName AppStdout "$LogFile"
& nssm set $ServiceName AppStderr "$LogFile"
# Rotation
& nssm set $ServiceName AppRotateFiles 1
& nssm set $ServiceName AppRotateOnline 1
& nssm set $ServiceName AppRotateBytes 10485760
Start-Service $ServiceName
Write-Log "Dienst aktiv. Logs: $LogFile" "Green"
} else {
# Falls der Dienst existiert, aber die Logs fehlen (Nachbesserung)
Write-Log "Prüfe Log-Konfiguration..." "Cyan"
# Log-Pfade nachrüsten falls nötig
& nssm set $ServiceName AppStdout "$LogFile"
& nssm set $ServiceName AppStderr "$LogFile"
& nssm set $ServiceName AppRotateFiles 1
@@ -130,13 +130,12 @@ function Setup-Service {
function Setup-Task {
$IexCommand = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl'))"
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCommand`""
$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 scharfgeschaltet." "Green"
Write-Log "Update-Task (03:00 Uhr) registriert." "Green"
}
# --- Main Logic ---
@@ -157,15 +156,19 @@ elseif ($mode -eq "update" -or (Test-Path $Symlink)) {
Download-Newt $v
} else {
Write-Log "System ist aktuell ($vO)." "Cyan"
# Log-Pfade auch beim Update sicherstellen
Setup-Service
Setup-Service # Sicherstellen, dass Logs & Rotation stimmen
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
}
}
elseif ($mode -eq "uninstall") {
Write-Log "Entferne Dienst..." "Yellow"
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName -Force
# Auch hier Hard-Kill Fallback nutzen
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
$NewtProcs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
if ($NewtProcs) { $NewtProcs | Stop-Process -Force }
& nssm remove $ServiceName confirm
}
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Log "Deinstallation fertig." "Green"
}