@@ -25,25 +25,51 @@ set "CHOSEN_DIR="
2525set "LOG_FILE="
2626set "HAS_ERROR=0"
2727set "ERRORS="
28+ set "EXIT_CODE=0"
2829
29- REM Self-elevate if not running as administrator
30- net session >nul 2>&1
31- if not %errorlevel%==0 (
30+ REM Capture elevation flag if passed
31+ set "ELEVATED_FLAG=%~1"
32+
33+ REM Self-elevate if not running as administrator (check High Mandatory Level)
34+ whoami /groups | find "S-1-16-12288" >nul 2>&1
35+ if errorlevel 1 if /I not "%ELEVATED_FLAG%"=="elevated" (
3236 color 0E
3337 echo Administrator permission is required.
3438 echo A prompt will appear asking for permission. Please click "Yes".
3539 echo If no prompt appears, right-click this file and select "Run as administrator".
3640 echo.
37- echo Elevating... A new window will open and remain open after running.
38- > "%TEMP%\getadmin.vbs" echo Set UAC = CreateObject^("Shell.Application"^)
39- >> "%TEMP%\getadmin.vbs" echo UAC.ShellExecute "%~f0", "", "", "runas", 1
40- wscript "%TEMP%\getadmin.vbs" >nul 2>&1
41- del "%TEMP%\getadmin.vbs" >nul 2>&1
42- echo This window will now close. The setup will continue in the new (admin) window.
43- pause
44- exit /b
41+ echo Choose an option:
42+ echo [Y] Try to auto-elevate (recommended)
43+ echo [N] I will re-run manually as Administrator
44+ choice /C YN /N /M "Attempt automatic elevation? (Y/N): "
45+ if errorlevel 2 (
46+ echo.
47+ echo Please close this window and re-run the script by right-clicking it and selecting "Run as administrator".
48+ echo Press any key to exit.
49+ pause >nul
50+ exit /b
51+ ) else (
52+ echo Elevating... A new window will open and remain open after running.
53+ rem Try PowerShell elevation first (keeps admin window open with /k)
54+ powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/k','\"%~f0 elevated\"' -WorkingDirectory '%~dp0'" 2>nul
55+ rem Fallback to VBScript if PowerShell attempt failed or is blocked
56+ if not exist "%TEMP%\elevate.tmp" (
57+ > "%TEMP%\getadmin.vbs" echo Set UAC = CreateObject^("Shell.Application"^)
58+ >> "%TEMP%\getadmin.vbs" echo UAC.ShellExecute "%~f0", "elevated", "", "runas", 1
59+ wscript "%TEMP%\getadmin.vbs" >nul 2>&1
60+ del "%TEMP%\getadmin.vbs" >nul 2>&1
61+ )
62+ echo.
63+ echo This window will now stay open so you can see messages.
64+ echo Once the new admin window appears, you may close this one after confirming progress.
65+ echo Press any key to exit this non-admin window.
66+ pause >nul
67+ exit /b
68+ )
4569)
4670echo Running with administrator privileges.
71+ echo Current directory: %cd%
72+ echo Script path: %~f0
4773echo.
4874
4975REM Choose a writable directory (primary first, then fallback)
@@ -67,6 +93,7 @@ if not defined CHOSEN_DIR (
6793 set "LOG_FILE=%~dp0setup.log"
6894 set "HAS_ERROR=1"
6995 set "ERRORS=!ERRORS!- No writable directory found (Primary: %PRIMARY_DIR%, Fallback: %FALLBACK_DIR%).!nl!"
96+ set "EXIT_CODE=1"
7097) else (
7198 set "MARKER_DIR=%CHOSEN_DIR%"
7299 if not "%MARKER_DIR:~-1%"=="\\" set "MARKER_DIR=%MARKER_DIR%\\"
@@ -86,6 +113,7 @@ if defined CHOSEN_DIR (
86113 echo [%date% %time%] Failed writing org marker file >> "%LOG_FILE%"
87114 set "HAS_ERROR=1"
88115 set "ERRORS=!ERRORS!- Failed writing organization marker file.!nl!"
116+ set "EXIT_CODE=1"
89117 ) else (
90118 echo [OK] Organization marker file: %MARKER_DIR%%ORG_ID%
91119 )
@@ -98,6 +126,7 @@ if defined CHOSEN_DIR (
98126 echo [%date% %time%] Failed writing employee marker file >> "%LOG_FILE%"
99127 set "HAS_ERROR=1"
100128 set "ERRORS=!ERRORS!- Failed writing employee marker file.!nl!"
129+ set "EXIT_CODE=1"
101130 ) else (
102131 echo [OK] Employee marker file: %MARKER_DIR%%EMPLOYEE_ID%
103132 )
@@ -122,6 +151,7 @@ if %errorlevel%==0 (
122151 echo [%date% %time%] Failed writing OrgId to HKLM >> "%LOG_FILE%"
123152 set "HAS_ERROR=1"
124153 set "ERRORS=!ERRORS!- Failed writing OrgId to HKLM registry.!nl!"
154+ set "EXIT_CODE=1"
125155 )
126156 reg add "HKLM\SOFTWARE\CompAI\Device" /v EmployeeId /t REG_SZ /d "%EMPLOYEE_ID%" /f >nul 2>&1
127157 if errorlevel 1 (
@@ -130,6 +160,7 @@ if %errorlevel%==0 (
130160 echo [%date% %time%] Failed writing EmployeeId to HKLM >> "%LOG_FILE%"
131161 set "HAS_ERROR=1"
132162 set "ERRORS=!ERRORS!- Failed writing EmployeeId to HKLM registry.!nl!"
163+ set "EXIT_CODE=1"
133164 )
134165) else (
135166 color 0E
@@ -143,6 +174,7 @@ if %errorlevel%==0 (
143174 echo [%date% %time%] Failed writing OrgId to HKCU >> "%LOG_FILE%"
144175 set "HAS_ERROR=1"
145176 set "ERRORS=!ERRORS!- Failed writing OrgId to HKCU registry.!nl!"
177+ set "EXIT_CODE=1"
146178 )
147179 reg add "HKCU\Software\CompAI\Device" /v EmployeeId /t REG_SZ /d "%EMPLOYEE_ID%" /f >nul 2>&1
148180 if errorlevel 1 (
@@ -151,6 +183,7 @@ if %errorlevel%==0 (
151183 echo [%date% %time%] Failed writing EmployeeId to HKCU >> "%LOG_FILE%"
152184 set "HAS_ERROR=1"
153185 set "ERRORS=!ERRORS!- Failed writing EmployeeId to HKCU registry.!nl!"
186+ set "EXIT_CODE=1"
154187 )
155188)
156189
@@ -162,7 +195,8 @@ if defined CHOSEN_DIR (
162195 echo WARNING: Employee marker file missing at %MARKER_DIR%%EMPLOYEE_ID%
163196 echo [%date% %time%] Verification failed: employee marker file missing >> "%LOG_FILE%"
164197 set "HAS_ERROR=1"
165- set "ERRORS=!ERRORS!- Employee marker file missing at %CHOSEN_DIR%\%EMPLOYEE_ID%.!nl!"
198+ set "ERRORS=!ERRORS!- Employee marker file missing at %MARKER_DIR%%EMPLOYEE_ID%.!nl!"
199+ set "EXIT_CODE=2"
166200 ) else (
167201 echo [OK] Employee marker file present.
168202 )
@@ -175,6 +209,7 @@ if errorlevel 1 (
175209 echo [%date% %time%] Warning: registry EmployeeId value not found or mismatched >> "%LOG_FILE%"
176210 set "HAS_ERROR=1"
177211 set "ERRORS=!ERRORS!- Registry EmployeeId not found or mismatched in HKLM/HKCU.!nl!"
212+ set "EXIT_CODE=2"
178213) else (
179214 echo [OK] Registry value found for EmployeeId.
180215)
@@ -202,8 +237,8 @@ if "%HAS_ERROR%"=="0" (
202237echo ------------------------------------------------------------
203238echo.
204239echo Press any key to close this window. This will not affect installation.
205- pause >nul
206- if "%HAS_ERROR%"=="0" (exit /b 0) else (exit /b 1 )` ;
240+ pause
241+ if "%HAS_ERROR%"=="0" (exit /b 0) else (exit /b %EXIT_CODE% )` ;
207242
208243 return script . replace ( / \n / g, '\r\n' ) ;
209244}
0 commit comments