install_newt-msp-site-win_v2.ps1 aktualisiert
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Windows-Installer für den Newt-Client (MAIEREDV).
|
Windows-Installer für den Newt-Client (MAIEREDV).
|
||||||
Features: Winget.pro, NSSM, Hard-Kill (Stopping-Fix), 10MB Log-Rotation, Auto-Cleanup.
|
Features: Winget.pro, NSSM, Heavy-Duty Kill (Taskkill /F /T), Rename-Move-Trick & Auto-Cleanup.
|
||||||
#>
|
#>
|
||||||
param([string]$mode = "install")
|
param([string]$mode = "install")
|
||||||
|
|
||||||
@@ -25,20 +25,17 @@ function Write-Log($msg, $color = "White") {
|
|||||||
# 3. Umgebung vorbereiten
|
# 3. Umgebung vorbereiten
|
||||||
function Prepare-Environment {
|
function Prepare-Environment {
|
||||||
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
||||||
Write-Log "Winget fehlt. Installiere via winget.pro..." "Cyan"
|
Write-Log "Winget fehlt. Installiere..." "Cyan"
|
||||||
try {
|
try { Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression } catch { exit 1 }
|
||||||
Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
|
||||||
} catch { Write-Log "FEHLER: Winget-Basis fehlt." "Red"; exit 1 }
|
|
||||||
}
|
}
|
||||||
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
||||||
Write-Log "NSSM wird via Winget geladen..." "Cyan"
|
|
||||||
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# 4. Download & Verbesserter Hard-Kill (Stopping-Fix)
|
# 4. Download & Brutal-Kill mit Rename-Trick
|
||||||
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,67 +46,63 @@ function Download-Newt {
|
|||||||
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
||||||
|
|
||||||
if (!(Test-Path $Target)) {
|
if (!(Test-Path $Target)) {
|
||||||
Write-Log "Downloade Version $VersionOnly..." "Cyan"
|
Write-Log "Download $VersionOnly..." "Cyan"
|
||||||
try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop }
|
try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop }
|
||||||
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Dienst-Stopp mit verbessertem Hard-Kill ---
|
# --- AGGRESSIVER STOPP ---
|
||||||
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
if ($Svc) {
|
if ($Svc -and $Svc.Status -ne 'Stopped') {
|
||||||
$WasRunning = $true # Wir gehen davon aus, dass wir ihn nachher wieder starten wollen
|
Write-Log "Dienst klemmt (Status: $($Svc.Status)). Deaktiviere Autostart..." "Yellow"
|
||||||
|
Set-Service $ServiceName -StartupType Disabled
|
||||||
|
|
||||||
if ($Svc.Status -ne 'Stopped') {
|
# Versuch 1: Sanft
|
||||||
Write-Log "Dienst $ServiceName ist im Status '$($Svc.Status)'. Versuche Stop..." "Yellow"
|
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
|
||||||
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
|
Start-Sleep -Seconds 5
|
||||||
|
|
||||||
$timeout = 30
|
# Versuch 2: Taskkill Tree (Härter als Stop-Process)
|
||||||
$timer = [System.Diagnostics.Stopwatch]::StartNew()
|
$Procs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
|
||||||
while (((Get-Service $ServiceName).Status -ne 'Stopped') -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
|
if ($Procs) {
|
||||||
Start-Sleep -Seconds 2
|
foreach ($p in $Procs) {
|
||||||
|
Write-Log "Sende Hard-Kill (Taskkill /F /T) an PID $($p.Id)..." "Red"
|
||||||
|
taskkill.exe /F /T /PID $($p.Id) 2>$null
|
||||||
}
|
}
|
||||||
$timer.Stop()
|
Start-Sleep -Seconds 3
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Wenn er immer noch nicht Stopped ist (z.B. hängt in 'Stopping') -> TASKKILL
|
# --- RENAME-TRICK (Falls Datei noch 'In Use' ist) ---
|
||||||
if ((Get-Service $ServiceName).Status -ne 'Stopped') {
|
|
||||||
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 {
|
try {
|
||||||
|
if (Test-Path $Symlink) {
|
||||||
|
Write-Log "Versuche Datei-Tausch..." "Cyan"
|
||||||
|
# Wir versuchen die Datei umzubenennen, falls Überschreiben fehlschlägt
|
||||||
|
$TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss")
|
||||||
|
Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
Copy-Item -Path $Target -Destination $Symlink -Force
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
||||||
Write-Log "Datei erfolgreich auf $VersionOnly getauscht." "Green"
|
Write-Log "Datei erfolgreich ersetzt." "Green"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "FEHLER: Datei $Symlink ist trotz Kill gesperrt! Eventuell manueller Zugriff?" "Red"
|
Write-Log "KRITISCH: Datei blockiert. Update wird nach Reboot aktiv." "Red"
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($WasRunning) {
|
# Dienst wieder scharf schalten
|
||||||
Start-Service $ServiceName
|
Set-Service $ServiceName -StartupType Automatic
|
||||||
Write-Log "Dienst wurde neu gestartet." "Green"
|
Start-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
}
|
Write-Log "Dienst neu gestartet." "Green"
|
||||||
|
|
||||||
# Cleanup: Behalte die neuesten 2 Versionen
|
# Cleanup: Alte Versionen UND .dead Leichen entfernen
|
||||||
Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
|
Get-ChildItem -Path $InstallDir -Filter "newt_*" |
|
||||||
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
||||||
Sort-Object LastWriteTime -Descending |
|
Sort-Object LastWriteTime -Descending |
|
||||||
Select-Object -Skip 2 | Remove-Item -Force
|
Select-Object -Skip 3 | Remove-Item -Force -ErrorAction SilentlyContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
function Setup-Service {
|
function Setup-Service {
|
||||||
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
||||||
Write-Log "--- Erst-Konfiguration ---" "Yellow"
|
Write-Log "--- Erst-Konfiguration ---" "Yellow"
|
||||||
$PangolinID = Read-Host "Pangolin ID"
|
$PangolinID = Read-Host "ID"; $PangolinSecret = Read-Host "Secret"; $PangolinEndpoint = Read-Host "Endpoint"
|
||||||
$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 AppStdout "$LogFile"
|
||||||
& nssm set $ServiceName AppStderr "$LogFile"
|
& nssm set $ServiceName AppStderr "$LogFile"
|
||||||
@@ -117,9 +110,8 @@ function Setup-Service {
|
|||||||
& nssm set $ServiceName AppRotateOnline 1
|
& nssm set $ServiceName AppRotateOnline 1
|
||||||
& nssm set $ServiceName AppRotateBytes 10485760
|
& nssm set $ServiceName AppRotateBytes 10485760
|
||||||
Start-Service $ServiceName
|
Start-Service $ServiceName
|
||||||
Write-Log "Dienst aktiv. Logs: $LogFile" "Green"
|
|
||||||
} else {
|
} else {
|
||||||
# Log-Pfade nachrüsten falls nötig
|
# Log-Repair für bestehende Dienste
|
||||||
& nssm set $ServiceName AppStdout "$LogFile"
|
& nssm set $ServiceName AppStdout "$LogFile"
|
||||||
& nssm set $ServiceName AppStderr "$LogFile"
|
& nssm set $ServiceName AppStderr "$LogFile"
|
||||||
& nssm set $ServiceName AppRotateFiles 1
|
& nssm set $ServiceName AppRotateFiles 1
|
||||||
@@ -133,42 +125,36 @@ function Setup-Task {
|
|||||||
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCommand`""
|
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCommand`""
|
||||||
$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 (03:00 Uhr) registriert." "Green"
|
Write-Log "Update-Task (IEX) registriert." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Main Logic ---
|
# --- Main Logic ---
|
||||||
Prepare-Environment
|
Prepare-Environment
|
||||||
|
|
||||||
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
|
Download-Newt $v; Setup-Service; Setup-Task
|
||||||
Setup-Service
|
Write-Log "🚀 Installation fertig!" "Green"
|
||||||
Setup-Task
|
|
||||||
Write-Log "🚀 Installation abgeschlossen!" "Green"
|
|
||||||
}
|
}
|
||||||
elseif ($mode -eq "update" -or (Test-Path $Symlink)) {
|
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 auf $vO verfügbar..." "Cyan"
|
Write-Log "Neue Version $vO gefunden..." "Cyan"
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
} else {
|
} else {
|
||||||
Write-Log "System ist aktuell ($vO)." "Cyan"
|
Write-Log "System aktuell ($vO)." "Cyan"
|
||||||
Setup-Service # Sicherstellen, dass Logs & Rotation stimmen
|
Setup-Service # Log-Fix falls nötig
|
||||||
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
elseif ($mode -eq "uninstall") {
|
elseif ($mode -eq "uninstall") {
|
||||||
Write-Log "Entferne Dienst..." "Yellow"
|
|
||||||
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
||||||
# Auch hier Hard-Kill Fallback nutzen
|
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
|
||||||
$NewtProcs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
|
|
||||||
if ($NewtProcs) { $NewtProcs | Stop-Process -Force }
|
|
||||||
& nssm remove $ServiceName confirm
|
& nssm remove $ServiceName confirm
|
||||||
}
|
}
|
||||||
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
||||||
Write-Log "Deinstallation fertig." "Green"
|
Write-Log "Alles entfernt." "Green"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user