install_newt-msp-site-win_v2.sh aktualisiert

This commit is contained in:
2026-02-18 11:11:37 +01:00
parent 10b98100e1
commit 20d8557382

View File

@@ -1,13 +1,15 @@
<# <#
.SYNOPSIS .SYNOPSIS
Windows-Installer für den Newt-Client. Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client).
Nutzt Winget für das Paketmanagement von NSSM. Nutzt winget.pro für NSSM und BITS-Foreground für High-Speed Downloads.
#> #>
param([string]$mode = "install") param([string]$mode = "install")
# 1. Stabilität & Encoding sicherstellen
[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"
@@ -15,40 +17,47 @@ $Symlink = "$InstallDir\newt_latest.exe"
$UpdaterTaskName = "MAIEREDV-Newt-Updater" $UpdaterTaskName = "MAIEREDV-Newt-Updater"
$GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1" $GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1"
function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color } # Helfer-Funktion für Ausgaben
function Write-Log($msg, $color = "White") {
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color
}
# 1. Winget & NSSM sicherstellen # 3. Umgebung vorbereiten (Winget & NSSM)
function Prepare-Environment { function Prepare-Environment {
if (!(Get-Command winget -ErrorAction SilentlyContinue)) { if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Log "Winget fehlt. Installiere Winget via winget.pro..." "Cyan" Write-Log "Winget fehlt. Installiere via winget.pro (High Speed)..." "Cyan"
try { try {
irm winget.pro | iex # Schnelle Installation von Winget auf Servern
Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression
# Pfad-Refresher
$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 { } catch {
Write-Log "FEHLER: Winget-Installation fehlgeschlagen." "Red"; exit 1 Write-Log "FEHLER: Winget konnte nicht installiert werden." "Red"; exit 1
} }
} }
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) { if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
Write-Log "NSSM wird via Winget installiert..." "Cyan" 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
# Pfad aktualisieren, falls Winget nssm gerade erst hinzugefügt hat
$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. Version abfragen
function Get-LatestVersion { function Get-LatestVersion {
try { try {
$url = "https://api.github.com/repos/$Repo/releases/latest" $url = "https://api.github.com/repos/$Repo/releases/latest"
$json = Invoke-RestMethod -Uri $url -UseBasicParsing $json = Invoke-RestMethod -Uri $url -UseBasicParsing
return $json.tag_name return $json.tag_name # z.B. "v1.9.0"
} catch { } catch {
Write-Log "FEHLER: GitHub API nicht erreichbar." "Red"; exit 1 Write-Log "FEHLER: GitHub API antwortet nicht." "Red"; exit 1
} }
} }
# 5. Download mit Turbo-BITS
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" }
$VersionOnly = $FullVersion.TrimStart('v') $VersionOnly = $FullVersion.TrimStart('v')
$Url = "https://github.com/$Repo/releases/download/$VersionOnly/newt_$ArchSuffix" $Url = "https://github.com/$Repo/releases/download/$VersionOnly/newt_$ArchSuffix"
@@ -56,15 +65,20 @@ 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 "Download Newt $VersionOnly..." "Cyan" Write-Log "Starte Turbo-Download von $VersionOnly..." "Cyan"
try { try {
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing -ErrorAction Stop # Priority Foreground umgeht die künstliche BITS-Drosselung (Hintergrundbremse)
Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop
Copy-Item -Path $Target -Destination $Symlink -Force Copy-Item -Path $Target -Destination $Symlink -Force
Write-Log "Download erfolgreich beendet." "Green"
} catch { } catch {
Write-Log "FEHLER beim Newt-Download: $_" "Red"; exit 1 Write-Log "BITS-Turbo fehlgeschlagen. Versuche Web-Fallback..." "Yellow"
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing
Copy-Item -Path $Target -Destination $Symlink -Force
} }
} }
# 6. Dienst mit NSSM erstellen
function Setup-Service { function Setup-Service {
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
Write-Log "--- Dienst-Konfiguration ---" "Yellow" Write-Log "--- Dienst-Konfiguration ---" "Yellow"
@@ -72,33 +86,38 @@ 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" Write-Log "Registriere Dienst via NSSM..." "Cyan"
# Da NSSM nun im PATH liegt, rufen wir es direkt auf # NSSM managed den Konsolenprozess perfekt
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
nssm set $ServiceName AppRestartDelay 5000 & nssm set $ServiceName AppRestartDelay 5000
nssm set $ServiceName AppStdout "$InstallDir\newt_service.log" & nssm set $ServiceName AppStdout "$InstallDir\newt_service.log"
nssm set $ServiceName AppStderr "$InstallDir\newt_service.log" & nssm set $ServiceName AppStderr "$InstallDir\newt_service.log"
Start-Service $ServiceName Start-Service $ServiceName
Write-Log "Dienst erfolgreich gestartet." "Green" Write-Log "Dienst aktiv und gestartet." "Green"
} else { } else {
Write-Log "Dienst wird aktualisiert..." "Yellow" Write-Log "Dienst bereits vorhanden. Starte neu..." "Yellow"
Restart-Service $ServiceName Restart-Service $ServiceName
} }
} }
# 7. Updater Task
function Setup-UpdaterTask { function Setup-UpdaterTask {
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl')) -mode update`"" $ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl')) -mode update`""
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $ActionCommand $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $ActionCommand
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am $Trigger = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $UpdaterTaskName -User "SYSTEM" -Force | Out-Null Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
Write-Log "Update-Timer (03:00 Uhr) ist scharfgeschaltet." "Green"
} }
# --- Main --- # --- Main Logic ---
Prepare-Environment Prepare-Environment
switch ($mode) { switch ($mode) {
@@ -107,25 +126,26 @@ switch ($mode) {
Download-Newt $v Download-Newt $v
Setup-Service Setup-Service
Setup-UpdaterTask Setup-UpdaterTask
Write-Log "Installation erfolgreich abgeschlossen!" "Green" Write-Log "🚀 Mission erfolgreich! Newt ist bereit." "Green"
} }
"update" { "update" {
$v = Get-LatestVersion $v = Get-LatestVersion
$vOnly = $v.TrimStart('v') $vOnly = $v.TrimStart('v')
if (Test-Path "$InstallDir\newt_$vOnly.exe") { if (Test-Path "$InstallDir\newt_$vOnly.exe") {
Write-Log "System ist aktuell." "Cyan" Write-Log "System ist auf dem neuesten Stand ($vOnly)." "Cyan"
} else { } else {
Download-Newt $v Download-Newt $v
Restart-Service $ServiceName Restart-Service $ServiceName
Write-Log "Update auf $v durchgefuehrt." "Green" Write-Log "🚀 Update auf $v erfolgreich eingespielt." "Green"
} }
} }
"uninstall" { "uninstall" {
Write-Log "Entferne alles..." "Yellow"
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName -Force Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
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 "🧹 Server ist wieder Newt-frei." "Green"
} }
} }