# ========================================== # Newt Installer - MAIEREDV # ========================================== 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" $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 Prepare-Environment { if (!(Get-Command winget -ErrorAction SilentlyContinue)) { try { irm winget.pro | iex } catch { Write-Log "Winget Error" "Red" } $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") } if (!(Get-Command nssm -ErrorAction SilentlyContinue)) { winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") } } function Download-Newt { param($FullVersion) $Arch = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" } $VOnly = $FullVersion.TrimStart('v') $Url = "https://github.com/$Repo/releases/download/$VOnly/newt_$Arch" $Target = "$InstallDir\newt_$VOnly.exe" if (!(Test-Path $InstallDir)) { New-Item $InstallDir -ItemType Directory -Force | Out-Null } if (!(Test-Path $Target)) { Write-Log "Download $VOnly..." "Cyan" try { Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop } catch { Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing } } $Svc = Get-Service $ServiceName -ErrorAction SilentlyContinue $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) { Start-Service $ServiceName } # Cleanup: Behalte die neuesten 2 exen Get-ChildItem $InstallDir -Filter "newt_*.exe" | Where { $_.Name -ne "newt_latest.exe" } | Sort LastWriteTime -Desc | Select -Skip 2 | Remove-Item -Force } function Setup-Service { if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { $ID = Read-Host "ID"; $Sec = Read-Host "Secret"; $End = Read-Host "Endpoint" $Args = "--id $ID --secret $Sec --endpoint $End" & nssm install $ServiceName "$Symlink" $Args & nssm set $ServiceName AppRotateFiles 1 & nssm set $ServiceName AppRotateOnline 1 & nssm set $ServiceName AppRotateBytes 10485760 Start-Service $ServiceName } } 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`"" $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`"" $T = New-ScheduledTaskTrigger -Daily -At 3am $P = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest Register-ScheduledTask -Action $A -Trigger $T -Principal $P -TaskName $UpdaterTaskName -Force | Out-Null } # --- Ausführung --- Prepare-Environment if ($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 "Fertig." "Green" } elseif ($mode -eq "update") { $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 "Aktuell." "Cyan" } } elseif ($mode -eq "uninstall") { if (Get-Service $ServiceName -ErrorAction SilentlyContinue) { Stop-Service $ServiceName -Force & nssm remove $ServiceName confirm } Unregister-ScheduledTask $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue }