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, 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")
|
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 {
|
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" }
|
||||||
@@ -54,33 +54,40 @@ function Download-Newt {
|
|||||||
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
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
|
$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) {
|
if ($Svc.Status -ne 'Stopped') {
|
||||||
Write-Log "Beende Dienst $ServiceName (Warte max 30s)..." "Yellow"
|
Write-Log "Dienst $ServiceName ist im Status '$($Svc.Status)'. Versuche Stop..." "Yellow"
|
||||||
Stop-Service $ServiceName -Force
|
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
$timeout = 30
|
$timeout = 30
|
||||||
$timer = [System.Diagnostics.Stopwatch]::StartNew()
|
$timer = [System.Diagnostics.Stopwatch]::StartNew()
|
||||||
while (((Get-Service $ServiceName).Status -ne 'Stopped') -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
|
while (((Get-Service $ServiceName).Status -ne 'Stopped') -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
|
||||||
Start-Sleep -Seconds 2
|
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') {
|
if ((Get-Service $ServiceName).Status -ne 'Stopped') {
|
||||||
Write-Log "Dienst klemmt! Erzeinge Abbruch (Hard-Kill)..." "Red"
|
Write-Log "Dienst reagiert nicht (Status: $((Get-Service $ServiceName).Status)). Erzeuge Hard-Kill!" "Red"
|
||||||
Stop-Process -Name "newt*" -Force -ErrorAction SilentlyContinue
|
$NewtProcs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue
|
||||||
Start-Sleep -Seconds 2
|
if ($NewtProcs) {
|
||||||
|
$NewtProcs | Stop-Process -Force -ErrorAction SilentlyContinue
|
||||||
|
Write-Log "Prozesse hart beendet." "Yellow"
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else { $WasRunning = $false }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
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 auf $VersionOnly getauscht." "Green"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "FEHLER: Datei $Symlink ist gesperrt!" "Red"
|
Write-Log "FEHLER: Datei $Symlink ist trotz Kill gesperrt! Eventuell manueller Zugriff?" "Red"
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($WasRunning) {
|
if ($WasRunning) {
|
||||||
@@ -88,7 +95,7 @@ function Download-Newt {
|
|||||||
Write-Log "Dienst wurde neu gestartet." "Green"
|
Write-Log "Dienst wurde neu gestartet." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup: Behalte die neuesten 2 Versionen
|
||||||
Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
|
Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
|
||||||
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
||||||
Sort-Object LastWriteTime -Descending |
|
Sort-Object LastWriteTime -Descending |
|
||||||
@@ -103,23 +110,16 @@ function Setup-Service {
|
|||||||
$PangolinEndpoint = Read-Host "Endpoint"
|
$PangolinEndpoint = Read-Host "Endpoint"
|
||||||
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
||||||
|
|
||||||
Write-Log "Erstelle Dienst und setze Log-Pfade..." "Cyan"
|
|
||||||
& nssm install $ServiceName "$Symlink" $ArgList
|
& nssm install $ServiceName "$Symlink" $ArgList
|
||||||
|
|
||||||
# Log-Pfade setzen
|
|
||||||
& nssm set $ServiceName AppStdout "$LogFile"
|
& nssm set $ServiceName AppStdout "$LogFile"
|
||||||
& nssm set $ServiceName AppStderr "$LogFile"
|
& nssm set $ServiceName AppStderr "$LogFile"
|
||||||
|
|
||||||
# Rotation
|
|
||||||
& nssm set $ServiceName AppRotateFiles 1
|
& nssm set $ServiceName AppRotateFiles 1
|
||||||
& 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"
|
Write-Log "Dienst aktiv. Logs: $LogFile" "Green"
|
||||||
} else {
|
} else {
|
||||||
# Falls der Dienst existiert, aber die Logs fehlen (Nachbesserung)
|
# Log-Pfade nachrüsten falls nötig
|
||||||
Write-Log "Prüfe Log-Konfiguration..." "Cyan"
|
|
||||||
& 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
|
||||||
@@ -130,13 +130,12 @@ function Setup-Service {
|
|||||||
|
|
||||||
function Setup-Task {
|
function Setup-Task {
|
||||||
$IexCommand = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl'))"
|
$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`""
|
$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 scharfgeschaltet." "Green"
|
Write-Log "Update-Task (03:00 Uhr) registriert." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Main Logic ---
|
# --- Main Logic ---
|
||||||
@@ -157,15 +156,19 @@ elseif ($mode -eq "update" -or (Test-Path $Symlink)) {
|
|||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
} else {
|
} else {
|
||||||
Write-Log "System ist aktuell ($vO)." "Cyan"
|
Write-Log "System ist aktuell ($vO)." "Cyan"
|
||||||
# Log-Pfade auch beim Update sicherstellen
|
Setup-Service # Sicherstellen, dass Logs & Rotation stimmen
|
||||||
Setup-Service
|
|
||||||
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) {
|
||||||
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
|
& 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"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user