Skip to content

Commit b0ae265

Browse files
Merge pull request #1395 from dunix2000/main
Crash detection and OS reinstall tool.
2 parents 26741c7 + 9b884c1 commit b0ae265

4 files changed

Lines changed: 767 additions & 0 deletions

File tree

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
@echo off
2+
setlocal
3+
4+
set WINPE_DRIVE=A
5+
set WINUSB_DRIVE=B
6+
set EXITCODE=0
7+
8+
rem Pre-check
9+
if not exist "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\DISM\dism.exe" (
10+
echo ERROR: Missing DISM tool!
11+
goto error
12+
)
13+
if not exist "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\en-us\winpe.wim" (
14+
echo ERROR: Missing winpe.wim image!
15+
goto error
16+
)
17+
if not exist "C:\WinPE_USB\Scripts\CrashDetectOsReinstall.cmd" (
18+
echo ERROR: Missing script C:\WinPE_USB\Scripts\CrashDetectOsReinstall.cmd!
19+
goto error
20+
)
21+
if not exist "C:\WinPE_USB\Scripts\Unattend.xml" (
22+
echo ERROR: Missing XML C:\WinPE_USB\Scripts\Unattend.xml!
23+
goto error
24+
)
25+
if not exist "C:\WinPE_USB\Images\install.wim" (
26+
echo ERROR: Missing WIM C:\WinPE_USB\Images\install.wim!
27+
goto error
28+
)
29+
30+
rem Environment setup.
31+
echo Setting up environment for ADK tools...
32+
cd "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools>"
33+
call DandISetEnv.bat
34+
echo Done setting up environment.
35+
36+
rem Ask for the USB disk number displayed in Diskpart.
37+
echo Displaying detected disk drives..
38+
echo Creating DiskPart script...
39+
(
40+
echo list disk
41+
) > C:\WinPE_USB\diskpart.txt
42+
diskpart /s C:\WinPE_USB\diskpart.txt
43+
del C:\WinPE_USB\diskpart.txt
44+
echo(
45+
echo(
46+
echo Note that Disk 0 is often the OS drive you do NOT want to format!
47+
echo(
48+
echo Ensure drive letters %WINPE_DRIVE%: and %WINUSB_DRIVE%: are not currently assigned to other drives!
49+
echo It's ok if it's assigned to the target USB.
50+
echo(
51+
set /p USBDISK=Enter the Disk Number of the USB drive you want to make WinPE bootable:
52+
echo(
53+
echo Disk %USBDISK% selected.
54+
55+
rem Create a USB drive with WinPE and data partitions.
56+
echo(
57+
echo WARNING!!!: VERIFY DISK %USBDISK% IS THE CORRECT USB DISK TO FORMAT!
58+
choice /C YN /M "ARE YOU SURE YOU WANT TO FORMAT DISK %USBDISK%?"
59+
echo(
60+
if %errorlevel% equ 2 (
61+
echo You chose NO, exiting without modifying USB key.
62+
exit /b 0
63+
)
64+
if %errorlevel% equ 1 (
65+
echo You chose YES, proceeding with formatting USB key.
66+
)
67+
68+
:dism
69+
rem Update startnet.cmd autorun script in WinPE boot image (winpe.wim).
70+
cd /d "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64"
71+
echo(
72+
if not exist "C:\WinPE_USB\WinPE_amd64\mount" (
73+
echo Creating temp folders C:\WinPE_USB\WinPE_amd64\mount...
74+
mkdir "C:\WinPE_USB\WinPE_amd64\mount"
75+
)
76+
echo Mounting WinPE image to modify...
77+
Dism /Mount-Image /ImageFile:"en-us\winpe.wim" /index:1 /MountDir:"C:\WinPE_USB\WinPE_amd64\mount"
78+
if errorlevel 1 (
79+
echo ERROR: DISM failed to mount ImageFile:"en-us\winpe.wim"!
80+
goto error
81+
)
82+
echo Done mounting winpe.wim image.
83+
echo Updating startnet.cmd autorun script in WinPE boot image...
84+
(
85+
echo wpeinit
86+
echo.
87+
echo @echo off
88+
echo rem Find USB drive.
89+
echo for %%%%D in (D E F G H I J K L M N O P Q R S T U V W Y Z^) do ^(
90+
echo if exist %%%%D:Scripts\CrashDetectOsReinstall.cmd ^(
91+
echo call %%%%D:Scripts\CrashDetectOsReinstall.cmd
92+
echo goto EOF
93+
echo ^)
94+
echo ^)
95+
) > "C:\WinPE_USB\WinPE_amd64\mount\Windows\System32\startnet.cmd"
96+
echo Done adding CrashDetectOsReinstall.cmd to startnet.cmd.
97+
echo Unmounting WinPE image with committed changes...
98+
Dism /Unmount-Image /MountDir:"C:\WinPE_USB\WinPE_amd64\mount" /commit
99+
if errorlevel 1 (
100+
echo ERROR: DISM failed to unmount ImageFile:"en-us\winpe.wim"!
101+
echo USB drive have not been touched yet.
102+
goto error
103+
)
104+
echo Done unmounting winpe.wim image.
105+
106+
rem Delete temp "WinPE_amd64" folder, else the "copype.cmd" below will fail if the folder is already present.
107+
rmdir /s /q "C:\WinPE_USB\WinPE_amd64"
108+
echo Deleted temp folder "C:\WinPE_USB\WinPE_amd64".
109+
echo(
110+
echo Wiping out USB and creating 2 partitions...
111+
echo Creating DiskPart script...
112+
(
113+
echo select disk %USBDISK%
114+
echo clean
115+
echo create partition primary size=2048
116+
echo active
117+
echo format fs=FAT32 quick label="WinPE"
118+
echo assign letter=%WINPE_DRIVE%
119+
echo create partition primary
120+
echo format fs=NTFS quick label="WinUSB"
121+
echo assign letter=%WINUSB_DRIVE%
122+
) > C:\WinPE_USB\diskpart.txt
123+
echo Partitioning USB...
124+
diskpart /s C:\WinPE_USB\diskpart.txt
125+
if errorlevel 1 (
126+
echo ERROR: DiskPart failed during partitioning and formatting USB!
127+
del C:\WinPE_USB\diskpart.txt
128+
goto error
129+
)
130+
del C:\WinPE_USB\diskpart.txt
131+
echo Done partitioning and formatting USB.
132+
echo(
133+
rem The "copype.cmd" script will create working directory "WinPE_amd64", it will fail if directory already exist.
134+
echo Copying WinPE working files to "C:\WinPE_USB\WinPE_amd64"...
135+
call copype.cmd amd64 "C:\WinPE_USB\WinPE_amd64"
136+
if errorlevel 1 (
137+
echo ERROR: Script "copype.cmd" failed to copy working files!
138+
goto error
139+
)
140+
echo Done copying WinPE files.
141+
echo(
142+
rem Install WinPE to the USB and make it bootable.
143+
echo Creating bootable WinPE USB...
144+
call Makewinpemedia.cmd /ufd /f "C:\WinPE_USB\WinPE_amd64" "%WINPE_DRIVE%:" /bootex
145+
if errorlevel 1 (
146+
echo ERROR: Script "Makewinpemedia.cmd" failed to make WinPE USB bootable!
147+
goto error
148+
)
149+
echo Done, WinPE USB drive is now bootable.
150+
echo(
151+
rem Copy Unattend.xml, scripts and install.wim over to USB
152+
echo Copying Unattend.xml and scripts over to USB...
153+
xcopy "C:\WinPE_USB\Scripts\" "%WINUSB_DRIVE%:\Scripts\" /E /I /R /Y
154+
if errorlevel 1 (
155+
echo ERROR: Failed copying scripts from "C:\WinPE_USB\Scripts\" to USB!
156+
goto error
157+
)
158+
echo Done copying script files.
159+
echo(
160+
echo Copying install.wim over to USB...this could take a while...
161+
robocopy "C:\WinPE_USB\Images" "%WINUSB_DRIVE%:\Images" install.wim /ETA /J
162+
if %errorlevel% geq 8 (
163+
echo ERROR: Failed copying install.wim from "C:\WinPE_USB\Images\" to USB!
164+
goto error
165+
)
166+
echo Done copying WIM file.
167+
echo(
168+
goto cleanup
169+
170+
:error
171+
set EXITCODE=1
172+
goto cleanup
173+
174+
:cleanup
175+
rem Clean up any temp folders.
176+
if exist "C:\WinPE_USB\WinPE_amd64" (
177+
rmdir /s /q "C:\WinPE_USB\WinPE_amd64"
178+
echo Cleaned up temp folder "C:\WinPE_USB\WinPE_amd64".
179+
)
180+
181+
:done
182+
if %EXITCODE% equ 0 (
183+
echo(
184+
echo SUCCESSFULLY CREATED BOOTABLE WINPE USB DRIVE.
185+
) else (
186+
echo(
187+
echo FAILED CREATING BOOTABLE WINPE USB DRIVE!
188+
)
189+
endlocal & exit /b %EXITCODE%

0 commit comments

Comments
 (0)