Skip to content

Commit 9c54eb0

Browse files
committed
fix: enhance logging and error handling in Windows script
- Introduced a logging mechanism to capture messages and command executions, improving visibility during script execution. - Updated directory handling to switch log files based on the chosen directory, ensuring logs are correctly maintained. - Enhanced error reporting for marker file writing and permission setting, providing clearer feedback on issues encountered during execution.
1 parent 042db43 commit 9c54eb0

1 file changed

Lines changed: 70 additions & 31 deletions

File tree

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

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

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ set "HAS_ERROR=0"
2727
set "ERRORS="
2828
set "EXIT_CODE=0"
2929
set "nl=^
30+
"
31+
32+
REM --- bootstrap log (updated once CHOSEN_DIR is known) ---
33+
set "LOG_FILE=%~dp0setup.log"
34+
35+
REM --- log a message to console and log ---
36+
:log_msg
37+
setlocal EnableDelayedExpansion
38+
set "msg=%~1"
39+
echo [%date% %time%] !msg!
40+
>>"%LOG_FILE%" echo [%date% %time%] !msg!
41+
endlocal & exit /b 0
42+
43+
REM --- run a command with logging and error capture ---
44+
:log_run
45+
setlocal EnableDelayedExpansion
46+
set "cmdline=%*"
47+
echo [%date% %time%] CMD: !cmdline!
48+
>>"%LOG_FILE%" echo [%date% %time%] CMD: !cmdline!
49+
%*
50+
set "rc=!errorlevel!"
51+
if not "!rc!"=="0" (
52+
echo [%date% %time%] ERR !rc!: !cmdline!
53+
>>"%LOG_FILE%" echo [%date% %time%] ERR !rc!: !cmdline!
54+
)
55+
endlocal & set "LAST_RC=%rc%"
56+
exit /b %LAST_RC%
3057
3158
REM Require Administrator (check High Mandatory Level) and exit with instructions if not elevated
3259
whoami /groups | find "S-1-16-12288" >nul 2>&1
@@ -43,29 +70,27 @@ if errorlevel 1 (
4370
REM Ensure this script runs in a persistent cmd session that stays open after completion
4471
if not "%PERSIST%"=="1" (
4572
set "PERSIST=1"
46-
echo Re-launching in a persistent window...
73+
call :log_msg "Re-launching in a persistent window"
4774
start "CompAI Device Setup" cmd /k "%~f0 %*"
4875
exit /b
4976
)
50-
echo Running with administrator privileges.
51-
echo Current directory: %cd%
52-
echo Script path: %~f0
53-
echo Switching working directory to script folder...
77+
call :log_msg "Running with administrator privileges"
78+
call :log_msg "Current directory: %cd%"
79+
call :log_msg "Script path: %~f0"
80+
call :log_msg "Switching working directory to script folder"
5481
cd /d "%~dp0"
55-
echo New current directory: %cd%
82+
call :log_msg "New current directory: %cd%"
5683
echo.
5784
5885
REM Choose a writable directory (primary first, then fallback) without parentheses blocks
59-
echo Choosing destination directory...
60-
echo Primary: "%PRIMARY_DIR%"
86+
call :log_msg "Choosing destination directory; primary=%PRIMARY_DIR% fallback=%FALLBACK_DIR%"
6187
if exist "%PRIMARY_DIR%\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
62-
if not defined CHOSEN_DIR echo CMD: mkdir "%PRIMARY_DIR%"
63-
if not defined CHOSEN_DIR mkdir "%PRIMARY_DIR%" 2>&1
88+
if not defined CHOSEN_DIR call :log_run mkdir "%PRIMARY_DIR%"
6489
if not defined CHOSEN_DIR if exist "%PRIMARY_DIR%\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
65-
if not defined CHOSEN_DIR echo Fallback: "%FALLBACK_DIR%"
90+
91+
if not defined CHOSEN_DIR call :log_msg "Primary not available; trying fallback"
6692
if not defined CHOSEN_DIR if exist "%FALLBACK_DIR%\NUL" set "CHOSEN_DIR=%FALLBACK_DIR%"
67-
if not defined CHOSEN_DIR echo CMD: mkdir "%FALLBACK_DIR%"
68-
if not defined CHOSEN_DIR mkdir "%FALLBACK_DIR%" 2>&1
93+
if not defined CHOSEN_DIR call :log_run mkdir "%FALLBACK_DIR%"
6994
if not defined CHOSEN_DIR if exist "%FALLBACK_DIR%\NUL" set "CHOSEN_DIR=%FALLBACK_DIR%"
7095
7196
if not defined CHOSEN_DIR (
@@ -81,54 +106,66 @@ if not defined CHOSEN_DIR (
81106
) else (
82107
set "MARKER_DIR=%CHOSEN_DIR%"
83108
if not "!MARKER_DIR:~-1!"=="\\" set "MARKER_DIR=!MARKER_DIR!\\"
84-
set "LOG_FILE=!MARKER_DIR!setup.log"
85-
echo Using directory: !MARKER_DIR!
109+
110+
REM switch the log file to the chosen directory, carrying over bootstrap logs
111+
set "FINAL_LOG=!MARKER_DIR!setup.log"
112+
if /i not "%LOG_FILE%"=="%FINAL_LOG%" (
113+
call :log_msg "Switching log to !FINAL_LOG!"
114+
if exist "%LOG_FILE%" type "%LOG_FILE%" >> "!FINAL_LOG!" & del "%LOG_FILE%"
115+
set "LOG_FILE=!FINAL_LOG!"
116+
)
117+
call :log_msg "Using directory: !MARKER_DIR!"
86118
)
87119
echo Logs will be written to: %LOG_FILE%
88120
echo.
89121
90122
REM Write marker files
91123
if defined CHOSEN_DIR (
92-
echo Writing organization marker file...
93-
echo CMD: write org marker to "%MARKER_DIR%%ORG_ID%"
94-
(echo %ORG_ID%) > "%MARKER_DIR%%ORG_ID%" 2>>"%LOG_FILE%"
124+
call :log_msg "Writing organization marker file"
125+
call :log_msg "Preparing to write org marker to !MARKER_DIR!!ORG_ID!"
126+
call :log_run cmd /c "(echo %ORG_ID%) > \"!MARKER_DIR!!ORG_ID!\""
95127
if errorlevel 1 (
96128
color 0E
97-
echo WARNING: Failed writing organization marker file to %MARKER_DIR%.
129+
call :log_msg "WARNING: Failed writing organization marker file to !MARKER_DIR!"
98130
echo [%date% %time%] Failed writing org marker file >> "%LOG_FILE%"
99131
set "HAS_ERROR=1"
100132
set "ERRORS=!ERRORS!- Failed writing organization marker file.!nl!"
101133
set "EXIT_CODE=1"
102134
) else (
103-
echo [OK] Organization marker file: %MARKER_DIR%%ORG_ID%
135+
call :log_msg "[OK] Organization marker file: !MARKER_DIR!!ORG_ID!"
104136
)
105137
106-
echo Writing employee marker file...
107-
echo CMD: write employee marker to "%MARKER_DIR%%EMPLOYEE_ID%"
108-
(echo %EMPLOYEE_ID%) > "%MARKER_DIR%%EMPLOYEE_ID%" 2>>"%LOG_FILE%"
138+
call :log_msg "Writing employee marker file"
139+
call :log_msg "Preparing to write employee marker to !MARKER_DIR!!EMPLOYEE_ID!"
140+
call :log_run cmd /c "(echo %EMPLOYEE_ID%) > \"!MARKER_DIR!!EMPLOYEE_ID!\""
109141
if errorlevel 1 (
110142
color 0E
111-
echo WARNING: Failed writing employee marker file to %MARKER_DIR%.
143+
call :log_msg "WARNING: Failed writing employee marker file to !MARKER_DIR!"
112144
echo [%date% %time%] Failed writing employee marker file >> "%LOG_FILE%"
113145
set "HAS_ERROR=1"
114146
set "ERRORS=!ERRORS!- Failed writing employee marker file.!nl!"
115147
set "EXIT_CODE=1"
116148
) else (
117-
echo [OK] Employee marker file: %MARKER_DIR%%EMPLOYEE_ID%
149+
call :log_msg "[OK] Employee marker file: !MARKER_DIR!!EMPLOYEE_ID!"
118150
)
119151
)
120152
121153
REM Set permissive read ACLs for SYSTEM and Administrators
122154
if defined CHOSEN_DIR (
123-
echo Setting permissions on marker files...
124-
icacls "!MARKER_DIR!" /inheritance:e >nul 2>&1
125-
icacls "!MARKER_DIR!!ORG_ID!" /grant *S-1-5-18:R *S-1-5-32-544:R >nul 2>&1
126-
icacls "!MARKER_DIR!!EMPLOYEE_ID!" /grant *S-1-5-18:R *S-1-5-32-544:R >nul 2>&1
155+
call :log_msg "Setting permissions on marker directory"
156+
call :log_run icacls "!MARKER_DIR!" /inheritance:e
157+
158+
call :log_msg "Granting read to SYSTEM and Administrators on org marker"
159+
call :log_run icacls "!MARKER_DIR!!ORG_ID!" /grant *S-1-5-18:R *S-1-5-32-544:R
160+
161+
call :log_msg "Granting read to SYSTEM and Administrators on employee marker"
162+
call :log_run icacls "!MARKER_DIR!!EMPLOYEE_ID!" /grant *S-1-5-18:R *S-1-5-32-544:R
127163
)
128164
129165
echo.
130166
echo Verifying markers...
131167
if defined CHOSEN_DIR (
168+
call :log_msg "Verifying marker exists: !MARKER_DIR!!EMPLOYEE_ID!"
132169
if not exist "!MARKER_DIR!!EMPLOYEE_ID!" (
133170
color 0E
134171
echo WARNING: Employee marker file missing at !MARKER_DIR!!EMPLOYEE_ID!
@@ -137,7 +174,7 @@ if defined CHOSEN_DIR (
137174
set "ERRORS=!ERRORS!- Employee marker file missing at !MARKER_DIR!!EMPLOYEE_ID!!.!nl!"
138175
set "EXIT_CODE=2"
139176
) else (
140-
echo [OK] Employee marker file present: !MARKER_DIR!!EMPLOYEE_ID!
177+
call :log_msg "[OK] Employee marker file present: !MARKER_DIR!!EMPLOYEE_ID!"
141178
)
142179
)
143180
rem Skipping registry checks per request
@@ -150,6 +187,7 @@ if "%HAS_ERROR%"=="0" (
150187
echo Setup completed successfully for %EMPLOYEE_ID%.
151188
if defined CHOSEN_DIR echo Files created in: %CHOSEN_DIR%
152189
echo Log file: %LOG_FILE%
190+
call :log_msg "RESULT: SUCCESS"
153191
) else (
154192
color 0C
155193
echo RESULT: COMPLETED WITH ISSUES
@@ -161,12 +199,13 @@ if "%HAS_ERROR%"=="0" (
161199
echo - Take a screenshot of this window.
162200
echo - Attach the log file from: %LOG_FILE%
163201
echo - Share both with your CompAI support contact.
202+
call :log_msg "RESULT: COMPLETED WITH ISSUES (exit=%EXIT_CODE%)"
164203
)
165204
echo ------------------------------------------------------------
166205
echo.
167206
echo Press any key to close this window. This will not affect installation.
168207
pause
169208
if "%HAS_ERROR%"=="0" (exit /b 0) else (exit /b %EXIT_CODE%)`;
170209

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

0 commit comments

Comments
 (0)