Files
update-install-newt/install_newt-msp-site-win_v2.sh

124 lines
4.8 KiB
Bash

<#
.SYNOPSIS
Windows-Installer für den Newt-Client.
Finaler Fix für sc.exe via CMD-Wrapper.
#>
param([string]$mode = "install")
# 1. Stabilität & Encoding
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$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"
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
} 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" }
$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 {
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"
if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID leer!" "Red"; exit 1 }
# Wir bauen den Befehl für CMD.exe zusammen.
# Das ist der sicherste Weg, damit die Leerzeichen nach dem '=' erhalten bleiben.
$ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command \`"$Symlink $ArgList\`""
Write-Log "Erstelle Windows Dienst via CMD-Wrapper..." "Cyan"
# Der Trick: Wir rufen cmd /c auf und übergeben den sc-Befehl als String.
# Beachte die Leerstellen nach den '=' Zeichen!
$cmdCommand = "sc create $ServiceName binPath= ""$BinaryPath"" DisplayName= ""MAIEREDV Managed Site Client"" start= auto"
cmd.exe /c $cmdCommand
Start-Sleep -Seconds 2
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Start-Service $ServiceName
Write-Log "Dienst erfolgreich gestartet." "Green"
} else {
Write-Log "FEHLER: Dienst konnte nicht erstellt werden." "Red"
}
} 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
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "SYSTEM" -Force | Out-Null
Write-Log "Update-Task erstellt." "Green"
}
# --- Main Logic ---
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 aktuell ($vOnly)." "Cyan"
} else {
Download-Newt $v
Restart-Service $ServiceName
Write-Log "Update auf $v durchgefuehrt." "Green"
}
}
"uninstall" {
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName -Force
cmd.exe /c "sc delete $ServiceName"
}
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Log "Deinstalliert." "Green"
}
}