install_newt-msp-site-win_v2.ps1 aktualisiert

This commit is contained in:
2026-03-30 10:39:50 +02:00
parent 1d3ce2bb88
commit 6276270834
+32 -29
View File
@@ -1,7 +1,6 @@
<# <#
.SYNOPSIS .SYNOPSIS
Windows-Installer für den Newt-Client (MAIEREDV). v2.7 - Brutal-Rename-Strategy & Immediate Kill.
v2.6 - Fix für Parser-Error (Variable Drive Reference) & Encoding.
#> #>
param([string]$mode = "install") param([string]$mode = "install")
@@ -13,7 +12,6 @@ $InstallDir = "C:\Program Files\me-msp-newt"
$ServiceName = "MAIEREDV-Managed-Site-Client" $ServiceName = "MAIEREDV-Managed-Site-Client"
$Symlink = "$InstallDir\newt_latest.exe" $Symlink = "$InstallDir\newt_latest.exe"
$LogFile = "$InstallDir\newt_service.log" $LogFile = "$InstallDir\newt_service.log"
$UpdaterTaskName = "MAIEREDV-Newt-Updater"
$GiteaUrl = "https://me-gitea.maieredv.cloud/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site-win_v2.ps1" $GiteaUrl = "https://me-gitea.maieredv.cloud/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site-win_v2.ps1"
function Write-Log($msg, $color = "White") { function Write-Log($msg, $color = "White") {
@@ -27,50 +25,55 @@ function Download-Newt {
$Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}" $Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}"
$Target = "${InstallDir}\newt_${VersionOnly}.exe" $Target = "${InstallDir}\newt_${VersionOnly}.exe"
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } if (!(Test-Path $InstallDir)) { New-Item $InstallDir -ItemType Directory -Force | Out-Null }
if (!(Test-Path $Target)) { if (!(Test-Path $Target)) {
Write-Log "Download $VersionOnly..." "Cyan" Write-Log "Download $VersionOnly..." "Cyan"
try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop } try { Start-BitsTransfer $Url $Target -Priority Foreground -ErrorAction Stop }
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing } catch { Invoke-WebRequest $Url -OutFile $Target -UseBasicParsing }
} }
Write-Log "Vorbereitung Datei-Update..." "Yellow" Write-Log "Erzwinge Dateizugriff..." "Yellow"
# 1. Dienst sofort hart auf Eis legen
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Set-Service $ServiceName -StartupType Disabled Set-Service $ServiceName -StartupType Disabled
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
} }
# Prozess-Kill ohne Doppelpunkt-Variablen-Falle # 2. Sofort-Kill ohne langes Warten
for ($i = 1; $i -le 5; $i++) { Write-Log "Sende Taskkill..." "Red"
$Procs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
if ($Procs) {
$pCount = $Procs.Count
Write-Log "Versuch $i - Kill hängende Prozesse ($pCount)..." "Red"
taskkill.exe /F /T /IM "newt*" 2>$null taskkill.exe /F /T /IM "newt*" 2>$null
Start-Sleep -Seconds 3 Start-Sleep -Seconds 2
} else {
break
}
}
try { # 3. DER RENAME-TRICK (Die Brechstange)
# Wir versuchen nicht zu loeschen, wir schieben die Leiche einfach zur Seite
if (Test-Path $Symlink) { if (Test-Path $Symlink) {
$TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss") $DeadFile = "$Symlink.zombie_" + (Get-Date -Format "yyyyMMdd_HHmmss")
Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction Stop try {
Write-Log "Alte Datei verschoben." "Green" # Move-Item auf derselben Partition klappt oft trotz Lock!
} Move-Item -Path $Symlink -Destination $DeadFile -Force -ErrorAction Stop
Copy-Item -Path $Target -Destination $Symlink -Force Write-Log "Pfad freigemacht (Zombie verschoben)." "Green"
Write-Log "Update erfolgreich." "Green"
} catch { } catch {
Write-Log "Dateisperre konnte nicht gelöst werden." "Red" Write-Log "Dateisperre extrem hartnaeckig. Versuche drueberzukopieren..." "Yellow"
}
} }
# 4. Neue Datei platzieren
try {
Copy-Item -Path $Target -Destination $Symlink -Force -ErrorAction Stop
Write-Log "Update erfolgreich eingespielt." "Green"
} catch {
Write-Log "KRITISCH: Update fehlgeschlagen. Datei wird vom Kernel blockiert." "Red"
Write-Log "Bitte den Server bei Gelegenheit neu starten." "Yellow"
}
# Dienst wieder reaktivieren
Set-Service $ServiceName -StartupType Automatic Set-Service $ServiceName -StartupType Automatic
Start-Service $ServiceName -ErrorAction SilentlyContinue Start-Service $ServiceName -ErrorAction SilentlyContinue
} }
# --- Restliche Funktionen (Setup-Service, Setup-Task etc.) ---
function Setup-Service { function Setup-Service {
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
Write-Log "Setup Dienst..." "Yellow" Write-Log "Setup Dienst..." "Yellow"
@@ -90,10 +93,10 @@ function Setup-Task {
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCmd`"" $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCmd`""
$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 "MAIEREDV-Newt-Updater" -Force | Out-Null
} }
# --- Main Logic --- # --- Main ---
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) { if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
if (!(Get-Command winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch {} } if (!(Get-Command winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch {} }
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
@@ -122,5 +125,5 @@ elseif ($mode -eq "uninstall") {
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
& nssm remove $ServiceName confirm & nssm remove $ServiceName confirm
} }
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue Unregister-ScheduledTask "MAIEREDV-Newt-Updater" -Confirm:$false -ErrorAction SilentlyContinue
} }