install_newt-msp-site-win_v2.ps1 aktualisiert

This commit is contained in:
2026-03-30 10:37:24 +02:00
parent ea12c6a8d0
commit 8062ad4cb5
+40 -67
View File
@@ -1,14 +1,13 @@
<# <#
.SYNOPSIS .SYNOPSIS
Windows-Installer für den Newt-Client (MAIEREDV). Windows-Installer für den Newt-Client (MAIEREDV).
v2.4 - Lokalisierungs-Fix (Deutsch/Englisch) & numerische Status-Codes. v2.5 - Prozess-fokussierter Kill & Immediate-Action.
#> #>
param([string]$mode = "install") param([string]$mode = "install")
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# --- Konfiguration ---
$Repo = "fosrl/newt" $Repo = "fosrl/newt"
$InstallDir = "C:\Program Files\me-msp-newt" $InstallDir = "C:\Program Files\me-msp-newt"
$ServiceName = "MAIEREDV-Managed-Site-Client" $ServiceName = "MAIEREDV-Managed-Site-Client"
@@ -21,19 +20,6 @@ function Write-Log($msg, $color = "White") {
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color
} }
# --- Umgebung vorbereiten ---
function Prepare-Environment {
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
try { Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression } catch {}
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}
}
# --- Download & Brutal-Kill ---
function Download-Newt { function Download-Newt {
param($FullVersion) param($FullVersion)
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
@@ -49,55 +35,46 @@ function Download-Newt {
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing } catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
} }
# --- AGGRESSIVER STOPP (Sprachen-unabhängig) --- # --- RADIKALER KILL-BLOCK ---
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue Write-Log "Bereite Datei-Update vor..." "Yellow"
if ($Svc) {
# Status 1 = Stopped / Beendet
if ($Svc.Status.value__ -ne 1) {
Write-Log "Dienst ist nicht beendet (Status-Code: $($Svc.Status.value__)). Deaktiviere..." "Yellow"
Set-Service $ServiceName -StartupType Disabled
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
$timeout = 30 # 1. Dienst deaktivieren & stoppen
$timer = [System.Diagnostics.Stopwatch]::StartNew() if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
# Wir warten bis Status == 1 Set-Service $ServiceName -StartupType Disabled
while (($Svc.Status.value__ -ne 1) -and ($timer.Elapsed.TotalSeconds -lt $timeout)) { Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2 }
$Svc.Refresh()
}
$timer.Stop()
}
# Wenn er immer noch klemmt -> TASKKILL # 2. PROZESS-CHECK (Nicht Dienst-Check!)
if ($Svc.Status.value__ -ne 1) { # Wir loopen max. 5 mal und versuchen zu killen
Write-Log "Dienst hängt immer noch. Sende Taskkill /F /T..." "Red" for ($i = 1; $i -le 5; $i++) {
taskkill.exe /F /T /IM "newt*" /FI "STATUS eq RUNNING" 2>$null $Procs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
if ($Procs) {
Write-Log "Versuch $i: Kill hängende Prozesse ($($Procs.Count))..." "Red"
taskkill.exe /F /T /IM "newt*" 2>$null taskkill.exe /F /T /IM "newt*" 2>$null
Start-Sleep -Seconds 5 Start-Sleep -Seconds 3
} else {
break
} }
} }
# --- RENAME-MOVE TRICK --- # 3. RENAME-TEST (Die ultimative Prüfung)
try { try {
if (Test-Path $Symlink) { if (Test-Path $Symlink) {
$TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss") $TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss")
Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction SilentlyContinue # Wenn Move-Item fehlschlägt, ist die Datei noch gelockt
Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction Stop
Write-Log "Dateipfad durch Umbenennen freigegeben." "Green"
} }
Copy-Item -Path $Target -Destination $Symlink -Force Copy-Item -Path $Target -Destination $Symlink -Force
Write-Log "Datei erfolgreich ersetzt." "Green" Write-Log "Update erfolgreich eingespielt." "Green"
} catch { } catch {
Write-Log "DATEI-LOCK: Konnte $Symlink nicht ersetzen!" "Red" Write-Log "KRITISCH: Datei immer noch gesperrt durch Kernel-Lock!" "Red"
Write-Log "Der Update wird erst nach einem System-Reboot aktiv." "Yellow"
} }
# Dienst wieder scharf schalten # Dienst wieder auf Kurs bringen
Set-Service $ServiceName -StartupType Automatic Set-Service $ServiceName -StartupType Automatic
Start-Service $ServiceName -ErrorAction SilentlyContinue Start-Service $ServiceName -ErrorAction SilentlyContinue
Write-Log "Dienst neu gestartet." "Green"
# Cleanup
Get-ChildItem -Path $InstallDir -Filter "newt_*" |
Where-Object { $_.Name -ne "newt_latest.exe" } |
Sort-Object LastWriteTime -Descending | Select-Object -Skip 3 | Remove-Item -Force -ErrorAction SilentlyContinue
} }
function Setup-Service { function Setup-Service {
@@ -106,19 +83,13 @@ function Setup-Service {
$PangolinID = Read-Host "ID"; $PangolinSecret = Read-Host "Secret"; $PangolinEndpoint = Read-Host "Endpoint" $PangolinID = Read-Host "ID"; $PangolinSecret = Read-Host "Secret"; $PangolinEndpoint = Read-Host "Endpoint"
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}" $ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
& nssm install $ServiceName "$Symlink" $ArgList & nssm install $ServiceName "$Symlink" $ArgList
& nssm set $ServiceName AppStdout "$LogFile"
& nssm set $ServiceName AppStderr "$LogFile"
& nssm set $ServiceName AppRotateFiles 1
& nssm set $ServiceName AppRotateOnline 1
& nssm set $ServiceName AppRotateBytes 10485760
Start-Service $ServiceName
} else {
& nssm set $ServiceName AppStdout "$LogFile"
& nssm set $ServiceName AppStderr "$LogFile"
& nssm set $ServiceName AppRotateFiles 1
& nssm set $ServiceName AppRotateOnline 1
& nssm set $ServiceName AppRotateBytes 10485760
} }
# Log-Repair & Rotation (Immer ausführen)
& nssm set $ServiceName AppStdout "$LogFile"
& nssm set $ServiceName AppStderr "$LogFile"
& nssm set $ServiceName AppRotateFiles 1
& nssm set $ServiceName AppRotateOnline 1
& nssm set $ServiceName AppRotateBytes 10485760
} }
function Setup-Task { function Setup-Task {
@@ -127,11 +98,15 @@ function Setup-Task {
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am $Trigger = New-ScheduledTaskTrigger -Daily -At 3am
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest $Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $UpdaterTaskName -Force | Out-Null Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $UpdaterTaskName -Force | Out-Null
Write-Log "Update-Task registriert." "Green"
} }
# --- Main Logic --- # --- Main Logic ---
Prepare-Environment if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
if (!(Get-Command winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch {} }
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}
if (!(Test-Path $Symlink) -or ($mode -eq "install")) { if (!(Test-Path $Symlink) -or ($mode -eq "install")) {
$v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name $v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name
Download-Newt $v; Setup-Service; Setup-Task Download-Newt $v; Setup-Service; Setup-Task
@@ -141,18 +116,16 @@ elseif ($mode -eq "update" -or (Test-Path $Symlink)) {
$v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name $v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name
$vO = $v.TrimStart('v') $vO = $v.TrimStart('v')
if (!(Test-Path "${InstallDir}\newt_${vO}.exe")) { if (!(Test-Path "${InstallDir}\newt_${vO}.exe")) {
Write-Log "Update verfügbar..." "Cyan"
Download-Newt $v Download-Newt $v
} else { } else {
Write-Log "System aktuell." "Cyan" Write-Log "System aktuell ($vO)." "Cyan"
Setup-Service Setup-Service # Sicherstellen dass Logs gehen
if ((Get-Service $ServiceName).Status.value__ -ne 4) { Start-Service $ServiceName } if ((Get-Service $ServiceName).Status.value__ -ne 4) { Start-Service $ServiceName }
} }
} }
elseif ($mode -eq "uninstall") { elseif ($mode -eq "uninstall") {
taskkill.exe /F /T /IM "newt*" 2>$null
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Set-Service $ServiceName -StartupType Disabled
taskkill.exe /F /T /IM "newt*" 2>$null
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
& nssm remove $ServiceName confirm & nssm remove $ServiceName confirm
} }