diff --git a/install_newt-msp-site-win_v2.ps1 b/install_newt-msp-site-win_v2.ps1 index af0f7c3..fa02086 100644 --- a/install_newt-msp-site-win_v2.ps1 +++ b/install_newt-msp-site-win_v2.ps1 @@ -1,7 +1,6 @@ <# .SYNOPSIS - Windows-Installer für den Newt-Client (MAIEREDV). - v2.6 - Fix für Parser-Error (Variable Drive Reference) & Encoding. + v2.7 - Brutal-Rename-Strategy & Immediate Kill. #> param([string]$mode = "install") @@ -13,7 +12,6 @@ $InstallDir = "C:\Program Files\me-msp-newt" $ServiceName = "MAIEREDV-Managed-Site-Client" $Symlink = "$InstallDir\newt_latest.exe" $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" function Write-Log($msg, $color = "White") { @@ -27,50 +25,55 @@ function Download-Newt { $Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}" $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)) { Write-Log "Download $VersionOnly..." "Cyan" - try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop } - catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing } + try { Start-BitsTransfer $Url $Target -Priority Foreground -ErrorAction Stop } + 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) { Set-Service $ServiceName -StartupType Disabled Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue } - # Prozess-Kill ohne Doppelpunkt-Variablen-Falle - for ($i = 1; $i -le 5; $i++) { - $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 - Start-Sleep -Seconds 3 - } else { - break + # 2. Sofort-Kill ohne langes Warten + Write-Log "Sende Taskkill..." "Red" + taskkill.exe /F /T /IM "newt*" 2>$null + Start-Sleep -Seconds 2 + + # 3. DER RENAME-TRICK (Die Brechstange) + # Wir versuchen nicht zu loeschen, wir schieben die Leiche einfach zur Seite + if (Test-Path $Symlink) { + $DeadFile = "$Symlink.zombie_" + (Get-Date -Format "yyyyMMdd_HHmmss") + try { + # Move-Item auf derselben Partition klappt oft trotz Lock! + Move-Item -Path $Symlink -Destination $DeadFile -Force -ErrorAction Stop + Write-Log "Pfad freigemacht (Zombie verschoben)." "Green" + } catch { + Write-Log "Dateisperre extrem hartnaeckig. Versuche drueberzukopieren..." "Yellow" } } + # 4. Neue Datei platzieren try { - if (Test-Path $Symlink) { - $TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss") - Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction Stop - Write-Log "Alte Datei verschoben." "Green" - } - Copy-Item -Path $Target -Destination $Symlink -Force - Write-Log "Update erfolgreich." "Green" + Copy-Item -Path $Target -Destination $Symlink -Force -ErrorAction Stop + Write-Log "Update erfolgreich eingespielt." "Green" } catch { - Write-Log "Dateisperre konnte nicht gelöst werden." "Red" + 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 Start-Service $ServiceName -ErrorAction SilentlyContinue } +# --- Restliche Funktionen (Setup-Service, Setup-Task etc.) --- function Setup-Service { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { Write-Log "Setup Dienst..." "Yellow" @@ -90,10 +93,10 @@ function Setup-Task { $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCmd`"" $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 + 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 winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch {} } 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 & nssm remove $ServiceName confirm } - Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue + Unregister-ScheduledTask "MAIEREDV-Newt-Updater" -Confirm:$false -ErrorAction SilentlyContinue } \ No newline at end of file