install_newt-msp-site-win_v2.ps1 aktualisiert
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
# ==========================================
|
<#
|
||||||
# Newt Installer - MAIEREDV
|
.SYNOPSIS
|
||||||
# ==========================================
|
Windows-Installer für den Newt-Client (MAIEREDV).
|
||||||
|
Features: Winget.pro, NSSM, Turbo-BITS, Log-Rotation, Auto-Cleanup & Hard-Kill-Service.
|
||||||
|
#>
|
||||||
param([string]$mode = "install")
|
param([string]$mode = "install")
|
||||||
|
|
||||||
|
# 1. Stabilität & Encoding
|
||||||
[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"
|
||||||
@@ -13,51 +17,93 @@ $Symlink = "$InstallDir\newt_latest.exe"
|
|||||||
$UpdaterTaskName = "MAIEREDV-Newt-Updater"
|
$UpdaterTaskName = "MAIEREDV-Newt-Updater"
|
||||||
$GiteaUrl = "https://me-gitea.maieredv.cloud/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site-win_v2.ps1"
|
$GiteaUrl = "https://me-gitea.maieredv.cloud/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site-win_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. Umgebung vorbereiten
|
||||||
function Prepare-Environment {
|
function Prepare-Environment {
|
||||||
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
||||||
try { irm winget.pro | iex } catch { Write-Log "Winget Error" "Red" }
|
Write-Log "Winget fehlt. Installiere via winget.pro..." "Cyan"
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
try {
|
||||||
|
Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression
|
||||||
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
|
} catch { Write-Log "FEHLER: Winget-Basis fehlt." "Red"; exit 1 }
|
||||||
}
|
}
|
||||||
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
||||||
|
Write-Log "NSSM wird via Winget geladen..." "Cyan"
|
||||||
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
||||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 4. Download & Hard-Kill Update
|
||||||
function Download-Newt {
|
function Download-Newt {
|
||||||
param($FullVersion)
|
param($FullVersion)
|
||||||
$Arch = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
|
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
|
||||||
$VOnly = $FullVersion.TrimStart('v')
|
$VersionOnly = $FullVersion.TrimStart('v')
|
||||||
$Url = "https://github.com/$Repo/releases/download/$VOnly/newt_$Arch"
|
$Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}"
|
||||||
$Target = "$InstallDir\newt_$VOnly.exe"
|
$Target = "${InstallDir}\newt_${VersionOnly}.exe"
|
||||||
|
|
||||||
if (!(Test-Path $InstallDir)) { New-Item $InstallDir -ItemType Directory -Force | Out-Null }
|
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
||||||
|
|
||||||
if (!(Test-Path $Target)) {
|
if (!(Test-Path $Target)) {
|
||||||
Write-Log "Download $VOnly..." "Cyan"
|
Write-Log "Downloade Version $VersionOnly..." "Cyan"
|
||||||
try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop }
|
try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop }
|
||||||
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- Dienst-Stopp mit Hard-Kill Fallback ---
|
||||||
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
$Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
$WasRunning = $Svc -and $Svc.Status -eq 'Running'
|
$WasRunning = $Svc -and $Svc.Status -eq 'Running'
|
||||||
if ($WasRunning) { Stop-Service $ServiceName -Force }
|
|
||||||
|
|
||||||
try { Copy-Item -Path $Target -Destination $Symlink -Force } catch { Write-Log "Copy Error" "Red" }
|
if ($WasRunning) {
|
||||||
|
Write-Log "Beende Dienst $ServiceName (Warte max 30s)..." "Yellow"
|
||||||
if ($WasRunning) { Start-Service $ServiceName }
|
Stop-Service $ServiceName -Force
|
||||||
|
|
||||||
|
$timeout = 30
|
||||||
|
$timer = [System.Diagnostics.Stopwatch]::StartNew()
|
||||||
|
while (((Get-Service $ServiceName).Status -ne 'Stopped') -and ($timer.Elapsed.TotalSeconds -lt $timeout)) {
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
}
|
||||||
|
$timer.Stop()
|
||||||
|
|
||||||
# Cleanup: Behalte die neuesten 2 exen
|
# Hard-Kill wenn er immer noch blockiert
|
||||||
Get-ChildItem $InstallDir -Filter "newt_*.exe" | Where { $_.Name -ne "newt_latest.exe" } | Sort LastWriteTime -Desc | Select -Skip 2 | Remove-Item -Force
|
if ((Get-Service $ServiceName).Status -ne 'Stopped') {
|
||||||
|
Write-Log "Dienst klemmt! Erzeinge Abbruch (Hard-Kill)..." "Red"
|
||||||
|
Stop-Process -Name "newt*" -Force -ErrorAction SilentlyContinue
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
||||||
|
Write-Log "Datei erfolgreich auf $VersionOnly getauscht." "Green"
|
||||||
|
} catch {
|
||||||
|
Write-Log "FEHLER: Datei $Symlink ist trotz Kill gesperrt!" "Red"
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($WasRunning) {
|
||||||
|
Start-Service $ServiceName
|
||||||
|
Write-Log "Dienst wurde neu gestartet." "Green"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Cleanup: Behalte die neuesten 2 Versionen
|
||||||
|
Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
|
||||||
|
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
||||||
|
Sort-Object LastWriteTime -Descending |
|
||||||
|
Select-Object -Skip 2 | Remove-Item -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
function Setup-Service {
|
function Setup-Service {
|
||||||
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
||||||
$ID = Read-Host "ID"; $Sec = Read-Host "Secret"; $End = Read-Host "Endpoint"
|
Write-Log "--- Erst-Konfiguration ---" "Yellow"
|
||||||
$Args = "--id $ID --secret $Sec --endpoint $End"
|
$PangolinID = Read-Host "Pangolin ID"
|
||||||
& nssm install $ServiceName "$Symlink" $Args
|
$PangolinSecret = Read-Host "Secret"
|
||||||
|
$PangolinEndpoint = Read-Host "Endpoint"
|
||||||
|
$ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}"
|
||||||
|
|
||||||
|
& nssm install $ServiceName "$Symlink" $ArgList
|
||||||
& nssm set $ServiceName AppRotateFiles 1
|
& nssm set $ServiceName AppRotateFiles 1
|
||||||
& nssm set $ServiceName AppRotateOnline 1
|
& nssm set $ServiceName AppRotateOnline 1
|
||||||
& nssm set $ServiceName AppRotateBytes 10485760
|
& nssm set $ServiceName AppRotateBytes 10485760
|
||||||
@@ -66,33 +112,44 @@ function Setup-Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Setup-Task {
|
function Setup-Task {
|
||||||
$Cmd = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; & ([scriptblock]::Create((New-Object System.Net.WebClient).DownloadString('$GiteaUrl'))) -mode update`""
|
# Wir nutzen den IEX-Befehl, der manuell funktioniert
|
||||||
$A = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; `$s=(New-Object System.Net.WebClient).DownloadString('$GiteaUrl'); `$b=[scriptblock]::Create(`$s); & `$b -mode update`""
|
$IexCommand = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl'))"
|
||||||
$T = New-ScheduledTaskTrigger -Daily -At 3am
|
|
||||||
$P = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
|
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCommand`""
|
||||||
Register-ScheduledTask -Action $A -Trigger $T -Principal $P -TaskName $UpdaterTaskName -Force | Out-Null
|
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
|
||||||
|
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
|
||||||
|
|
||||||
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $UpdaterTaskName -Force | Out-Null
|
||||||
|
Write-Log "Update-Task (03:00 Uhr) via IEX-Methode scharfgeschaltet." "Green"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Ausführung ---
|
# --- Main Logic ---
|
||||||
Prepare-Environment
|
Prepare-Environment
|
||||||
|
|
||||||
if ($mode -eq "install") {
|
# Wenn die latest.exe fehlt oder wir im Install-Mode sind -> Installieren
|
||||||
$v = (Invoke-RestMethod "https://api.github.com/repos/$Repo/releases/latest").tag_name
|
if (!(Test-Path $Symlink) -or ($mode -eq "install")) {
|
||||||
|
$v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name
|
||||||
Download-Newt $v
|
Download-Newt $v
|
||||||
Setup-Service
|
Setup-Service
|
||||||
Setup-Task
|
Setup-Task
|
||||||
Write-Log "Fertig." "Green"
|
Write-Log "🚀 Fertig installiert!" "Green"
|
||||||
}
|
}
|
||||||
elseif ($mode -eq "update") {
|
# Wenn wir nur updaten wollen oder der Task läuft
|
||||||
$v = (Invoke-RestMethod "https://api.github.com/repos/$Repo/releases/latest").tag_name
|
elseif ($mode -eq "update" -or (Test-Path $Symlink)) {
|
||||||
|
$v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name
|
||||||
$vO = $v.TrimStart('v')
|
$vO = $v.TrimStart('v')
|
||||||
if (!(Test-Path "$InstallDir\newt_$vO.exe")) { Download-Newt $v }
|
if (!(Test-Path "${InstallDir}\newt_${vO}.exe")) {
|
||||||
else { Write-Log "Aktuell." "Cyan" }
|
Write-Log "Neue Version $vO gefunden. Starte Update..." "Cyan"
|
||||||
|
Download-Newt $v
|
||||||
|
} else {
|
||||||
|
Write-Log "System ist aktuell ($vO)." "Cyan"
|
||||||
|
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
elseif ($mode -eq "uninstall") {
|
elseif ($mode -eq "uninstall") {
|
||||||
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
||||||
Stop-Service $ServiceName -Force
|
Stop-Service $ServiceName -Force
|
||||||
& nssm remove $ServiceName confirm
|
& nssm remove $ServiceName confirm
|
||||||
}
|
}
|
||||||
Unregister-ScheduledTask $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user