@echo off
setlocal
set log=%~dpn0.log
set task=%~n0Task
echo Starting...
call :run 
echo Script completed.
exit
:run
echo Checking admin rights...
call :check_admin
if errorlevel 1 goto :run_as_admin
:try_get_admin
choice /C YN /M "Admin rights not detected. Attempt to run as an administrator"
if %errorlevel% neq 1 goto :eof
powershell -Command Start-Process -Verb RunAs "%comspec%" /k,"%~f0"
if errorlevel 1 goto :eof
exit
:run_as_admin
echo Running elevated, we need to go deeper...
call :check_system
if errorlevel 1 goto :run_as_system
:try_get_system
schtasks /Create /TN %task% /TR "%~f0" /RU SYSTEM /RL HIGHEST /SC ONCE /ST 23:59 /F
timeout 2 >nul
if errorlevel 1 goto :try_get_system_error
schtasks /Run /TN %task% /I
timeout 2 >nul
if errorlevel 1 goto :try_get_system_error
echo Script is running as system, its output will be written to "%log%"
timeout 2 >nul
explorer.exe /select,"%log%"
timeout 2 >nul
powershell -Command Get-Content -Wait "%log%"
timeout 99
exit
:try_get_system_error
echo result: %errorlevel%
timeout 99
exit
:check_admin
:: if admin return 1
fsutil dirty query %SystemDrive% >nul 2>&1
if errorlevel 1 exit /b 0
exit /b 1
goto :eof
:check_system
:: if system return 1
echo whoami:
whoami /user /fo csv /nh
for /F "usebackq delims=, tokens=2" %%i in (`"whoami /user /fo csv /nh"`) do set sid=%%i
if "%sid%" neq ""S-1-5-18"" exit /b 0
exit /b 1
:run_as_system
schtasks /Delete /TN %task% /F 2>nul >nul
call :log_dir_create "%log%"
call :main >"%log%" 2>&1
goto :eof
:main
echo [%time%] Starting %~f0...
call :clean_msi
call :stop Kabuto
call :stop Syncro
call :clean Kabuto
call :clean Syncro
call :clean_data Kabuto
call :clean_data Syncro
echo [%time%] Completed, press Ctrl-C to close the console
goto :eof
:stop
:: safety assertion 
if "%~1" neq "Syncro" if "%~1" neq "Kabuto" exit /b 1
echo --
echo -- Stopping %1 --
echo --
echo Stopping Services
for %%s in (%1 %1Live %1Overmind) do sc config %%s start= disabled
for %%s in (%1 %1Live %1Overmind) do net stop %%s
for %%s in (%1 %1Live %1Overmind) do sc delete %%s
for %%s in (%1 Syncro_RemoteControl) do sc config %%s start= disabled
for %%s in (%1 Syncro_RemoteControl) do net stop %%s
for %%s in (%1 Syncro_RemoteControl) do sc delete %%s
echo Killing Processes
taskkill /im %1.App.Runner.exe /f 
taskkill /im %1.Service.Runner.exe /f
taskkill /im %1Live.Agent.Runner.exe /f
taskkill /im %1Live.Agent.Service.exe /f
taskkill /im %1.Overmind.Service.exe /f
taskkill /im %1.Access.Service.exe /f
taskkill /im %1.Access.App.exe /f
timeout 5 /nobreak >nul
goto :eof
:clean
:: safety assertion 
if "%~1" neq "Syncro" if "%~1" neq "Kabuto" exit /b 1
echo --
echo -- Removing %1 --
echo --
echo Removing Files
rmdir "C:\Program Files\%1" /s /q
rmdir "C:\Program Files (x86)\%1" /s /q
rmdir "C:\Program Files\RepairTech\%1" /s /q
rmdir "C:\Program Files\RepairTech\LiveAgent" /s /q
rmdir "C:\Program Files\RepairTech\SquirrelTemp" /s /q
rmdir "C:\Program Files\RepairTech"
echo Removing Registry files for App and Service
reg delete HKLM\Software\WOW6432Node\RepairTech\%1 /f
reg delete HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\%1 /f
reg delete HKLM\Software\RepairTech\%1 /f
reg delete HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\%1 /f
reg delete HKLM\System\CurrentControlSet\Services\%1 /f
reg delete HKLM\System\CurrentControlSet\Services\%1Live /f
goto :eof
:clean_msi
echo --
echo -- Removing MSI --
echo --
MsiExec.exe /x{B7F56D3D-2AD3-4021-9D36-3B9E9C9FBE33} /qn /norestart
reg delete HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{B7F56D3D-2AD3-4021-9D36-3B9E9C9FBE33} /f
reg delete HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{B7F56D3D-2AD3-4021-9D36-3B9E9C9FBE33} /f
reg delete HKLM\SOFTWARE\Classes\Installer\Products\D3D65F7B3DA21204D963B3E9C9F9EB33 /f
goto :eof
:clean_data
echo --
echo -- Removing %1 data --
echo --
rmdir "C:\ProgramData\%1" /s /q
goto :eof
:log_dir_create
if not exist %~dp1 mkdir %~dp1
goto :eof