123 lines
4.8 KiB
Bash
123 lines
4.8 KiB
Bash
<#
|
|
.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")
|
|
|
|
# 1. Stabilität & Encoding sicherstellen
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
# 2. Konfiguration
|
|
$Repo = "fosrl/newt"
|
|
$InstallDir = "C:\Program Files\me-msp-newt"
|
|
$ServiceName = "MAIEREDV-Managed-Site-Client"
|
|
$Symlink = "$InstallDir\newt_latest.exe"
|
|
$TaskName = "Newt-Updater"
|
|
$GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1"
|
|
|
|
# Helfer-Funktionen
|
|
function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color }
|
|
|
|
function Get-LatestVersion {
|
|
try {
|
|
$url = "https://api.github.com/repos/$Repo/releases/latest"
|
|
$json = Invoke-RestMethod -Uri $url -UseBasicParsing
|
|
return $json.tag_name # Gibt z.B. "v1.9.0" zurück
|
|
} catch {
|
|
Write-Log "FEHLER: Konnte Version nicht abrufen." "Red"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
function Download-Newt {
|
|
param($FullVersion)
|
|
|
|
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
|
|
|
|
# URL-Fix: Das 'v' für den Download-Pfad entfernen (v1.9.0 -> 1.9.0)
|
|
$VersionOnly = $FullVersion.TrimStart('v')
|
|
$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-Log "Download von: $Url" "Cyan"
|
|
try {
|
|
# BITS umgeht die IE-Sicherheitskonfiguration auf Windows Servern
|
|
Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop
|
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
|
Write-Log "Installation erfolgreich: $Target" "Green"
|
|
} catch {
|
|
Write-Log "FEHLER beim Download: $_" "Red"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
function Setup-Service {
|
|
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
|
Write-Log "--- Dienst-Konfiguration ---" "Yellow"
|
|
$PangolinID = Read-Host "Bitte Pangolin ID 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"
|
|
# PowerShell Wrapper verhindert Timeouts beim Dienst-Start
|
|
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`""
|
|
|
|
Write-Log "Erstelle Windows Dienst..." "Cyan"
|
|
& sc.exe create $ServiceName binPath= $BinaryPath start= auto DisplayName= "MAIEREDV Managed Site Client"
|
|
Start-Service $ServiceName
|
|
} else {
|
|
Write-Log "Dienst existiert bereits. Starte neu..." "Yellow"
|
|
Restart-Service $ServiceName
|
|
}
|
|
}
|
|
|
|
function Setup-UpdateTask {
|
|
$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
|
|
|
|
# Task als SYSTEM-Nutzer registrieren
|
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "SYSTEM" -Force | Out-Null
|
|
Write-Log "Update-Task fuer 03:00 Uhr erstellt." "Green"
|
|
}
|
|
|
|
# --- Ablauf-Steuerung ---
|
|
switch ($mode) {
|
|
"install" {
|
|
$v = Get-LatestVersion
|
|
Download-Newt $v
|
|
Setup-Service
|
|
Setup-UpdateTask
|
|
Write-Log "Installation abgeschlossen!" "Green"
|
|
}
|
|
"update" {
|
|
$v = Get-LatestVersion
|
|
$vOnly = $v.TrimStart('v')
|
|
if (Test-Path "$InstallDir\newt_$vOnly.exe") {
|
|
Write-Log "System ist bereits aktuell ($vOnly)." "Cyan"
|
|
} else {
|
|
Download-Newt $v
|
|
Restart-Service $ServiceName
|
|
Write-Log "Update auf $v durchgefuehrt." "Green"
|
|
}
|
|
}
|
|
"uninstall" {
|
|
Write-Log "Deinstallation wird gestartet..." "Yellow"
|
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
|
Stop-Service $ServiceName -Force
|
|
& sc.exe delete $ServiceName
|
|
}
|
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
|
|
Write-Log "Dienst und Updater entfernt." "Green"
|
|
}
|
|
default {
|
|
Write-Log "Unbekannter Modus: $mode. Nutze install, update oder uninstall." "Red"
|
|
}
|
|
} |