install_newt-msp-site-win_v2.sh aktualisiert
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Windows-Installer für den Newt-Client mit NSSM.
|
Windows-Installer für den Newt-Client.
|
||||||
Version 5 - Mit dynamischer Dateisuche für NSSM.
|
Nutzt Winget für das Paketmanagement von NSSM.
|
||||||
#>
|
#>
|
||||||
param([string]$mode = "install")
|
param([string]$mode = "install")
|
||||||
|
|
||||||
@@ -12,44 +12,28 @@ $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"
|
||||||
$NssmPath = "$InstallDir\nssm.exe"
|
|
||||||
$UpdaterTaskName = "MAIEREDV-Newt-Updater"
|
$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 }
|
||||||
|
|
||||||
function Get-NSSM {
|
# 1. Winget & NSSM sicherstellen
|
||||||
if (Test-Path $NssmPath) { return }
|
function Prepare-Environment {
|
||||||
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
||||||
|
Write-Log "Winget fehlt. Installiere Winget via winget.pro..." "Cyan"
|
||||||
Write-Log "NSSM wird von Chocolatey geladen..." "Cyan"
|
try {
|
||||||
$tempZip = "$env:TEMP\nssm_pkg.zip"
|
irm winget.pro | iex
|
||||||
$extractPath = "$env:TEMP\nssm_temp"
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
$nssmDownloadUrl = "https://community.chocolatey.org/api/v2/package/nssm/2.24.101"
|
} catch {
|
||||||
|
Write-Log "FEHLER: Winget-Installation fehlgeschlagen." "Red"; exit 1
|
||||||
try {
|
|
||||||
Invoke-WebRequest -Uri $nssmDownloadUrl -OutFile $tempZip -UseBasicParsing -ErrorAction Stop
|
|
||||||
|
|
||||||
if (Test-Path $extractPath) { Remove-Item $extractPath -Recurse -Force }
|
|
||||||
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
||||||
[System.IO.Compression.ZipFile]::ExtractToDirectory($tempZip, $extractPath)
|
|
||||||
|
|
||||||
# Dynamische Suche: Wir suchen die passende win64 oder win32 exe, egal in welchem Unterordner
|
|
||||||
$archDirName = if ([Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" }
|
|
||||||
$foundNssm = Get-ChildItem -Path $extractPath -Filter "nssm.exe" -Recurse | Where-Object { $_.FullName -like "*$archDirName*" } | Select-Object -First 1
|
|
||||||
|
|
||||||
if ($foundNssm) {
|
|
||||||
Copy-Item $foundNssm.FullName -Destination $NssmPath -Force
|
|
||||||
Write-Log "NSSM bereitgestellt aus: $($foundNssm.FullName)" "Green"
|
|
||||||
} else {
|
|
||||||
throw "nssm.exe für $archDirName im Paket nicht gefunden."
|
|
||||||
}
|
}
|
||||||
} catch {
|
}
|
||||||
Write-Log "FEHLER bei NSSM-Bereitstellung: $_" "Red"
|
|
||||||
exit 1
|
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
||||||
} finally {
|
Write-Log "NSSM wird via Winget installiert..." "Cyan"
|
||||||
if (Test-Path $tempZip) { Remove-Item $tempZip -Force }
|
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
||||||
if (Test-Path $extractPath) { Remove-Item $extractPath -Recurse -Force }
|
# Pfad aktualisieren, falls Winget nssm gerade erst hinzugefügt hat
|
||||||
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +54,8 @@ 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"
|
||||||
|
|
||||||
Get-NSSM
|
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
||||||
|
|
||||||
Write-Log "Download Newt $VersionOnly..." "Cyan"
|
Write-Log "Download Newt $VersionOnly..." "Cyan"
|
||||||
try {
|
try {
|
||||||
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing -ErrorAction Stop
|
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing -ErrorAction Stop
|
||||||
@@ -90,15 +75,16 @@ function Setup-Service {
|
|||||||
$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"
|
||||||
& $NssmPath install $ServiceName "$Symlink" $ArgList
|
# Da NSSM nun im PATH liegt, rufen wir es direkt auf
|
||||||
& $NssmPath set $ServiceName Description "MAIEREDV Managed Site Client"
|
nssm install $ServiceName "$Symlink" $ArgList
|
||||||
& $NssmPath set $ServiceName AppExit Default Restart
|
nssm set $ServiceName Description "MAIEREDV Managed Site Client"
|
||||||
& $NssmPath set $ServiceName AppRestartDelay 5000
|
nssm set $ServiceName AppExit Default Restart
|
||||||
& $NssmPath set $ServiceName AppStdout "$InstallDir\newt_service.log"
|
nssm set $ServiceName AppRestartDelay 5000
|
||||||
& $NssmPath set $ServiceName AppStderr "$InstallDir\newt_service.log"
|
nssm set $ServiceName AppStdout "$InstallDir\newt_service.log"
|
||||||
|
nssm set $ServiceName AppStderr "$InstallDir\newt_service.log"
|
||||||
|
|
||||||
Start-Service $ServiceName
|
Start-Service $ServiceName
|
||||||
Write-Log "Dienst gestartet." "Green"
|
Write-Log "Dienst erfolgreich gestartet." "Green"
|
||||||
} else {
|
} else {
|
||||||
Write-Log "Dienst wird aktualisiert..." "Yellow"
|
Write-Log "Dienst wird aktualisiert..." "Yellow"
|
||||||
Restart-Service $ServiceName
|
Restart-Service $ServiceName
|
||||||
@@ -110,34 +96,36 @@ function Setup-UpdaterTask {
|
|||||||
$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 $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
|
||||||
Write-Log "Update-Task erstellt." "Green"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- Main ---
|
||||||
|
Prepare-Environment
|
||||||
|
|
||||||
switch ($mode) {
|
switch ($mode) {
|
||||||
"install" {
|
"install" {
|
||||||
$v = Get-LatestVersion
|
$v = Get-LatestVersion
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
Setup-Service
|
Setup-Service
|
||||||
Setup-UpdaterTask
|
Setup-UpdaterTask
|
||||||
Write-Log "Installation erfolgreich!" "Green"
|
Write-Log "Installation erfolgreich abgeschlossen!" "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 "Aktuell." "Cyan"
|
Write-Log "System ist aktuell." "Cyan"
|
||||||
} else {
|
} else {
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
Restart-Service $ServiceName
|
Restart-Service $ServiceName
|
||||||
Write-Log "Update fertig." "Green"
|
Write-Log "Update auf $v durchgefuehrt." "Green"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"uninstall" {
|
"uninstall" {
|
||||||
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
||||||
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
|
Stop-Service $ServiceName -Force
|
||||||
& $NssmPath remove $ServiceName confirm
|
nssm remove $ServiceName confirm
|
||||||
}
|
}
|
||||||
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
||||||
Write-Log "Entfernt." "Green"
|
Write-Log "Deinstallation fertig." "Green"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user