install_newt-msp-site-win_v2.ps1 aktualisiert
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Windows-Installer für den Newt-Client (MAIEREDV).
|
Windows-Installer für den Newt-Client (MAIEREDV).
|
||||||
Features: Winget.pro, NSSM, Heavy-Duty Kill (Taskkill /F /T), Rename-Move-Trick & Auto-Cleanup.
|
v2.4 - Lokalisierungs-Fix (Deutsch/Englisch) & numerische Status-Codes.
|
||||||
#>
|
#>
|
||||||
param([string]$mode = "install")
|
param([string]$mode = "install")
|
||||||
|
|
||||||
# 1. Stabilität & Encoding
|
|
||||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||||
|
|
||||||
# 2. Konfiguration
|
# --- 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"
|
||||||
@@ -22,11 +21,10 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
# 3. Umgebung vorbereiten
|
# --- 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..." "Cyan"
|
try { Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression } catch {}
|
||||||
try { Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression } catch { exit 1 }
|
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
}
|
}
|
||||||
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
||||||
@@ -35,7 +33,7 @@ function Prepare-Environment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# 4. Download & Brutal-Kill mit Rename-Trick
|
# --- 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" }
|
||||||
@@ -51,39 +49,44 @@ function Download-Newt {
|
|||||||
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- AGGRESSIVER STOPP ---
|
# --- AGGRESSIVER STOPP (Sprachen-unabhängig) ---
|
||||||
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
if ($Svc -and $Svc.Status -ne 'Stopped') {
|
if ($Svc) {
|
||||||
Write-Log "Dienst klemmt (Status: $($Svc.Status)). Deaktiviere Autostart..." "Yellow"
|
# Status 1 = Stopped / Beendet
|
||||||
Set-Service $ServiceName -StartupType Disabled
|
if ($Svc.Status.value__ -ne 1) {
|
||||||
|
Write-Log "Dienst ist nicht beendet (Status-Code: $($Svc.Status.value__)). Deaktiviere..." "Yellow"
|
||||||
# Versuch 1: Sanft
|
Set-Service $ServiceName -StartupType Disabled
|
||||||
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
|
# Wir warten bis Status == 1
|
||||||
if ($Procs) {
|
while (($Svc.Status.value__ -ne 1) -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
|
||||||
foreach ($p in $Procs) {
|
Start-Sleep -Seconds 2
|
||||||
Write-Log "Sende Hard-Kill (Taskkill /F /T) an PID $($p.Id)..." "Red"
|
$Svc.Refresh()
|
||||||
taskkill.exe /F /T /PID $($p.Id) 2>$null
|
|
||||||
}
|
}
|
||||||
Start-Sleep -Seconds 3
|
$timer.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
# Wenn er immer noch klemmt -> TASKKILL
|
||||||
|
if ($Svc.Status.value__ -ne 1) {
|
||||||
|
Write-Log "Dienst hängt immer noch. Sende Taskkill /F /T..." "Red"
|
||||||
|
taskkill.exe /F /T /IM "newt*" /FI "STATUS eq RUNNING" 2>$null
|
||||||
|
taskkill.exe /F /T /IM "newt*" 2>$null
|
||||||
|
Start-Sleep -Seconds 5
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- RENAME-TRICK (Falls Datei noch 'In Use' ist) ---
|
# --- RENAME-MOVE TRICK ---
|
||||||
try {
|
try {
|
||||||
if (Test-Path $Symlink) {
|
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")
|
$TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss")
|
||||||
Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction SilentlyContinue
|
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 ersetzt." "Green"
|
Write-Log "Datei erfolgreich ersetzt." "Green"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "KRITISCH: Datei blockiert. Update wird nach Reboot aktiv." "Red"
|
Write-Log "DATEI-LOCK: Konnte $Symlink nicht ersetzen!" "Red"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dienst wieder scharf schalten
|
# Dienst wieder scharf schalten
|
||||||
@@ -91,11 +94,10 @@ function Download-Newt {
|
|||||||
Start-Service $ServiceName -ErrorAction SilentlyContinue
|
Start-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
Write-Log "Dienst neu gestartet." "Green"
|
Write-Log "Dienst neu gestartet." "Green"
|
||||||
|
|
||||||
# Cleanup: Alte Versionen UND .dead Leichen entfernen
|
# Cleanup
|
||||||
Get-ChildItem -Path $InstallDir -Filter "newt_*" |
|
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 3 | Remove-Item -Force -ErrorAction SilentlyContinue
|
||||||
Select-Object -Skip 3 | Remove-Item -Force -ErrorAction SilentlyContinue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Setup-Service {
|
function Setup-Service {
|
||||||
@@ -111,7 +113,6 @@ function Setup-Service {
|
|||||||
& nssm set $ServiceName AppRotateBytes 10485760
|
& nssm set $ServiceName AppRotateBytes 10485760
|
||||||
Start-Service $ServiceName
|
Start-Service $ServiceName
|
||||||
} else {
|
} else {
|
||||||
# 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
|
||||||
@@ -126,7 +127,7 @@ 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 (IEX) registriert." "Green"
|
Write-Log "Update-Task registriert." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Main Logic ---
|
# --- Main Logic ---
|
||||||
@@ -134,18 +135,18 @@ 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; Setup-Service; Setup-Task
|
Download-Newt $v; Setup-Service; Setup-Task
|
||||||
Write-Log "🚀 Installation fertig!" "Green"
|
Write-Log "Installation fertig!" "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 "Neue Version $vO gefunden..." "Cyan"
|
Write-Log "Update verfügbar..." "Cyan"
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
} else {
|
} else {
|
||||||
Write-Log "System aktuell ($vO)." "Cyan"
|
Write-Log "System aktuell." "Cyan"
|
||||||
Setup-Service # Log-Fix falls nötig
|
Setup-Service
|
||||||
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
if ((Get-Service $ServiceName).Status.value__ -ne 4) { Start-Service $ServiceName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
elseif ($mode -eq "uninstall") {
|
elseif ($mode -eq "uninstall") {
|
||||||
@@ -156,5 +157,4 @@ elseif ($mode -eq "uninstall") {
|
|||||||
& 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 "Alles entfernt." "Green"
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user