install_newt-msp-site-win_v2.sh aktualisiert

This commit is contained in:
2026-02-18 11:00:24 +01:00
parent e82312224c
commit 72f5a3fcf9

View File

@@ -1,7 +1,7 @@
<# <#
.SYNOPSIS .SYNOPSIS
Windows-Installer für den Newt-Client. Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client).
Finaler Fix für sc.exe via CMD-Wrapper. Nutzt NSSM für ein sauberes Dienst-Management.
#> #>
param([string]$mode = "install") param([string]$mode = "install")
@@ -9,23 +9,41 @@ param([string]$mode = "install")
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# 2. Konfiguration
$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" $NssmPath = "$InstallDir\nssm.exe"
$UpdaterTaskName = "MAIEREDV-Newt-Updater"
$GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1" $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 Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color }
# 3. NSSM bereitstellen (falls nicht vorhanden)
function Get-NSSM {
if (Test-Path $NssmPath) { return }
Write-Log "NSSM wird heruntergeladen..." "Cyan"
$nssmZip = "$env:TEMP\nssm.zip"
$nssmDownloadUrl = "https://nssm.cc/release/nssm-2.24.zip"
Invoke-WebRequest -Uri $nssmDownloadUrl -OutFile $nssmZip -UseBasicParsing
Expand-Archive -Path $nssmZip -DestinationPath "$env:TEMP\nssm_temp" -Force
$archDir = if ([Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" }
Copy-Item "$env:TEMP\nssm_temp\nssm-2.24\$archDir\nssm.exe" -Destination $NssmPath -Force
Remove-Item $nssmZip -ErrorAction SilentlyContinue
Remove-Item "$env:TEMP\nssm_temp" -Recurse -ErrorAction SilentlyContinue
}
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 return $json.tag_name
} catch { } catch {
Write-Log "FEHLER: Konnte Version nicht abrufen." "Red" Write-Log "FEHLER: Konnte Version nicht abrufen." "Red"; exit 1
exit 1
} }
} }
@@ -37,15 +55,15 @@ function Download-Newt {
$Target = "$InstallDir\newt_$VersionOnly.exe" $Target = "$InstallDir\newt_$VersionOnly.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 }
Get-NSSM
Write-Log "Download von: $Url" "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 "Installation erfolgreich: $Target" "Green" Write-Log "Datei geladen: $VersionOnly" "Green"
} catch { } catch {
Write-Log "FEHLER beim Download: $_" "Red" Write-Log "FEHLER beim Download: $_" "Red"; exit 1
exit 1
} }
} }
@@ -58,38 +76,27 @@ function Setup-Service {
if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID leer!" "Red"; exit 1 } 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" $ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command \`"$Symlink $ArgList\`""
Write-Log "Erstelle Windows Dienst via CMD-Wrapper..." "Cyan" Write-Log "Erstelle Dienst mit NSSM..." "Cyan"
& $NssmPath install $ServiceName "$Symlink" $ArgList
& $NssmPath set $ServiceName Description "MAIEREDV Managed Site Client"
& $NssmPath set $ServiceName AppExit Default Restart
& $NssmPath set $ServiceName AppRestartDelay 5000
# 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 Start-Service $ServiceName
Write-Log "Dienst erfolgreich gestartet." "Green" Write-Log "Dienst wurde erfolgreich erstellt und gestartet." "Green"
} else {
Write-Log "FEHLER: Dienst konnte nicht erstellt werden." "Red"
}
} else { } else {
Write-Log "Dienst existiert bereits. Starte neu..." "Yellow" Write-Log "Dienst existiert bereits. Starte neu..." "Yellow"
Restart-Service $ServiceName Restart-Service $ServiceName
} }
} }
function Setup-UpdateTask { function Setup-UpdaterTask {
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl')) -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 $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
Write-Log "Update-Task erstellt." "Green" Write-Log "Update-Task erstellt." "Green"
} }
@@ -99,8 +106,8 @@ switch ($mode) {
$v = Get-LatestVersion $v = Get-LatestVersion
Download-Newt $v Download-Newt $v
Setup-Service Setup-Service
Setup-UpdateTask Setup-UpdaterTask
Write-Log "Installation abgeschlossen!" "Green" Write-Log "Installation mit NSSM abgeschlossen!" "Green"
} }
"update" { "update" {
$v = Get-LatestVersion $v = Get-LatestVersion
@@ -110,15 +117,16 @@ switch ($mode) {
} else { } else {
Download-Newt $v Download-Newt $v
Restart-Service $ServiceName Restart-Service $ServiceName
Write-Log "Update auf $v durchgefuehrt." "Green" Write-Log "Update via NSSM-Dienst abgeschlossen." "Green"
} }
} }
"uninstall" { "uninstall" {
Write-Log "Deinstallation..." "Yellow"
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName -Force Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
cmd.exe /c "sc delete $ServiceName" & $NssmPath remove $ServiceName confirm
} }
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Log "Deinstalliert." "Green" Write-Log "Alles entfernt." "Green"
} }
} }