install_newt-msp-site-win_v2.sh aktualisiert

This commit is contained in:
2026-02-18 10:46:14 +01:00
parent 089ae4c26f
commit 28d77b41d6

View File

@@ -1,143 +1,108 @@
<# <#
.SYNOPSIS .SYNOPSIS
Windows-Pendant zum Newt-Installer (Bash). Windows-Pendant zum Newt-Installer.
Unterstützt: --install, --update, --reinstall, --uninstall
#> #>
param([string]$mode = "install")
# 1. TLS 1.2 erzwingen (Wichtig für GitHub Downloads auf Windows Server) # TLS 1.2 erzwingen
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# 2. Variablen definieren
$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"
# Helfer für bunte Ausgaben
function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green } function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow } function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow }
function Write-ErrorMsg($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 } function Write-ErrorMsg($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 }
# 3. Neueste Version von GitHub holen (API)
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.TrimStart('v') # Wir speichern die Version EXAKT so wie GitHub sie ausgibt (meist mit v)
return $json.tag_name
} catch { } catch {
Write-ErrorMsg "Konnte Version nicht von GitHub abrufen." Write-ErrorMsg "Konnte Version nicht von GitHub abrufen."
} }
} }
# 4. Download via BITS (Server-sicher)
function Download-Newt { function Download-Newt {
param($Version) param($Version)
$Arch = if ([Environment]::Is64BitOperatingSystem) { "newt_windows_amd64.exe" } else { "newt_windows_386.exe" } # Architektur-Mapping
$Url = "https://github.com/$Repo/releases/download/v$Version/$Arch" $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
$Target = "$InstallDir\newt_$Version.exe"
# WICHTIG: Prüfen ob das 'v' schon im Tag ist
$Tag = if ($Version.StartsWith("v")) { $Version } else { "v$Version" }
# Die Datei auf GitHub heißt oft einfach 'newt_windows_amd64.exe' (ohne Version im Dateinamen)
$Url = "https://github.com/$Repo/releases/download/$Tag/newt_$ArchSuffix"
$Target = "$InstallDir\newt_$($Tag).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 "⬇️ Downloading $Url via BITS..." Write-Info "⬇️ Versuche Download von: $Url"
try { try {
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 "✅ Installiert: newt $Version unter $Target" Write-Info "✅ Installiert: $Tag"
} catch { } catch {
Write-ErrorMsg "Download fehlgeschlagen: $_" Write-ErrorMsg "Download fehlgeschlagen (404?). Prüfe ob die Datei unter $Url existiert. Fehler: $_"
} }
} }
# 5. Dienst erstellen (Nativ mit PowerShell-Wrapper)
function Setup-Service { function Setup-Service {
$PangolinID = Read-Host "🆔 Bitte Pangolin ID eingeben" if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
$PangolinSecret = Read-Host "🔑 Bitte Secret eingeben" $PangolinID = Read-Host "🆔 Pangolin ID"
$PangolinEndpoint = Read-Host "🌐 Bitte Endpoint eingeben (z.B. https://pangolin.domain.com)" $PangolinSecret = Read-Host "🔑 Secret"
$PangolinEndpoint = Read-Host "🌐 Endpoint"
$ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint" $ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`""
# Da newt.exe kein nativer Windows-Service ist, nutzen wir einen PowerShell-Wrapper & sc.exe create $ServiceName binPath= $BinaryPath start= auto DisplayName= "MAIEREDV Managed Site Client"
# Dieser verhindert den "Dienst antwortete nicht rechtzeitig" Fehler. Start-Service $ServiceName
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`""
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Write-Warn "Dienst existiert bereits. Aktualisiere Konfiguration..."
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
# Set-Service kann den BinaryPath nicht direkt ändern, daher sc.exe (nativ)
& sc.exe config $ServiceName binPath= $BinaryPath
} else { } else {
New-Service -Name $ServiceName ` Restart-Service $ServiceName
-BinaryPathName $BinaryPath `
-DisplayName "MAIEREDV Managed Site Client" `
-StartupType Automatic
} }
Start-Service $ServiceName
Write-Info "🛠️ Dienst $ServiceName gestartet."
} }
# 6. Täglicher Update-Task (Pendant zum systemd timer)
function Setup-UpdateTask { function Setup-UpdateTask {
if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) { # Hier deine Gitea URL eintragen
Write-Info "⏳ Update-Task existiert bereits." $GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1"
return
}
$ScriptPath = $MyInvocation.MyCommand.Path
# Falls als One-Liner ausgeführt, hier festen Pfad oder URL eintragen:
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('DEINE_GITEA_URL_ZUM_SCRIPT')) -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 $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 Uhr) erstellt." Write-Info "✅ Update-Task registriert."
} }
# 7. Modi # --- Ausführung ---
function Mode-Install {
$v = Get-LatestVersion
if (Test-Path "$InstallDir\newt_$v.exe") {
Write-Warn "⚠️ Version $v ist bereits installiert. Nutze --reinstall."
Setup-UpdateTask
return
}
Download-Newt $v
Setup-Service
Setup-UpdateTask
Write-Info "🚀 Installation abgeschlossen!"
}
function Mode-Update {
$v = Get-LatestVersion
if (Test-Path "$InstallDir\newt_$v.exe") {
Write-Info "✅ Version $v ist aktuell. Nichts zu tun."
return
}
Download-Newt $v
Restart-Service $ServiceName
Write-Info "🚀 Update auf $v abgeschlossen!"
}
function Mode-Uninstall {
Write-Warn "⚠️ Deinstalliere Newt..."
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName -Force
$service = Get-CimInstance Win32_Service -Filter "Name='$ServiceName'"
$service | Remove-CimInstance
}
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Info "🧹 Dienst und Task entfernt. Ordner $InstallDir bitte manuell löschen falls gewünscht."
}
# 8. Main Logic
param([string]$mode = "install")
switch ($mode) { switch ($mode) {
"install" { Mode-Install } "install" {
"update" { Mode-Update } $v = Get-LatestVersion
"reinstall" { Download-Newt (Get-LatestVersion); Setup-Service; Setup-UpdateTask } Download-Newt $v
"uninstall" { Mode-Uninstall } Setup-Service
default { Write-ErrorMsg "Unbekannter Parameter: $mode (Nutze install, update, reinstall, uninstall)" } Setup-UpdateTask
}
"update" {
$v = Get-LatestVersion
$Tag = if ($v.StartsWith("v")) { $v } else { "v$v" }
if (Test-Path "$InstallDir\newt_$Tag.exe") {
Write-Info "Schon aktuell."
} else {
Download-Newt $v
Restart-Service $ServiceName
}
}
"uninstall" {
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName
& sc.exe delete $ServiceName
}
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
}
} }