install_newt-msp-site-win_v2.ps1 aktualisiert
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client).
|
Windows-Installer für den Newt-Client (MAIEREDV Managed Site Client).
|
||||||
Features: Winget.pro integration, NSSM-Service, BITS-Turbo, 10MB Log-Rotation.
|
Features: Winget.pro, NSSM-Service, Turbo-BITS, Log-Rotation & Safe-Update.
|
||||||
#>
|
#>
|
||||||
param([string]$mode = "install")
|
param([string]$mode = "install")
|
||||||
|
|
||||||
@@ -59,14 +59,38 @@ function Download-Newt {
|
|||||||
|
|
||||||
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 }
|
||||||
|
|
||||||
Write-Log "Download von $VersionOnly..." "Cyan"
|
Write-Log "Prüfe/Downloade Version $VersionOnly..." "Cyan"
|
||||||
|
|
||||||
|
# Download nur wenn Datei noch nicht existiert
|
||||||
|
if (!(Test-Path $Target)) {
|
||||||
|
try {
|
||||||
|
Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop
|
||||||
|
Write-Log "Download erfolgreich." "Green"
|
||||||
|
} catch {
|
||||||
|
Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow"
|
||||||
|
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Datei-Update (Safe-Swap)
|
||||||
|
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
|
$WasRunning = $Service -and $Service.Status -eq 'Running'
|
||||||
|
|
||||||
|
if ($WasRunning) {
|
||||||
|
Write-Log "Stoppe Dienst für Datei-Update..." "Yellow"
|
||||||
|
Stop-Service $ServiceName -Force
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop
|
|
||||||
Copy-Item -Path $Target -Destination $Symlink -Force
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
||||||
|
Write-Log "newt_latest.exe wurde auf Stand $VersionOnly aktualisiert." "Green"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow"
|
Write-Log "FEHLER: Konnte Symlink nicht überschreiben. Datei evtl. blockiert." "Red"
|
||||||
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing
|
}
|
||||||
Copy-Item -Path $Target -Destination $Symlink -Force
|
|
||||||
|
if ($WasRunning) {
|
||||||
|
Start-Service $ServiceName
|
||||||
|
Write-Log "Dienst wieder gestartet." "Green"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,11 +101,11 @@ function Setup-Service {
|
|||||||
$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 }
|
if ([string]::IsNullOrWhiteSpace($PangolinID)) { Write-Log "FEHLER: ID darf nicht leer sein!" "Red"; exit 1 }
|
||||||
|
|
||||||
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
||||||
|
|
||||||
Write-Log "Erstelle Dienst mit NSSM (inkl. Log-Rotation)..." "Cyan"
|
Write-Log "Erstelle Dienst mit NSSM..." "Cyan"
|
||||||
& nssm install $ServiceName "$Symlink" $ArgList
|
& nssm install $ServiceName "$Symlink" $ArgList
|
||||||
& nssm set $ServiceName Description "MAIEREDV Managed Site Client"
|
& nssm set $ServiceName Description "MAIEREDV Managed Site Client"
|
||||||
& nssm set $ServiceName AppExit Default Restart
|
& nssm set $ServiceName AppExit Default Restart
|
||||||
@@ -97,20 +121,16 @@ function Setup-Service {
|
|||||||
Start-Service $ServiceName
|
Start-Service $ServiceName
|
||||||
Write-Log "Dienst erfolgreich gestartet." "Green"
|
Write-Log "Dienst erfolgreich gestartet." "Green"
|
||||||
} else {
|
} else {
|
||||||
Write-Log "Dienst vorhanden. Starte neu..." "Yellow"
|
Write-Log "Dienst vorhanden. Führe Update-Check aus..." "Yellow"
|
||||||
Restart-Service $ServiceName
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Setup-UpdaterTask {
|
function Setup-UpdaterTask {
|
||||||
# HIER IST DIE ANPASSUNG: Nutzt nun den ScriptBlock-Syntax für den täglichen Task
|
|
||||||
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; & ([scriptblock]::Create((New-Object System.Net.WebClient).DownloadString('$GiteaUrl'))) -mode update`""
|
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; & ([scriptblock]::Create((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 $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
|
||||||
Write-Log "Täglicher Update-Task (03:00 Uhr) registriert." "Green"
|
Write-Log "Update-Task registriert." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Main ---
|
# --- Main ---
|
||||||
@@ -122,17 +142,32 @@ switch ($mode) {
|
|||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
Setup-Service
|
Setup-Service
|
||||||
Setup-UpdaterTask
|
Setup-UpdaterTask
|
||||||
Write-Log "Installation fertig!" "Green"
|
Write-Log "🚀 Installation 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") {
|
$TargetVersionPath = "${InstallDir}\newt_${vOnly}.exe"
|
||||||
Write-Log "System ist aktuell ($vOnly)." "Cyan"
|
|
||||||
|
$NeedsUpdate = $false
|
||||||
|
if (!(Test-Path $TargetVersionPath)) {
|
||||||
|
$NeedsUpdate = $true
|
||||||
|
} elseif (Test-Path $Symlink) {
|
||||||
|
# Hash-Vergleich um sicherzugehen, dass latest.exe wirklich die neue Version ist
|
||||||
|
$HashLatest = (Get-FileHash $Symlink).Hash
|
||||||
|
$HashTarget = (Get-FileHash $TargetVersionPath).Hash
|
||||||
|
if ($HashLatest -ne $HashTarget) { $NeedsUpdate = $true }
|
||||||
} else {
|
} else {
|
||||||
|
$NeedsUpdate = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($NeedsUpdate) {
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
Restart-Service $ServiceName
|
Write-Log "🚀 Update auf $v durchgeführt." "Green"
|
||||||
Write-Log "Update auf $v abgeschlossen." "Green"
|
} else {
|
||||||
|
Write-Log "System ist bereits aktuell ($vOnly)." "Cyan"
|
||||||
|
# Falls der Dienst aus irgendeinem Grund steht, starten wir ihn hier
|
||||||
|
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"uninstall" {
|
"uninstall" {
|
||||||
|
|||||||
Reference in New Issue
Block a user