summaryrefslogtreecommitdiff
path: root/.github/crashdetect/CrashDetectCreateUsb.cmd
blob: 571f03936c69d34b3af231ae9a2e7fa5d08319d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
@echo off
setlocal

set WINPE_DRIVE=A
set WINUSB_DRIVE=B
set EXITCODE=0

rem Pre-check
if not exist "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\DISM\dism.exe" (
    echo ERROR: Missing DISM tool!
    goto error
)
if not exist "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\en-us\winpe.wim" (
    echo ERROR: Missing winpe.wim image!
    goto error
)
if not exist "C:\WinPE_USB\Scripts\CrashDetectOsReinstall.cmd" (
    echo ERROR: Missing script C:\WinPE_USB\Scripts\CrashDetectOsReinstall.cmd!
    goto error
)
if not exist "C:\WinPE_USB\Scripts\Unattend.xml" (
    echo ERROR: Missing XML C:\WinPE_USB\Scripts\Unattend.xml!
    goto error
)
if not exist "C:\WinPE_USB\Images\install.wim" (
    echo ERROR: Missing WIM C:\WinPE_USB\Images\install.wim!
    goto error
)

rem Environment setup.
echo Setting up environment for ADK tools...
cd "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools>"
call DandISetEnv.bat
echo Done setting up environment.

rem Ask for the USB disk number displayed in Diskpart.
echo Displaying detected disk drives..
echo Creating DiskPart script...
(
echo list disk
) > C:\WinPE_USB\diskpart.txt
diskpart /s C:\WinPE_USB\diskpart.txt
del C:\WinPE_USB\diskpart.txt
echo(
echo(
echo Note that Disk 0 is often the OS drive you do NOT want to format!
echo(
echo Ensure drive letters %WINPE_DRIVE%: and %WINUSB_DRIVE%: are not currently assigned to other drives!
echo It's ok if it's assigned to the target USB.
echo(
set /p USBDISK=Enter the Disk Number of the USB drive you want to make WinPE bootable: 
echo(
echo Disk %USBDISK% selected.

rem Create a USB drive with WinPE and data partitions.
echo(
echo WARNING!!!: VERIFY DISK %USBDISK% IS THE CORRECT USB DISK TO FORMAT!
choice /C YN /M "ARE YOU SURE YOU WANT TO FORMAT DISK %USBDISK%?"
echo(
if %errorlevel% equ 2 (
    echo You chose NO, exiting without modifying USB key.
    exit /b 0
)
if %errorlevel% equ 1 (
    echo You chose YES, proceeding with formatting USB key.
)

:dism
rem Update startnet.cmd autorun script in WinPE boot image (winpe.wim).
cd /d "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64"
echo(
if not exist "C:\WinPE_USB\WinPE_amd64\mount" (
    echo Creating temp folders C:\WinPE_USB\WinPE_amd64\mount...
    mkdir "C:\WinPE_USB\WinPE_amd64\mount"
)
echo Mounting WinPE image to modify...
Dism /Mount-Image /ImageFile:"en-us\winpe.wim" /index:1 /MountDir:"C:\WinPE_USB\WinPE_amd64\mount"
if errorlevel 1 (
    echo ERROR: DISM failed to mount ImageFile:"en-us\winpe.wim"!
    goto error
)
echo Done mounting winpe.wim image.
echo Updating startnet.cmd autorun script in WinPE boot image...
(
echo wpeinit
echo.
echo @echo off
echo rem Find USB drive.
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 ^(
echo     if exist %%%%D:Scripts\CrashDetectOsReinstall.cmd ^(
echo         call %%%%D:Scripts\CrashDetectOsReinstall.cmd
echo         goto EOF
echo     ^)
echo ^)
) > "C:\WinPE_USB\WinPE_amd64\mount\Windows\System32\startnet.cmd"
echo Done adding CrashDetectOsReinstall.cmd to startnet.cmd.
echo Unmounting WinPE image with committed changes...
Dism /Unmount-Image /MountDir:"C:\WinPE_USB\WinPE_amd64\mount" /commit
if errorlevel 1 (
    echo ERROR: DISM failed to unmount ImageFile:"en-us\winpe.wim"!
    echo USB drive have not been touched yet.
    goto error
)
echo Done unmounting winpe.wim image.

rem Delete temp "WinPE_amd64" folder, else the "copype.cmd" below will fail if the folder is already present.
rmdir /s /q "C:\WinPE_USB\WinPE_amd64"
echo Deleted temp folder "C:\WinPE_USB\WinPE_amd64".
echo(
echo Wiping out USB and creating 2 partitions...
echo Creating DiskPart script...
(
echo select disk %USBDISK%
echo clean
echo create partition primary size=2048
echo active
echo format fs=FAT32 quick label="WinPE"
echo assign letter=%WINPE_DRIVE%
echo create partition primary
echo format fs=NTFS quick label="WinUSB"
echo assign letter=%WINUSB_DRIVE%
) > C:\WinPE_USB\diskpart.txt
echo Partitioning USB...
diskpart /s C:\WinPE_USB\diskpart.txt
if errorlevel 1 (
    echo ERROR: DiskPart failed during partitioning and formatting USB!
    del C:\WinPE_USB\diskpart.txt
    goto error
)
del C:\WinPE_USB\diskpart.txt
echo Done partitioning and formatting USB.
echo(
rem The "copype.cmd" script will create working directory "WinPE_amd64", it will fail if directory already exist.
echo Copying WinPE working files to "C:\WinPE_USB\WinPE_amd64"...
call copype.cmd amd64 "C:\WinPE_USB\WinPE_amd64"
if errorlevel 1 (
    echo ERROR: Script "copype.cmd" failed to copy working files!
    goto error
)
echo Done copying WinPE files.
echo(
rem Install WinPE to the USB and make it bootable.
echo Creating bootable WinPE USB...
call Makewinpemedia.cmd /ufd /f "C:\WinPE_USB\WinPE_amd64" "%WINPE_DRIVE%:" /bootex
if errorlevel 1 (
    echo ERROR: Script "Makewinpemedia.cmd" failed to make WinPE USB bootable!
    goto error
)
echo Done, WinPE USB drive is now bootable.
echo(
rem Copy Unattend.xml, scripts and install.wim over to USB
echo Copying Unattend.xml and scripts over to USB...
xcopy "C:\WinPE_USB\Scripts\" "%WINUSB_DRIVE%:\Scripts\" /E /I /R /Y
if errorlevel 1 (
    echo ERROR: Failed copying scripts from "C:\WinPE_USB\Scripts\" to USB!
    goto error
)
echo Done copying script files.
echo(
echo Copying install.wim over to USB...this could take a while...
robocopy "C:\WinPE_USB\Images" "%WINUSB_DRIVE%:\Images" install.wim /ETA /J
if %errorlevel% geq 8 (
    echo ERROR: Failed copying install.wim from "C:\WinPE_USB\Images\" to USB!
    goto error
)
echo Done copying WIM file.
echo(
goto cleanup

:error
set EXITCODE=1
goto cleanup

:cleanup
rem Clean up any temp folders.
if exist "C:\WinPE_USB\WinPE_amd64" (
    rmdir /s /q "C:\WinPE_USB\WinPE_amd64"
    echo Cleaned up temp folder "C:\WinPE_USB\WinPE_amd64".
)

:done
if %EXITCODE% equ 0 (
    echo(
    echo SUCCESSFULLY CREATED BOOTABLE WINPE USB DRIVE.
) else (
    echo(
    echo FAILED CREATING BOOTABLE WINPE USB DRIVE!
)
endlocal & exit /b %EXITCODE%