Skip to content

Commit e27a003

Browse files
committed
fix: enhance structure and logging in Windows script
- Added structured comments to improve readability and organization of the script. - Enhanced logging messages for better clarity during execution, particularly for directory selection and permission settings. - Improved error handling and messaging for missing marker files, ensuring clearer feedback on script status.
1 parent 9c54eb0 commit e27a003

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

  • apps/portal/src/app/api/download-agent/scripts

apps/portal/src/app/api/download-agent/scripts/windows.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ echo Date: %date% %time%
1616
echo ------------------------------------------------------------
1717
echo.
1818
19+
REM =========================
1920
REM Variables
21+
REM =========================
2022
set "ORG_ID=${orgId}"
2123
set "EMPLOYEE_ID=${employeeId}"
2224
set "PRIMARY_DIR=${fleetDevicePath}"
@@ -26,6 +28,7 @@ set "LOG_FILE="
2628
set "HAS_ERROR=0"
2729
set "ERRORS="
2830
set "EXIT_CODE=0"
31+
REM newline token (exactly this 2-line shape)
2932
set "nl=^
3033
"
3134
@@ -55,7 +58,11 @@ if not "!rc!"=="0" (
5558
endlocal & set "LAST_RC=%rc%"
5659
exit /b %LAST_RC%
5760
58-
REM Require Administrator (check High Mandatory Level) and exit with instructions if not elevated
61+
call :log_msg "Script starting"
62+
63+
REM =========================
64+
REM Admin check
65+
REM =========================
5966
whoami /groups | find "S-1-16-12288" >nul 2>&1
6067
if errorlevel 1 (
6168
color 0E
@@ -67,13 +74,16 @@ if errorlevel 1 (
6774
exit /b 5
6875
)
6976
70-
REM Ensure this script runs in a persistent cmd session that stays open after completion
77+
REM =========================
78+
REM Relaunch persistent window
79+
REM =========================
7180
if not "%PERSIST%"=="1" (
7281
set "PERSIST=1"
7382
call :log_msg "Re-launching in a persistent window"
7483
start "CompAI Device Setup" cmd /k "%~f0 %*"
7584
exit /b
7685
)
86+
7787
call :log_msg "Running with administrator privileges"
7888
call :log_msg "Current directory: %cd%"
7989
call :log_msg "Script path: %~f0"
@@ -82,20 +92,22 @@ cd /d "%~dp0"
8292
call :log_msg "New current directory: %cd%"
8393
echo.
8494
85-
REM Choose a writable directory (primary first, then fallback) without parentheses blocks
95+
REM =========================
96+
REM Choose writable directory
97+
REM =========================
8698
call :log_msg "Choosing destination directory; primary=%PRIMARY_DIR% fallback=%FALLBACK_DIR%"
87-
if exist "%PRIMARY_DIR%\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
99+
if exist "%PRIMARY_DIR%\\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
88100
if not defined CHOSEN_DIR call :log_run mkdir "%PRIMARY_DIR%"
89-
if not defined CHOSEN_DIR if exist "%PRIMARY_DIR%\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
101+
if not defined CHOSEN_DIR if exist "%PRIMARY_DIR%\\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
90102
91103
if not defined CHOSEN_DIR call :log_msg "Primary not available; trying fallback"
92-
if not defined CHOSEN_DIR if exist "%FALLBACK_DIR%\NUL" set "CHOSEN_DIR=%FALLBACK_DIR%"
104+
if not defined CHOSEN_DIR if exist "%FALLBACK_DIR%\\NUL" set "CHOSEN_DIR=%FALLBACK_DIR%"
93105
if not defined CHOSEN_DIR call :log_run mkdir "%FALLBACK_DIR%"
94-
if not defined CHOSEN_DIR if exist "%FALLBACK_DIR%\NUL" set "CHOSEN_DIR=%FALLBACK_DIR%"
106+
if not defined CHOSEN_DIR if exist "%FALLBACK_DIR%\\NUL" set "CHOSEN_DIR=%FALLBACK_DIR%"
95107
96108
if not defined CHOSEN_DIR (
97109
color 0E
98-
echo WARNING: No writable directory found.
110+
call :log_msg "WARNING: No writable directory found"
99111
echo Primary attempted: "%PRIMARY_DIR%"
100112
echo Fallback attempted: "%FALLBACK_DIR%"
101113
echo [%date% %time%] No writable directory found. Primary: %PRIMARY_DIR%, Fallback: %FALLBACK_DIR% >> "%~dp0setup.log"
@@ -107,7 +119,7 @@ if not defined CHOSEN_DIR (
107119
set "MARKER_DIR=%CHOSEN_DIR%"
108120
if not "!MARKER_DIR:~-1!"=="\\" set "MARKER_DIR=!MARKER_DIR!\\"
109121
110-
REM switch the log file to the chosen directory, carrying over bootstrap logs
122+
REM switch the log file to the chosen directory, carry over bootstrap logs
111123
set "FINAL_LOG=!MARKER_DIR!setup.log"
112124
if /i not "%LOG_FILE%"=="%FINAL_LOG%" (
113125
call :log_msg "Switching log to !FINAL_LOG!"
@@ -119,7 +131,9 @@ if not defined CHOSEN_DIR (
119131
echo Logs will be written to: %LOG_FILE%
120132
echo.
121133
134+
REM =========================
122135
REM Write marker files
136+
REM =========================
123137
if defined CHOSEN_DIR (
124138
call :log_msg "Writing organization marker file"
125139
call :log_msg "Preparing to write org marker to !MARKER_DIR!!ORG_ID!"
@@ -150,7 +164,9 @@ if defined CHOSEN_DIR (
150164
)
151165
)
152166
153-
REM Set permissive read ACLs for SYSTEM and Administrators
167+
REM =========================
168+
REM Permissions
169+
REM =========================
154170
if defined CHOSEN_DIR (
155171
call :log_msg "Setting permissions on marker directory"
156172
call :log_run icacls "!MARKER_DIR!" /inheritance:e
@@ -162,13 +178,16 @@ if defined CHOSEN_DIR (
162178
call :log_run icacls "!MARKER_DIR!!EMPLOYEE_ID!" /grant *S-1-5-18:R *S-1-5-32-544:R
163179
)
164180
181+
REM =========================
182+
REM Verify
183+
REM =========================
165184
echo.
166185
echo Verifying markers...
167186
if defined CHOSEN_DIR (
168187
call :log_msg "Verifying marker exists: !MARKER_DIR!!EMPLOYEE_ID!"
169188
if not exist "!MARKER_DIR!!EMPLOYEE_ID!" (
170189
color 0E
171-
echo WARNING: Employee marker file missing at !MARKER_DIR!!EMPLOYEE_ID!
190+
call :log_msg "WARNING: Employee marker file missing at !MARKER_DIR!!EMPLOYEE_ID!"
172191
echo [%date% %time%] Verification failed: employee marker file missing >> "!LOG_FILE!"
173192
set "HAS_ERROR=1"
174193
set "ERRORS=!ERRORS!- Employee marker file missing at !MARKER_DIR!!EMPLOYEE_ID!!.!nl!"
@@ -179,6 +198,9 @@ if defined CHOSEN_DIR (
179198
)
180199
rem Skipping registry checks per request
181200
201+
REM =========================
202+
REM Result / Exit
203+
REM =========================
182204
echo.
183205
echo ------------------------------------------------------------
184206
if "%HAS_ERROR%"=="0" (
@@ -207,5 +229,5 @@ echo Press any key to close this window. This will not affect installation.
207229
pause
208230
if "%HAS_ERROR%"=="0" (exit /b 0) else (exit /b %EXIT_CODE%)`;
209231

210-
return script.replace(/\\n/g, '\r\n');
232+
return script.replace(/\n/g, '\r\n');
211233
}

0 commit comments

Comments
 (0)