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 Managed Site Client).
|
Windows-Installer für den Newt-Client.
|
||||||
Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation & Safe-Update.
|
Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation, Safe-Update & Auto-Cleanup.
|
||||||
#>
|
#>
|
||||||
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"
|
||||||
@@ -21,7 +20,7 @@ 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 (Winget & NSSM)
|
# --- 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 via winget.pro..." "Cyan"
|
||||||
@@ -50,6 +49,7 @@ function Get-LatestVersion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- Download & Cleanup ---
|
||||||
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" }
|
||||||
@@ -59,20 +59,18 @@ 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 }
|
||||||
|
|
||||||
Write-Log "Prüfe/Downloade Version $VersionOnly..." "Cyan"
|
# 1. Download
|
||||||
|
|
||||||
# Download nur wenn Datei noch nicht existiert
|
|
||||||
if (!(Test-Path $Target)) {
|
if (!(Test-Path $Target)) {
|
||||||
|
Write-Log "Downloade Version $VersionOnly..." "Cyan"
|
||||||
try {
|
try {
|
||||||
Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop
|
Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop
|
||||||
Write-Log "Download erfolgreich." "Green"
|
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow"
|
Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow"
|
||||||
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing
|
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Datei-Update (Safe-Swap)
|
# 2. Safe-Swap (Dienst stoppen/starten)
|
||||||
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
$WasRunning = $Service -and $Service.Status -eq 'Running'
|
$WasRunning = $Service -and $Service.Status -eq 'Running'
|
||||||
|
|
||||||
@@ -83,15 +81,28 @@ function Download-Newt {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Copy-Item -Path $Target -Destination $Symlink -Force
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
||||||
Write-Log "newt_latest.exe wurde auf Stand $VersionOnly aktualisiert." "Green"
|
Write-Log "newt_latest.exe auf Stand $VersionOnly aktualisiert." "Green"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "FEHLER: Konnte Symlink nicht überschreiben. Datei evtl. blockiert." "Red"
|
Write-Log "FEHLER: Datei blockiert." "Red"
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($WasRunning) {
|
if ($WasRunning) {
|
||||||
Start-Service $ServiceName
|
Start-Service $ServiceName
|
||||||
Write-Log "Dienst wieder gestartet." "Green"
|
Write-Log "Dienst wieder gestartet." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 3. AUFRÄUMEN (Cleanup)
|
||||||
|
# Wir behalten die newt_latest.exe und die letzten 2 Versions-Exen
|
||||||
|
Write-Log "Räume alte Versionen auf..." "Yellow"
|
||||||
|
$OldVersions = Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
|
||||||
|
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
||||||
|
Sort-Object LastWriteTime -Descending |
|
||||||
|
Select-Object -Skip 2 # Die neuesten 2 bleiben zur Sicherheit da
|
||||||
|
|
||||||
|
foreach ($file in $OldVersions) {
|
||||||
|
Write-Log "Lösche alte Version: $($file.Name)" "Gray"
|
||||||
|
Remove-Item $file.FullName -Force
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Setup-Service {
|
function Setup-Service {
|
||||||
@@ -101,11 +112,8 @@ function Setup-Service {
|
|||||||
$PangolinSecret = Read-Host "Bitte Secret eingeben"
|
$PangolinSecret = Read-Host "Bitte Secret eingeben"
|
||||||
$PangolinEndpoint = Read-Host "Bitte Endpoint eingeben"
|
$PangolinEndpoint = Read-Host "Bitte Endpoint eingeben"
|
||||||
|
|
||||||
if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID darf nicht leer sein!" "Red"; exit 1 }
|
|
||||||
|
|
||||||
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
||||||
|
|
||||||
Write-Log "Erstelle Dienst mit NSSM..." "Cyan"
|
|
||||||
& nssm install $ServiceName "$Symlink" $ArgList
|
& nssm install $ServiceName "$Symlink" $ArgList
|
||||||
& nssm set $ServiceName Description "MAIEREDV Managed Site Client"
|
& nssm set $ServiceName Description "MAIEREDV Managed Site Client"
|
||||||
& nssm set $ServiceName AppExit Default Restart
|
& nssm set $ServiceName AppExit Default Restart
|
||||||
@@ -119,9 +127,7 @@ function Setup-Service {
|
|||||||
& nssm set $ServiceName AppRotateBytes 10485760
|
& nssm set $ServiceName AppRotateBytes 10485760
|
||||||
|
|
||||||
Start-Service $ServiceName
|
Start-Service $ServiceName
|
||||||
Write-Log "Dienst erfolgreich gestartet." "Green"
|
Write-Log "Dienst gestartet." "Green"
|
||||||
} else {
|
|
||||||
Write-Log "Dienst vorhanden. Führe Update-Check aus..." "Yellow"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +159,6 @@ switch ($mode) {
|
|||||||
if (!(Test-Path $TargetVersionPath)) {
|
if (!(Test-Path $TargetVersionPath)) {
|
||||||
$NeedsUpdate = $true
|
$NeedsUpdate = $true
|
||||||
} elseif (Test-Path $Symlink) {
|
} elseif (Test-Path $Symlink) {
|
||||||
# Hash-Vergleich um sicherzugehen, dass latest.exe wirklich die neue Version ist
|
|
||||||
$HashLatest = (Get-FileHash $Symlink).Hash
|
$HashLatest = (Get-FileHash $Symlink).Hash
|
||||||
$HashTarget = (Get-FileHash $TargetVersionPath).Hash
|
$HashTarget = (Get-FileHash $TargetVersionPath).Hash
|
||||||
if ($HashLatest -ne $HashTarget) { $NeedsUpdate = $true }
|
if ($HashLatest -ne $HashTarget) { $NeedsUpdate = $true }
|
||||||
@@ -165,17 +170,8 @@ switch ($mode) {
|
|||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
Write-Log "🚀 Update auf $v durchgeführt." "Green"
|
Write-Log "🚀 Update auf $v durchgeführt." "Green"
|
||||||
} else {
|
} else {
|
||||||
Write-Log "System ist bereits aktuell ($vOnly)." "Cyan"
|
Write-Log "System ist aktuell ($vOnly)." "Cyan"
|
||||||
# Falls der Dienst aus irgendeinem Grund steht, starten wir ihn hier
|
|
||||||
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"uninstall" {
|
"uninstall
|
||||||
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
|
||||||
Stop-Service $ServiceName -Force
|
|
||||||
& nssm remove $ServiceName confirm
|
|
||||||
}
|
|
||||||
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
|
||||||
Write-Log "Deinstalliert." "Green"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user