$ErrorActionPreference = 'Continue'
$svc = Get-Service -Name 'Syncro','SyncroLive','SyncroOvermind' -ErrorAction SilentlyContinue
$path = Test-Path 'C:\Program Files\RepairTech\Syncro'
if ($svc -or $path) {
    $names = ($svc | ForEach-Object { $_.Name + '=' + $_.Status }) -join ','
    Write-Output ('SYNCRO-ALREADY-INSTALLED services=[' + $names + '] repairtech-path=' + $path)
} else {
    Write-Output 'SYNCRO-NOT-FOUND - installing'
    $msi = 'C:\Windows\Temp\SyncroSetup-khalsa.msi'
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor 3072
    (New-Object Net.WebClient).DownloadFile('https://rmm.syncromsp.com/dl/msi/djEtOTQ1NjU1NC0xNTA3NjA4MzYxLTQ4NzUzLTM2ODUwMjI=', $msi)
    Write-Output ('MSI downloaded ' + [math]::Round((Get-Item $msi).Length / 1MB, 1) + ' MB')
    $p = Start-Process msiexec.exe -ArgumentList '/i', $msi, '/qn', '/norestart' -Wait -PassThru
    Write-Output ('msiexec exit=' + $p.ExitCode)
    Start-Sleep -Seconds 30
    $svc2 = Get-Service -Name 'Syncro','SyncroLive','SyncroOvermind' -ErrorAction SilentlyContinue
    if ($svc2) {
        Write-Output ('SYNCRO-INSTALL-VERIFIED ' + (($svc2 | ForEach-Object { $_.Name + '=' + $_.Status }) -join ','))
    } else {
        Write-Output 'SYNCRO-SERVICES-NOT-FOUND-AFTER-INSTALL - check manually'
    }
    Remove-Item $msi -Force -ErrorAction SilentlyContinue
}
