117 lines
4.6 KiB
Bash
117 lines
4.6 KiB
Bash
param([string]$mode = "install")
|
|
|
|
# 1. TLS 1.2 erzwingen (Pflicht für GitHub auf Windows Server)
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
|
|
$Repo = "fosrl/newt"
|
|
$InstallDir = "C:\Program Files\me-msp-newt"
|
|
$ServiceName = "MAIEREDV-Managed-Site-Client"
|
|
$Symlink = "$InstallDir\newt_latest.exe"
|
|
$TaskName = "Newt-Updater"
|
|
|
|
function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green }
|
|
function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow }
|
|
function Write-ErrorMsg($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 }
|
|
|
|
# Neueste Version von GitHub API holen
|
|
function Get-LatestVersion {
|
|
try {
|
|
$url = "https://api.github.com/repos/$Repo/releases/latest"
|
|
$json = Invoke-RestMethod -Uri $url -UseBasicParsing
|
|
# Wir geben den reinen Tag zurück (z.B. "v1.9.0")
|
|
return $json.tag_name
|
|
} catch {
|
|
Write-ErrorMsg "Konnte Version nicht von GitHub abrufen."
|
|
}
|
|
}
|
|
|
|
function Download-Newt {
|
|
param($FullVersion)
|
|
|
|
# Architektur bestimmen
|
|
$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)
|
|
$VersionOnly = $FullVersion.TrimStart('v')
|
|
|
|
# Exakter URL-Aufbau basierend auf deinem funktionierenden curl-Link
|
|
$Url = "https://github.com/$Repo/releases/download/$VersionOnly/newt_$ArchSuffix"
|
|
$Target = "$InstallDir\newt_$VersionOnly.exe"
|
|
|
|
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
|
|
|
Write-Info "⬇️ Versuche Download von: $Url"
|
|
try {
|
|
# BITS ist sauberer auf Servern
|
|
Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop
|
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
|
Write-Info "✅ Datei erfolgreich nach $Target geladen."
|
|
} catch {
|
|
Write-ErrorMsg "Download fehlgeschlagen. Bitte prüfe die URL: $Url. Fehler: $_"
|
|
}
|
|
}
|
|
|
|
function Setup-Service {
|
|
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
|
$PangolinID = Read-Host "🆔 Bitte Pangolin ID eingeben"
|
|
$PangolinSecret = Read-Host "🔑 Bitte Secret eingeben"
|
|
$PangolinEndpoint = Read-Host "🌐 Bitte Endpoint eingeben"
|
|
|
|
$ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
|
|
# Wrapper um PowerShell, damit der Dienst nicht wegen Timeouts crasht
|
|
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`""
|
|
|
|
Write-Info "🛠️ Erstelle Dienst..."
|
|
& sc.exe create $ServiceName binPath= $BinaryPath start= auto DisplayName= "MAIEREDV Managed Site Client"
|
|
Start-Service $ServiceName
|
|
} else {
|
|
Write-Info "🔄 Dienst existiert bereits, starte neu..."
|
|
Restart-Service $ServiceName
|
|
}
|
|
}
|
|
|
|
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`""
|
|
|
|
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $ActionCommand
|
|
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
|
|
|
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "SYSTEM" -Force | Out-Null
|
|
Write-Info "✅ Täglicher Update-Task (03:00) wurde erstellt."
|
|
}
|
|
|
|
# --- Main Logic ---
|
|
switch ($mode) {
|
|
"install" {
|
|
$v = Get-LatestVersion
|
|
Download-Newt $v
|
|
Setup-Service
|
|
Setup-UpdateTask
|
|
Write-Info "🚀 Fertig!"
|
|
}
|
|
"update" {
|
|
$v = Get-LatestVersion
|
|
$vOnly = $v.TrimStart('v')
|
|
if (Test-Path "$InstallDir\newt_$vOnly.exe") {
|
|
Write-Info "Bereits auf dem neuesten Stand ($vOnly)."
|
|
} else {
|
|
Download-Newt $v
|
|
Restart-Service $ServiceName
|
|
Write-Info "🚀 Update auf $v abgeschlossen."
|
|
}
|
|
}
|
|
"uninstall" {
|
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
|
Stop-Service $ServiceName
|
|
& sc.exe delete $ServiceName
|
|
}
|
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
|
|
Write-Warn "🧹 Dienst und Task wurden entfernt."
|
|
}
|
|
default {
|
|
Write-ErrorMsg "Modus unbekannt. Nutze: install, update, uninstall"
|
|
}
|
|
} |