$ErrorActionPreference = 'Continue'
$keys = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',
        'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
$hits = Get-ItemProperty $keys -ErrorAction SilentlyContinue |
    Where-Object { $_.DisplayName -match 'Syncro|Kabuto|RepairTech' } |
    Select-Object DisplayName, DisplayVersion, PSChildName, UninstallString
if ($hits) { $hits | Format-List | Out-String | Write-Output } else { Write-Output 'NO-STALE-UNINSTALL-ENTRIES' }

$reboot = (Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending') -or
          (Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired')
Write-Output ('PENDING-REBOOT=' + $reboot)

$msi = 'C:\Windows\Temp\SyncroSetup-khalsa.msi'
if (-not (Test-Path $msi)) {
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor 3072
    (New-Object Net.WebClient).DownloadFile('https://rmm.syncromsp.com/dl/msi/djEtOTQ1NjU1NC0xNTA3NjA4MzYxLTQ4NzUzLTM2ODUwMjI=', $msi)
}
$log = 'C:\Windows\Temp\syncro-install.log'
$p = Start-Process msiexec.exe -ArgumentList '/i', $msi, '/qn', '/norestart', '/L*v', $log -Wait -PassThru
Write-Output ('msiexec exit=' + $p.ExitCode)
if (Test-Path $log) {
    Write-Output '--- last 60 log lines ---'
    Get-Content $log -Tail 60 | Write-Output
    Write-Output '--- return-value-3 context ---'
    $lines = Get-Content $log
    $idx = ($lines | Select-String 'Return value 3' | Select-Object -First 1).LineNumber
    if ($idx) { $lines[[math]::Max(0, $idx - 15)..[math]::Min($lines.Count - 1, $idx + 2)] | Write-Output }
}
