@echo off
setlocal
title SoftwareAudit - Install auto scan
set "SRV=https://softwareaudit.service.bgs.co.th"
set "D="
set "TMPD=%TEMP%\SoftwareAudit-Install"
if not exist "%TMPD%" mkdir "%TMPD%"
echo ============================================================
echo  SoftwareAudit : install automatic scan (runs in background at every startup)
echo  (a UAC prompt will appear - click Yes)
echo ============================================================
echo Downloading from %SRV% ...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference='SilentlyContinue'; try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor 3072 } catch {}; try { Invoke-WebRequest -UseBasicParsing ('%SRV%/scanner/Agent.ps1?d=%D%') -OutFile '%TMPD%\Agent.ps1'; Unblock-File '%TMPD%\Agent.ps1' -ErrorAction SilentlyContinue; exit 0 } catch { Write-Host $_.Exception.Message; exit 1 }"
if errorlevel 1 (
  echo.
  echo Download failed - please check the network connection to %SRV% and try again.
  pause
  exit /b 1
)
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Start-Process powershell.exe -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File \"%TMPD%\Agent.ps1\" -Install -Pause'; exit 0 } catch { exit 2 }"
if errorlevel 2 (
  echo Administrator rights were not granted - continuing for the current user only ...
  powershell -NoProfile -ExecutionPolicy Bypass -File "%TMPD%\Agent.ps1" -Install -NoAdmin -Pause
)
