install_newt-msp-site-win_v2.sh aktualisiert

This commit is contained in:
2026-02-18 11:03:46 +01:00
parent 72f5a3fcf9
commit 4e832c6382

View File

@@ -1,7 +1,7 @@
<# <#
.SYNOPSIS .SYNOPSIS
Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client). Windows-Installer für den Newt-Client mit NSSM.
Nutzt NSSM für ein sauberes Dienst-Management. Nutzt einen stabilen Download-Mirror für NSSM.
#> #>
param([string]$mode = "install") param([string]$mode = "install")
@@ -20,21 +20,33 @@ $GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/bran
function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color } function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color }
# 3. NSSM bereitstellen (falls nicht vorhanden) # 3. NSSM bereitstellen (Mirror-Download)
function Get-NSSM { function Get-NSSM {
if (Test-Path $NssmPath) { return } if (Test-Path $NssmPath) { return }
Write-Log "NSSM wird heruntergeladen..." "Cyan" if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
Write-Log "NSSM wird über Mirror heruntergeladen..." "Cyan"
$nssmZip = "$env:TEMP\nssm.zip" $nssmZip = "$env:TEMP\nssm.zip"
$nssmDownloadUrl = "https://nssm.cc/release/nssm-2.24.zip"
Invoke-WebRequest -Uri $nssmDownloadUrl -OutFile $nssmZip -UseBasicParsing # Mirror von einem stabilen Repository (GitHub Mirror von NSSM)
Expand-Archive -Path $nssmZip -DestinationPath "$env:TEMP\nssm_temp" -Force $nssmDownloadUrl = "https://github.com/kirill-grouchnikov/nssm/raw/master/release/nssm-2.24.zip"
$archDir = if ([Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" } try {
Copy-Item "$env:TEMP\nssm_temp\nssm-2.24\$archDir\nssm.exe" -Destination $NssmPath -Force # BITS ist auf Servern zuverlässiger als Invoke-WebRequest
Start-BitsTransfer -Source $nssmDownloadUrl -Destination $nssmZip -ErrorAction Stop
Remove-Item $nssmZip -ErrorAction SilentlyContinue Expand-Archive -Path $nssmZip -DestinationPath "$env:TEMP\nssm_temp" -Force
Remove-Item "$env:TEMP\nssm_temp" -Recurse -ErrorAction SilentlyContinue
$archDir = if ([Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" }
Copy-Item "$env:TEMP\nssm_temp\nssm-2.24\$archDir\nssm.exe" -Destination $NssmPath -Force
Write-Log "NSSM erfolgreich bereitgestellt." "Green"
} catch {
Write-Log "FEHLER: NSSM Download fehlgeschlagen: $_" "Red"
exit 1
} finally {
Remove-Item $nssmZip -ErrorAction SilentlyContinue
Remove-Item "$env:TEMP\nssm_temp" -Recurse -ErrorAction SilentlyContinue
}
} }
function Get-LatestVersion { function Get-LatestVersion {
@@ -43,7 +55,7 @@ function Get-LatestVersion {
$json = Invoke-RestMethod -Uri $url -UseBasicParsing $json = Invoke-RestMethod -Uri $url -UseBasicParsing
return $json.tag_name return $json.tag_name
} catch { } catch {
Write-Log "FEHLER: Konnte Version nicht abrufen." "Red"; exit 1 Write-Log "FEHLER: Konnte Newt-Version nicht abrufen." "Red"; exit 1
} }
} }
@@ -54,28 +66,23 @@ function Download-Newt {
$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 }
Get-NSSM Get-NSSM
Write-Log "Download Newt $VersionOnly..." "Cyan"
Write-Log "Download von: $Url" "Cyan"
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-Log "Datei geladen: $VersionOnly" "Green"
} catch { } catch {
Write-Log "FEHLER beim Download: $_" "Red"; exit 1 Write-Log "FEHLER beim Newt-Download: $_" "Red"; exit 1
} }
} }
function Setup-Service { function Setup-Service {
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
Write-Log "--- Dienst-Konfiguration ---" "Yellow" Write-Log "--- Konfiguration ---" "Yellow"
$PangolinID = Read-Host "Bitte Pangolin ID eingeben" $PangolinID = Read-Host "Bitte Pangolin ID eingeben"
$PangolinSecret = Read-Host "Bitte Secret eingeben" $PangolinSecret = Read-Host "Bitte Secret eingeben"
$PangolinEndpoint = Read-Host "Bitte Endpoint eingeben" $PangolinEndpoint = Read-Host "Bitte Endpoint eingeben"
if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID leer!" "Red"; exit 1 }
$ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint" $ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
Write-Log "Erstelle Dienst mit NSSM..." "Cyan" Write-Log "Erstelle Dienst mit NSSM..." "Cyan"
@@ -84,10 +91,14 @@ function Setup-Service {
& $NssmPath set $ServiceName AppExit Default Restart & $NssmPath set $ServiceName AppExit Default Restart
& $NssmPath set $ServiceName AppRestartDelay 5000 & $NssmPath set $ServiceName AppRestartDelay 5000
# Output Logs aktivieren (Sehr nützlich!)
& $NssmPath set $ServiceName AppStdout "$InstallDir\newt_service.log"
& $NssmPath set $ServiceName AppStderr "$InstallDir\newt_service.log"
Start-Service $ServiceName Start-Service $ServiceName
Write-Log "Dienst wurde erfolgreich erstellt und gestartet." "Green" Write-Log "Dienst erfolgreich gestartet." "Green"
} else { } else {
Write-Log "Dienst existiert bereits. Starte neu..." "Yellow" Write-Log "Dienst wird aktualisiert und neu gestartet..." "Yellow"
Restart-Service $ServiceName Restart-Service $ServiceName
} }
} }
@@ -107,17 +118,17 @@ switch ($mode) {
Download-Newt $v Download-Newt $v
Setup-Service Setup-Service
Setup-UpdaterTask Setup-UpdaterTask
Write-Log "Installation mit NSSM abgeschlossen!" "Green" Write-Log "Installation fertig!" "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-Log "System aktuell ($vOnly)." "Cyan" Write-Log "Schon aktuell." "Cyan"
} else { } else {
Download-Newt $v Download-Newt $v
Restart-Service $ServiceName Restart-Service $ServiceName
Write-Log "Update via NSSM-Dienst abgeschlossen." "Green" Write-Log "Update auf $v durchgefuehrt." "Green"
} }
} }
"uninstall" { "uninstall" {
@@ -127,6 +138,6 @@ switch ($mode) {
& $NssmPath remove $ServiceName confirm & $NssmPath remove $ServiceName confirm
} }
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Log "Alles entfernt." "Green" Write-Log "Dienst und Task entfernt." "Green"
} }
} }