install_newt-msp-site-win_v2.sh aktualisiert

This commit is contained in:
2026-02-18 10:50:11 +01:00
parent a774d23f6b
commit c3c9c6f43b

View File

@@ -1,117 +1,123 @@
<#
.SYNOPSIS
Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client).
Kompatibel mit Windows Server (BITS-Download & TLS 1.2).
#>
param([string]$mode = "install") param([string]$mode = "install")
# 1. TLS 1.2 erzwingen (Pflicht für GitHub auf Windows Server) # 1. Stabilität & Encoding sicherstellen
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[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"
$TaskName = "Newt-Updater" $TaskName = "Newt-Updater"
$GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1"
function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green } # Helfer-Funktionen
function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow } function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color }
function Write-ErrorMsg($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 }
# Neueste Version von GitHub API holen
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
# Wir geben den reinen Tag zurück (z.B. "v1.9.0") return $json.tag_name # Gibt z.B. "v1.9.0" zurück
return $json.tag_name
} catch { } catch {
Write-ErrorMsg "Konnte Version nicht von GitHub abrufen." Write-Log "FEHLER: Konnte Version nicht abrufen." "Red"
exit 1
} }
} }
function Download-Newt { function Download-Newt {
param($FullVersion) param($FullVersion)
# Architektur bestimmen
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
# URL FIX: Das 'v' für die URL entfernen, falls vorhanden (macht aus v1.9.0 -> 1.9.0) # URL-Fix: Das 'v' für den Download-Pfad entfernen (v1.9.0 -> 1.9.0)
$VersionOnly = $FullVersion.TrimStart('v') $VersionOnly = $FullVersion.TrimStart('v')
# Exakter URL-Aufbau basierend auf deinem funktionierenden curl-Link
$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 -ItemType Directory -Path $InstallDir -Force | Out-Null } if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
Write-Info "⬇️ Versuche Download von: $Url" Write-Log "Download von: $Url" "Cyan"
try { try {
# BITS ist sauberer auf Servern # BITS umgeht die IE-Sicherheitskonfiguration auf Windows Servern
Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop
Copy-Item -Path $Target -Destination $Symlink -Force Copy-Item -Path $Target -Destination $Symlink -Force
Write-Info "✅ Datei erfolgreich nach $Target geladen." Write-Log "Installation erfolgreich: $Target" "Green"
} catch { } catch {
Write-ErrorMsg "Download fehlgeschlagen. Bitte prüfe die URL: $Url. Fehler: $_" Write-Log "FEHLER beim Download: $_" "Red"
exit 1
} }
} }
function Setup-Service { function Setup-Service {
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
$PangolinID = Read-Host "🆔 Bitte Pangolin ID eingeben" Write-Log "--- Dienst-Konfiguration ---" "Yellow"
$PangolinSecret = Read-Host "🔑 Bitte Secret eingeben" $PangolinID = Read-Host "Bitte Pangolin ID eingeben"
$PangolinEndpoint = Read-Host "🌐 Bitte Endpoint eingeben" $PangolinSecret = Read-Host "Bitte Secret eingeben"
$PangolinEndpoint = Read-Host "Bitte Endpoint eingeben (z.B. https://pangolin.domain.com)"
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"
# Wrapper um PowerShell, damit der Dienst nicht wegen Timeouts crasht # PowerShell Wrapper verhindert Timeouts beim Dienst-Start
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`"" $BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`""
Write-Info "🛠️ Erstelle Dienst..." Write-Log "Erstelle Windows Dienst..." "Cyan"
& sc.exe create $ServiceName binPath= $BinaryPath start= auto DisplayName= "MAIEREDV Managed Site Client" & sc.exe create $ServiceName binPath= $BinaryPath start= auto DisplayName= "MAIEREDV Managed Site Client"
Start-Service $ServiceName Start-Service $ServiceName
} else { } else {
Write-Info "🔄 Dienst existiert bereits, starte neu..." Write-Log "Dienst existiert bereits. Starte neu..." "Yellow"
Restart-Service $ServiceName Restart-Service $ServiceName
} }
} }
function Setup-UpdateTask { function Setup-UpdateTask {
# DEINE GITEA URL HIER EINTRAGEN
$GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1"
$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
# Task als SYSTEM-Nutzer registrieren
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "SYSTEM" -Force | Out-Null Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "SYSTEM" -Force | Out-Null
Write-Info "✅ Täglicher Update-Task (03:00) wurde erstellt." Write-Log "Update-Task fuer 03:00 Uhr erstellt." "Green"
} }
# --- Main Logic --- # --- Ablauf-Steuerung ---
switch ($mode) { switch ($mode) {
"install" { "install" {
$v = Get-LatestVersion $v = Get-LatestVersion
Download-Newt $v Download-Newt $v
Setup-Service Setup-Service
Setup-UpdateTask Setup-UpdateTask
Write-Info "🚀 Fertig!" Write-Log "Installation abgeschlossen!" "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-Info "Bereits auf dem neuesten Stand ($vOnly)." Write-Log "System ist bereits aktuell ($vOnly)." "Cyan"
} else { } else {
Download-Newt $v Download-Newt $v
Restart-Service $ServiceName Restart-Service $ServiceName
Write-Info "🚀 Update auf $v abgeschlossen." Write-Log "Update auf $v durchgefuehrt." "Green"
} }
} }
"uninstall" { "uninstall" {
Write-Log "Deinstallation wird gestartet..." "Yellow"
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName Stop-Service $ServiceName -Force
& sc.exe delete $ServiceName & sc.exe delete $ServiceName
} }
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Warn "🧹 Dienst und Task wurden entfernt." Write-Log "Dienst und Updater entfernt." "Green"
} }
default { default {
Write-ErrorMsg "Modus unbekannt. Nutze: install, update, uninstall" Write-Log "Unbekannter Modus: $mode. Nutze install, update oder uninstall." "Red"
} }
} }