forked from MicksITBlogs/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateUSB.vbs
189 lines (141 loc) · 5.25 KB
/
CreateUSB.vbs
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
'*******************************************************************************
' Program: CreateUSB.vbs
' Author: Mick Pletcher
' Date: 08 February 2010
' Modified:
'
' Description: This script creates a bootable USB thumb drive from a WIM file.
' WinPE must be installed on the machine and its directory
' specified in constant WinPELoc with no backslash after it. The
' WIM files reside in the WinPELoc directory for which this script
' reads from. A diskpart script must also be present in the WinPELOC
' directory specifing the parameters for the thumb drive.
' 1) Partition and Format the USB Thumb Drive
' 2) Select the WIM file
' 3) Prompt to remove and reinsert the thumb drive
' 4) Copy the OS down to the thumb Drive and prompt when complete
' 5) Cleanup Global Variables
'*******************************************************************************
Option Explicit
REM Define Global Constants
CONST WinPELoc = "c:\winpe"
REM Define Global Variables
DIM strImageName : Set strImageName = Nothing
PartitionDisk()
SelectImage()
CopyWIM()
PromptThumbRemoval()
CopyOS()
Complete()
GlobalVariableCleanup()
'*******************************************************************************
'*******************************************************************************
Sub PartitionDisk()
REM Define Local Objects
DIM oShell : SET oShell = CreateObject("Wscript.Shell")
REM Define Local Variables
DIM PartDisk : PartDisk = "diskpart.exe /s" & Chr(32) & WinPELoc & "\usbpartscript.txt"
oShell.Run PartDisk, 1, True
REM Cleanup Local Variables
Set oShell = Nothing
Set PartDisk = Nothing
End Sub
'*******************************************************************************
Sub SelectImage()
REM Define Local Constants
CONST ForAppending = 2
CONST strComputer = "."
REM Define Local Variables
DIM Count : Count = 1
DIM FileName : Set FileName = Nothing
DIM FileVerify : Set FileVerify = Nothing
DIM objFile : Set objFile = Nothing
DIM strList : strList = "Select an Image File:"
REM Define Objects
DIM objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
DIM objWMIService : Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
DIM colFileList : Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & Chr(39) & WinPELoc & Chr(39) & "} Where " _
& "ResultClass = CIM_DataFile")
REM Get List of WIM files
For Each objFile In colFileList
FileVerify = Right(objFile.Name, 3)
If FileVerify = "wim" then
FileName = Len(objFile.Name)
FileName = FileName - 9
FileName = Right(objFile.Name, FileName)
strList = strList & vbCrLf & Count & " - " & FileName
Count = Count + 1
End If
Set FileVerify = Nothing
Next
REM Select WIM File
strImageName = InputBox(strList, "Image")
strImageName = CInt(strImageName)
REM ReInitialize Variables
Count = 1
Set FileName = Nothing
Set FileVerify = Nothing
Set objFile = Nothing
REM Get File Name
For Each objFile In colFileList
FileVerify = Right(objFile.Name, 3)
If FileVerify = "wim" then
FileName = Len(objFile.Name)
FileName = FileName - 9
FileName = Right(objFile.Name, FileName)
If Count = strImageName then
strImageName = FileName
End If
Count = Count + 1
End If
Set FileVerify = Nothing
Next
REM Cleanup Local Variables
Set colFileList = Nothing
Set Count = Nothing
Set FileName = Nothing
Set FileVerify = Nothing
Set objFile = Nothing
Set objFSO = Nothing
Set objWMIService = Nothing
Set strList = Nothing
End Sub
'*******************************************************************************
Sub CopyWIM()
REM Define Local Objects
DIM oShell : SET oShell = CreateObject("Wscript.Shell")
REM Define Local Variables
DIM CopyFile : CopyFile = "xcopy" & Chr(32) & WinPELoc & "\" & strImageName & Chr(32) & WinPELoc & "\iso\sources\boot.wim /y"
oShell.Run CopyFile, 1, True
REM Cleanup Local Variables
Set CopyFile = Nothing
Set oShell = Nothing
End Sub
'*******************************************************************************
Sub PromptThumbRemoval()
MsgBox("Remove and Re-enter USB Thumb Drive. Press any key to continue")
End Sub
'*******************************************************************************
Sub CopyOS()
REM Define Local Objects
DIM oShell : SET oShell = CreateObject("Wscript.Shell")
REM Define Local Variables
DIM CopyFiles
CopyFiles = "xcopy" & Chr(32) & WinPELoc & "\iso\*.* /s /e /f e:\"
oShell.Run CopyFiles, 1, True
CopyFiles = "xcopy" & Chr(32) & WinPELoc & "\iso\*.* /s /e /f f:\"
oShell.Run CopyFiles, 1, True
REM Cleanup Local Variables
Set oShell = Nothing
Set CopyFiles = Nothing
End Sub
'*******************************************************************************
Sub Complete()
MsgBox("USB Image is Complete")
End Sub
'*******************************************************************************
Sub GlobalVariableCleanup()
Set strImageName = Nothing
End Sub