<# .SYNOPSIS Windows-Installer für den Newt-Client (MAIEREDV). v2.6 - Fix für Parser-Error (Variable Drive Reference) & Encoding. #> param([string]$mode = "install") [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $Repo = "fosrl/newt" $InstallDir = "C:\Program Files\me-msp-newt" $ServiceName = "MAIEREDV-Managed-Site-Client" $Symlink = "$InstallDir\newt_latest.exe" $LogFile = "$InstallDir\newt_service.log" $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" function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color } function Download-Newt { param($FullVersion) $ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } $VersionOnly = $FullVersion.TrimStart('v') $Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}" $Target = "${InstallDir}\newt_${VersionOnly}.exe" if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } if (!(Test-Path $Target)) { Write-Log "Download $VersionOnly..." "Cyan" try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop } catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing } } Write-Log "Vorbereitung Datei-Update..." "Yellow" if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { Set-Service $ServiceName -StartupType Disabled Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue } # Prozess-Kill ohne Doppelpunkt-Variablen-Falle for ($i = 1; $i -le 5; $i++) { $Procs = Get-Process -Name "newt*" -ErrorAction SilentlyContinue if ($Procs) { $pCount = $Procs.Count Write-Log "Versuch $i - Kill hängende Prozesse ($pCount)..." "Red" taskkill.exe /F /T /IM "newt*" 2>$null Start-Sleep -Seconds 3 } else { break } } try { if (Test-Path $Symlink) { $TempName = "$Symlink.dead_" + (Get-Date -Format "yyyyMMdd_HHmmss") Move-Item -Path $Symlink -Destination $TempName -Force -ErrorAction Stop Write-Log "Alte Datei verschoben." "Green" } Copy-Item -Path $Target -Destination $Symlink -Force Write-Log "Update erfolgreich." "Green" } catch { Write-Log "Dateisperre konnte nicht gelöst werden." "Red" } Set-Service $ServiceName -StartupType Automatic Start-Service $ServiceName -ErrorAction SilentlyContinue } function Setup-Service { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { Write-Log "Setup Dienst..." "Yellow" $PangolinID = Read-Host "ID"; $PangolinSecret = Read-Host "Secret"; $PangolinEndpoint = Read-Host "Endpoint" $ArgList = "--id ${PangolinID} --secret ${PangolinSecret} --endpoint ${PangolinEndpoint}" & nssm install $ServiceName "$Symlink" $ArgList } & nssm set $ServiceName AppStdout "$LogFile" & nssm set $ServiceName AppStderr "$LogFile" & nssm set $ServiceName AppRotateFiles 1 & nssm set $ServiceName AppRotateOnline 1 & nssm set $ServiceName AppRotateBytes 10485760 } function Setup-Task { $IexCmd = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl'))" $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$IexCmd`"" $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 } # --- Main Logic --- if (!(Get-Command nssm -ErrorAction SilentlyContinue)) { if (!(Get-Command winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch {} } winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") } if (!(Test-Path $Symlink) -or ($mode -eq "install")) { $v = (Invoke-RestMethod "https://api.github.com/repos/${Repo}/releases/latest").tag_name Download-Newt $v; Setup-Service; Setup-Task Write-Log "Installation fertig!" "Green" } 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') if (!(Test-Path "${InstallDir}\newt_${vO}.exe")) { Download-Newt $v } else { Write-Log "System aktuell." "Cyan" Setup-Service if ((Get-Service $ServiceName).Status.value__ -ne 4) { Start-Service $ServiceName } } } elseif ($mode -eq "uninstall") { taskkill.exe /F /T /IM "newt*" 2>$null if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue & nssm remove $ServiceName confirm } Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue }