install_newt-msp-site-win_v2.ps1 aktualisiert
This commit is contained in:
@@ -1,23 +1,40 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
v2.7 - Brutal-Rename-Strategy & Immediate Kill.
|
Windows-Installer für den Newt-Client (MAIEREDV).
|
||||||
|
v2.9 - Direct-Action: Taskkill first, then Service-Cleanup.
|
||||||
|
Kein Rename-Hokuspokus, dafür sauberer Datei-Tausch.
|
||||||
#>
|
#>
|
||||||
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
|
||||||
$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"
|
||||||
$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") {
|
||||||
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color
|
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 3. Umgebung (NSSM / Winget)
|
||||||
|
function Prepare-Environment {
|
||||||
|
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
||||||
|
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
||||||
|
try { Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression } catch {}
|
||||||
|
}
|
||||||
|
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
||||||
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 4. Der aggressive Update-Prozess
|
||||||
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" }
|
||||||
@@ -25,55 +42,46 @@ 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 $InstallDir -ItemType Directory -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 "Download $VersionOnly..." "Cyan"
|
Write-Log "Download $VersionOnly..." "Cyan"
|
||||||
try { Start-BitsTransfer $Url $Target -Priority Foreground -ErrorAction Stop }
|
try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop }
|
||||||
catch { Invoke-WebRequest $Url -OutFile $Target -UseBasicParsing }
|
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Log "Erzwinge Dateizugriff..." "Yellow"
|
Write-Log "Löse Dateisperren..." "Yellow"
|
||||||
|
|
||||||
# 1. Dienst sofort hart auf Eis legen
|
# --- SCHRITT 1: SOFORT-KILL (Macht den Pfad frei) ---
|
||||||
|
Write-Log "Beende Prozesse (taskkill)..." "Red"
|
||||||
|
taskkill.exe /F /T /IM "newt*" 2>$null
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
|
||||||
|
# --- SCHRITT 2: DIENST-REINIGUNG ---
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
# 2. Sofort-Kill ohne langes Warten
|
# --- SCHRITT 3: DATEI-TAUSCH ---
|
||||||
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 {
|
try {
|
||||||
Copy-Item -Path $Target -Destination $Symlink -Force -ErrorAction Stop
|
Copy-Item -Path $Target -Destination $Symlink -Force -ErrorAction Stop
|
||||||
Write-Log "Update erfolgreich eingespielt." "Green"
|
Write-Log "Update erfolgreich kopiert." "Green"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "KRITISCH: Update fehlgeschlagen. Datei wird vom Kernel blockiert." "Red"
|
Write-Log "FEHLER: Datei konnte nicht ersetzt werden. Handle noch offen?" "Red"
|
||||||
Write-Log "Bitte den Server bei Gelegenheit neu starten." "Yellow"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dienst wieder reaktivieren
|
# --- SCHRITT 4: REAKTIVIERUNG ---
|
||||||
Set-Service $ServiceName -StartupType Automatic
|
Set-Service $ServiceName -StartupType Automatic
|
||||||
Start-Service $ServiceName -ErrorAction SilentlyContinue
|
Start-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
|
Write-Log "Dienst wieder online." "Green"
|
||||||
|
|
||||||
|
# Cleanup: Alte Versionen entfernen (Behalte 2)
|
||||||
|
Get-ChildItem -Path $InstallDir -Filter "newt_*" |
|
||||||
|
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
||||||
|
Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 | Remove-Item -Force -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"
|
||||||
@@ -81,6 +89,7 @@ function Setup-Service {
|
|||||||
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
||||||
& nssm install $ServiceName "$Symlink" $ArgList
|
& nssm install $ServiceName "$Symlink" $ArgList
|
||||||
}
|
}
|
||||||
|
# Log-Fix & Rotation
|
||||||
& 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
|
||||||
@@ -93,20 +102,16 @@ 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 "MAIEREDV-Newt-Updater" -Force | Out-Null
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $UpdaterTaskName -Force | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Main ---
|
# --- Main Logic ---
|
||||||
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
Prepare-Environment
|
||||||
if (!(Get-Command winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch {} }
|
|
||||||
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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
|
||||||
@@ -114,8 +119,8 @@ elseif ($mode -eq "update" -or (Test-Path $Symlink)) {
|
|||||||
if (!(Test-Path "${InstallDir}\newt_${vO}.exe")) {
|
if (!(Test-Path "${InstallDir}\newt_${vO}.exe")) {
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
} else {
|
} else {
|
||||||
Write-Log "System aktuell." "Cyan"
|
Write-Log "System aktuell ($vO)." "Cyan"
|
||||||
Setup-Service
|
Setup-Service # Log-Konfiguration auffrischen
|
||||||
if ((Get-Service $ServiceName).Status.value__ -ne 4) { Start-Service $ServiceName }
|
if ((Get-Service $ServiceName).Status.value__ -ne 4) { Start-Service $ServiceName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,5 +130,6 @@ 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 "MAIEREDV-Newt-Updater" -Confirm:$false -ErrorAction SilentlyContinue
|
Unregister-ScheduledTask $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
||||||
|
Write-Log "Deinstallation fertig." "Green"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user