Skip to content

Commit ec94486

Browse files
authored
Refactor DeployWorkstation.bat for better execution flow
Updated the script to improve error handling and parameter passing.
1 parent a6f768e commit ec94486

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

DeployWorkstation.bat

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22
REM ========================================================
33
REM DeployWorkstation.bat
44
REM Launcher for DeployWorkstation.ps1
5-
REM Version 5.1 – PNWC Edition (fixed)
5+
REM Version 5.1 – PNWC Edition
66
REM ========================================================
77

8-
setlocal EnableExtensions EnableDelayedExpansion
8+
setlocal enabledelayedexpansion
99

1010
echo.
1111
echo ===== DeployWorkstation Launcher v5.1 =====
1212
echo.
1313

1414
REM --------------------------------------------------------
1515
REM 1) Elevation check
16+
REM Re-launch this .bat elevated if not already admin.
1617
REM --------------------------------------------------------
1718
net session >nul 2>&1
18-
if errorlevel 1 (
19+
if %errorlevel% neq 0 (
1920
echo Requesting administrative privileges...
2021
echo Please click "Yes" in the UAC prompt.
2122
echo.
22-
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
23-
"Start-Process -FilePath '%~f0' -Verb RunAs"
23+
powershell.exe -NoProfile -Command ^
24+
"Start-Process -FilePath '%~f0' -Verb RunAs -Wait"
2425
exit /b
2526
)
2627

@@ -31,15 +32,11 @@ REM --------------------------------------------------------
3132
REM 2) Change to the directory containing this .bat
3233
REM --------------------------------------------------------
3334
pushd "%~dp0"
34-
if errorlevel 1 (
35-
echo [ERROR] Failed to access script folder.
36-
goto :error_exit
37-
)
3835

3936
REM --------------------------------------------------------
4037
REM 3) Verify the PowerShell script is present
4138
REM --------------------------------------------------------
42-
if not exist "%~dp0DeployWorkstation.ps1" (
39+
if not exist "DeployWorkstation.ps1" (
4340
echo [ERROR] DeployWorkstation.ps1 not found.
4441
echo Expected: %~dp0DeployWorkstation.ps1
4542
echo.
@@ -59,7 +56,7 @@ echo 4. System configuration only
5956
echo 5. Exit
6057
echo.
6158
set "choice="
62-
set /p "choice=Enter choice (1-5): "
59+
set /p choice="Enter choice (1-5): "
6360

6461
set "ps_params="
6562

@@ -79,7 +76,7 @@ if "%choice%"=="1" (
7976
echo [*] System configuration only.
8077
set "ps_params=-SkipBloatwareRemoval -SkipAppInstall"
8178
) else if "%choice%"=="5" (
82-
set "ps_exit=0"
79+
echo Exiting.
8380
goto :normal_exit
8481
) else (
8582
echo [!] Invalid choice - please try again.
@@ -90,18 +87,22 @@ if "%choice%"=="1" (
9087
REM --------------------------------------------------------
9188
REM 5) Show what will run, then launch
9289
REM --------------------------------------------------------
93-
if defined ps_params (
94-
echo Parameters : !ps_params!
95-
) else (
90+
if "!ps_params!"=="" (
9691
echo Parameters : (none - full run)
92+
) else (
93+
echo Parameters : !ps_params!
9794
)
9895
echo.
9996
echo Starting Windows PowerShell 5.1...
10097
echo.
10198

102-
powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%~dp0DeployWorkstation.ps1" !ps_params!
99+
if "!ps_params!"=="" (
100+
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "DeployWorkstation.ps1"
101+
) else (
102+
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "DeployWorkstation.ps1" !ps_params!
103+
)
103104

104-
REM Capture exit code immediately
105+
REM Capture exit code immediately before anything can overwrite it
105106
set "ps_exit=%errorlevel%"
106107

107108
REM --------------------------------------------------------
@@ -122,12 +123,14 @@ REM --------------------------------------------------------
122123
:error_exit
123124
echo.
124125
echo ===== Launch aborted =====
125-
set "ps_exit=1"
126+
popd
127+
pause
128+
exit /b 1
126129

127130
REM --------------------------------------------------------
128131
:normal_exit
129132
popd
130133
echo.
131134
echo Press any key to close...
132135
pause >nul
133-
exit /b %ps_exit%
136+
exit /b 0

0 commit comments

Comments
 (0)