-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateBootStick.bat
349 lines (259 loc) · 6.85 KB
/
createBootStick.bat
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
@echo off
setlocal enabledelayedexpansion
:: https://learn.microsoft.com/en-us/windows-server-essentials/install/create-a-bootable-usb-flash-drive
set my_name=%~n0%~x0
set my_dir="%~dp0"
set "my_dir=%my_dir:~1,-2%"
set /a MODE_NONE=0
set /a MODE_MOUNT=1
set /a MODE_EXTRACT=2
set /a MODE_SOURCE=3
set /a MODE_MAX=4
set usbLetter=X
set isoLetter=
set inFile=
set "extractedOut=%tmp%\%my_name%-extractedIso"
set format=fat32
set /a mode=%MODE_MOUNT%
set dp_file="%tmp%\%my_name%-diskpart.txt"
set /a PTT_GPT=1
set /a PTT_MBR=2
set /a ptt=%PTT_GPT%
set /a verbose=0
set /a isMounted=0
if [%1]==[] goto usage
GOTO :ParseParams
:ParseParams
REM IF "%~1"=="" GOTO Main
if [%1]==[/?] goto help
if [%1]==[/h] goto help
if [%1]==[/help] goto help
IF "%~1"=="/ul" (
SET usbLetter=%2
SHIFT
goto reParseParams
)
IF "%~1"=="/f" (
SET format=%~2
SHIFT
goto reParseParams
)
IF "%~1"=="/if" (
SET "inFile=%~f2"
SHIFT
goto reParseParams
)
IF "%~1"=="/mount" (
SET mode=%MODE_MOUNT%
goto reParseParams
)
IF "%~1"=="/extract" (
SET mode=%MODE_EXTRACT%
goto reParseParams
)
IF "%~1"=="/source" (
SET mode=%MODE_SOURCE%
goto reParseParams
)
IF /i "%~1"=="/mbr" (
SET /a ptt=%PTT_MBR%
goto reParseParams
)
IF "%~1"=="/v" (
SET /a verbose=1
goto reParseParams
)
IF "%~1"=="/h" (
goto help
)
:reParseParams
SHIFT
if [%1]==[] goto main
GOTO :ParseParams
:main
if [%usbLetter%]==[""] goto usage
if [%usbLetter%]==[] goto usage
if ["%inFile%"]==[""] goto usage
if ["%inFile%"]==[] goto usage
if %verbose% EQU 1 (
echo usbLetter=%usbLetter%
echo isoLetter=%isoLetter%
echo format=%format%
echo mode=%mode%
echo inFile=%inFile%
)
call :checkPermissions
if NOT %errorlevel% EQU 0 (
echo [e] Admin privileges required!
exit /b -1
)
call :listDisks
SET /P diskNumber="[?] Type disk number of your USB stick: "
IF /I [!diskNumber!] EQU [] (
echo [e] No Disk number given!
goto mainEnd
)
call :VPrint "selected disk : %diskNumber%"
call :formatUsb %diskNumber% %format% %usbLetter%
if %mode% EQU %MODE_MOUNT% (
if NOT EXIST "%inFile%" (
echo [e] "%inFile%" not found!
goto usage
)
call :mount "%inFile%"
if !errorlevel! NEQ 0 (
goto mainEnd
)
set /a isMounted=1
SET /P isoLetter="[?] Type mounted iso drive letter: "
IF /I [!isoLetter!] EQU [] (
echo [e] No iso drive letter given!
goto mainEnd
)
set extractedOut=!isoLetter!:\
) else (
if %mode% EQU %MODE_EXTRACT% (
if NOT EXIST "%inFile%" (
echo [e] "%inFile%" not found!
goto usage
)
call :extractFiles "%inFile%" "%extractedOut%"
) else (
if %mode% EQU %MODE_SOURCE% (
call :isDir %inFile%
if [!errorlevel!] == [0] (
echo [e] %inFile% is not a directory!
goto usage
)
set "extractedOut=%inFile%"
)
)
)
call :VPrint "extractedOut: %extractedOut%"
call :copyFiles "%extractedOut%" %usbLetter%
:mainEnd
call :clean
endlocal
echo exiting with error level %errorlevel%
exit /B %errorlevel%
:checkPermissions
call :VPrint "checking Admin permissions..."
net session >nul 2>&1
exit /B %errorlevel%
:listDisks
setlocal
echo list disk > %dp_file%
echo Exit >> %dp_file%
if %verbose% EQU 1 (
echo the script:
type %dp_file%
)
diskpart /s %dp_file%
ENDLOCAL
exit /B %errorlevel%
:formatUsb
setlocal
set diskNumber=%1
set fmt=%2
set letter=%3
call :VPrint "formating usb drive"
echo select disk %diskNumber% > %dp_file%
echo clean >> %dp_file%
echo create partition primary >> %dp_file%
echo select partition 1 >> %dp_file%
echo active >> %dp_file%
echo format FS=%fmt% quick >> %dp_file%
echo assign letter=%letter% >> %dp_file%
echo Exit >> %dp_file%
REM select partition type
if %ptt% EQU PTT_MBR (
echo convert mbr >> %dp_file%
) else (
echo convert gpt >> %dp_file%
)
if %verbose% EQU 1 (
echo the script:
type %dp_file%
)
diskpart /s %dp_file%
ENDLOCAL
exit /B %errorlevel%
:mount
setlocal
set "imagePath=%~1"
call :VPrint "mounting %imagePath%"
powershell Mount-DiskImage -ImagePath "%imagePath%"
ENDLOCAL
exit /B %errorlevel%
:unmount
setlocal
set "imagePath=%~1"
if %isMounted% NEQ 1 exit /b 0
call :VPrint "unmounting %imagePath%"
powershell Dismount-DiskImage -ImagePath "%imagePath%"
ENDLOCAL
exit /B %errorlevel%
:extractFiles
setlocal
set archive=%1
set outPath=%2
call :VPrint "extracting %archive% to %outPath%"
7z x %archive% -o%outPath%
ENDLOCAL
exit /B %errorlevel%
:copyFiles
setlocal
set "source=%~1"
set "target=%~2"
call :VPrint "copying %source% to %target%"
xcopy "%source%\*.*" "%target%:\" /E /F /H
ENDLOCAL
exit /B %errorlevel%
:clean
setlocal
call :VPrint "cleaning up"
if %mode% EQU %MODE_MOUNT% (
call :unmount "%inFile%"
) else (
if %mode% EQU %MODE_EXTRACT% (
rmdir /s /q "%extractedOut%"
))
del %dp_file%
ENDLOCAL
exit /B %errorlevel%
:VPrint
if %verbose% EQU 1 echo %~1
exit /B %errorlevel%
:isDir
setlocal
set v=%~1
if [%v:~0,-1%\] == [%v%] (
if exist %v% (
endlocal
exit /b 1
)
) else (
if exist %v%\ (
endlocal
exit /b 1
)
)
endlocal
exit /b 0
:usage
echo Usage: %prog_name% /if ^<path^> [/mount^|/extract^|/source] [/ul X] [/f ^<format^>] [/mbr] [/v]
exit /B %errorlevel%
:help
call :usage
echo.
echo Options:
echo /if Path to input iso file or folder.
echo /mount Mounting mode: Mounts the iso file and copies its contents over to the usb drive.
echo /extract Extracting mode: Uses 7z to extract the contents of the iso file and copies them over to the usb drive. (7z has to be installed and available in the PATH/current environment.)
echo /source Source mode: Uses a path to an already extracted iso to copy over to the usb drive.
echo /ul Desired drive letter of boot usb stick. Default: X.
echo /f File format of the stick. Default: Fat32.
echo /mbr Creates mbr (legacy) stick.
echo.
echo /v More verbose mode.
exit /B %errorlevel%