-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcvt2vhd.cmd
274 lines (253 loc) · 10.4 KB
/
cvt2vhd.cmd
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
@echo off
REM
REM invoke this cvt2vhd <letter of drive we're imaging:> <letter of drive we're using to save WIM and VHD:> <size of VHD in gigabytes>
REM if you add "skipwim," it'll skip imaging (which takes time)
REM
REM exdrive = external drive letter we'll write the wim and then vhd to (should include colon)
REM imgdrive = local drive with Windows folder on it that we'll be imaging (does not require sysprep, that's up to you, but should include colon)
REM vhdsize = size of the image.vhd file (an expandable file) in megabytes... we'll add 000 to make it gigabytes
REM tdrive = temporary drive letter to use when creating and attaching the VHD (should include colon)
set exdrive=%2
set imgdrive=%1
set vhdsize=%3
set skipwim=false
if a%4==askipwim set skipwim=true
if a%exdrive%%imgdrive%%vhdsize%==a (goto :background)
REM
REM Find an available drive letter for a temporary drive
REM
set tdrive=none
if not exist w:\ set tdrive=w
if not exist V:\ set tdrive=v
if not exist U:\ set tdrive=u
if not exist T:\ set tdrive=t
if not exist S:\ set tdrive=s
if not exist R:\ set tdrive=r
if not exist Q:\ set tdrive=q
if not %tdrive%==none goto :foundtdrive
echo.
echo Error: I need a temporary drive letter, but could
echo not find one between Q: and W:. I can't do the job
echo without a free drive letter, so I've got to stop.
echo.
goto :end
:foundtdrive
REM
REM check for inputs on everything
REM
if a%exdrive%==a (goto :needinputs)
if a%imgdrive%==a (goto :needinputs)
if a%vhdsize%==a (goto :needinputs)
REM
REM Check the drives exist
REM
if not exist %exdrive%\ ((echo.) & (echo Drive %exdrive% seems not to exist.) & (goto :end))
if not exist %imgdrive%\ ((echo.) & (echo Drive %imgdrive% seems not to exist.) & (goto :end))
if exist %exdrive%\image.vhd ((echo.)&(echo %exdrive%\image.vhd already exists.) & (goto :end))
REM if not exist \windows\system32\imagex.exe ((echo.)&(echo ImageX missing... please only run this from a system booted from & echo a Steadier State USB stick/CD.) & (goto :end))
REM
CLS
echo.
echo Convert PC to VHD Routine
echo -------------------------
echo.
echo This command file takes the operating system drive of this
echo computer and re-images it as a file named image.vhd.
echo To run this correctly, you should have booted the computer
echo that you want to convert to VHD to WinPE using the USB
echo stick that you created with buildpe, as part of Steadier State.
echo If that's not the case, just press ctrl-C and stop this.
echo.
echo Before going further, you should have the PC connected to
echo a drive -- networked, external, or whatever -- with enough
echo space to hold your Windows box C: drive on it.
echo.
echo You've said that:
echo the OS you want convert to VHD is %imgdrive%.
echo the drive you want to create the image/VHD on is %exdrive%.
echo you want the resulting "image.vhd" file to be %vhdsize% gigabytes.
echo Additionally, I will use drive %tdrive%: as a temporary drive letter.
echo.
REM
REM Ready to get to work
REM
if a%skipwim%==atrue goto :makevhd
echo =========================================
echo Step 1: Capture Windows drive as an image
echo =========================================
echo Imaging %imgdrive% to %exdrive%, command is:
echo imagex /capture %imgdrive% %exdrive%\image.wim "Intermediate image" /verify
imagex /capture %imgdrive% %exdrive%\image.wim "Intermediate image" /verify
set imagexrc=%errorlevel%
if not %imagexrc%==0 ((echo ImageX failed with error code %imagexrc%, exiting.) & (goto :eof))
echo.
echo Step 1 successful.
echo.
:makevhd
echo ======================================================
echo Step 2: create an empty VHD file to receive the image.
echo ======================================================
echo.
echo create vdisk file="%exdrive%\image.vhd" type=expandable maximum=%vhdsize%000 >dps1.txt
echo select vdisk file="%exdrive%\image.vhd" >>dps1.txt
echo attach vdisk >>dps1.txt
echo cre par pri >>dps1.txt
echo format fs=ntfs quick label="Windows" >>dps1.txt
echo assign letter=%tdrive% >>dps1.txt
echo exit >>dps1.txt
rem
echo We'll issue these commands to DISKPART:
echo.
type dps1.txt
diskpart /s dps1.txt
set dprc=%errorlevel%
if %dprc%==0 ((echo Diskpart successful.) & (goto :fillvhd))
echo.
echo Diskpart failure. Return code=%dprc%. CVT2VHD unable to finish, exiting.
goto :eof
:fillvhd
echo.
echo =============================
echo Step 3: Apply image to VHD
echo =============================
echo.
rem
rem now to VHD
rem
echo we'll run this command:
echo imagex /apply %exdrive%\image.wim 1 %tdrive%: /verify
imagex /apply %exdrive%\image.wim 1 %tdrive%: /verify
set imagexrc=%errorlevel%
if not %imagexrc%==0 ((echo ImageX failed with error code %imagexrc%, exiting.) & (goto :eof))
echo.
echo ImageX /apply succeeded.
:freevhd
echo.
echo ====================================
echo Step 4: Detach and close VHD file
echo ====================================
rem
rem now detach image.vhd and free up the temp drive letter
rem
echo Finally, unmount the VHD:
echo select vdisk file="%exdrive%\image.vhd" >dps1.txt
echo detach vdisk >>dps1.txt
echo exit >>dps1.txt
echo.
echo We'll execute these DISKPART commands:
echo dps1.txt:
echo.
type dps1.txt
diskpart /s dps1.txt
echo.
echo Done.
set dprc=%errorlevel%
if %dprc%==0 ((echo Diskpart successful.) & (goto :happyend))
echo.
echo Diskpart failure detaching VHD. Return code=%dprc%.
echo Check there's enough space on drive %imgdrive% and try again.
echo CVT2VHD unable to finish, exiting.
goto :eof
:happyend
rem
rem and now we're done, it's in image.vhd
rem
Echo Completed creating your image.vhd. To deploy this to a PC, you should:
echo.
echo 1) Boot the target PC with your Steadier State USB stick/CD.
echo 2) Run prepnewpc on the target PC (which will wipe the hard drive of that
echo system, so if there's anything you care about on that target PC, back
echo it up first)!
echo 3) Your target PC will now have a large empty C: drive. Copy this image.vhd
echo that you've just created to the root of that C:\ drive.
echo 4) Once image.vhd has copied, remove the USB stick/CD and reboot the PC from
echo its hard drive. When you reboot, Steadier State will determine that your
echo system needs a snapshot file and an OS boot entry, so it creates those
echo things and automatically reboots again. Let it do its work and reboot and
echo your system will soon boot up in Windows 7, but at that point you'll be
echo protected by Steadier State -- whenever you want your system to forget
echo everything done to it since now, just reboot and at Boot Manager, choose
echo "Roll Back Windows."
echo.
echo Thanks for using Steadier State, I hope it's of value.
echo -- Mark Minasi help@minasi.com www.steadierstate.com
goto :eof
:background
echo How and Why To Use CVT2VHD in Steadier State
echo --------------------------------------------
echo.
echo Steadier State lets you create a system to run Windows that
echo will be exactly the same as the last time started it, no
echo matter what was done with Windows the last time it was used.
echo This is accomplished by using "VHD Differencing" technology,
echo and is a perfect fit for schools, libraries, Internet Cafes
echo or any public facing computer that you want to provide the
echo same, consistent user experience for every user.
echo.
echo Steadier State is created by Mark Manasi and later modified
echo by Thomas Freedman to do just that. The changes made by Thomas
echo dramatically reduced the time to return the system to it's
echo "template" state on every reboot without the need to boot a
echo supervisor OS like WinPE. WinPE is still required to do merges
echo to update the template image but is booted separately than the
echo main Windows OS that runs from one of 2 differencing VHD files.
echo The process for this is quite involved but the good news is
echo most of that process is automated with good old DOS batch file
echo scripts, so it's "plain to see" what's going.
echo.
echo This script automates the conversion of a running "model"
echo Windows 7 system into a template image.vhd file Windows will
echo use as it's entire file system.
echo.
echo To get a system ready for conversion, first boot it from
echo your SDRState bootable USB stick or CD. Then, connect
echo the system to some large external drive, whether it's a
echo networked drive mapped to a drive letter or perhaps a large
echo external hard disk -- you'll need that because you're going
echo to take that system's C: drive and rebuild it as one large
echo VHD file. On the USB stick/CD, you'll see a file named
echo cvt2vhd.cmd. Run that file, putting in the drive letter
echo to image (probably C:), the external drive/mapped drive to
echo save the new VHD to (could be anything, I'll use e: in my
echo example), and then the maximum size that the VHD will need
echo to be, in gigabytes (I'll assume 80 GB in my example). For
echo example, you might start it like this:
echo.
echo cvt2vhd c: e: 80
echo.
echo That'll take a while, but when it's done, you'll have a file
echo named image.vhd on your target drive -- again, E: in my
echo example. Once you've got that image.vhd, it's ready to prep
echo a system to get it ready to be able to use that VHD. You can
echo do that by booting the system with your USB stick/CD and
echo then running prepnewpc.
echo.
echo Also, remember that the physical volume that you deploy this
echo image.vhd to should have an amount of free space equal to at
echo least 2.5 times the image.vhd maximum size -- 200 GB in this
echo case. (80 GB x 2.5 = 200 GB.)
echo.
echo Thanks for using Steadier State, I hope it's of value.
echo -- Mark Minasi help@minasi.com www.steadierstate.com
goto :end
:needinputs
echo.
echo This script needs three inputs: the drive letter you want
echo to image, like C:, the drive letter with sufficient space
echo to let you create the image, and the size of the VHD
echo file you want, in gigabytes. So, for example, if you typed
echo.
echo cvt2vhd e: g: 80
echo.
echo Then cvt2vhd would image drive E: as image.vhd onto drive G:,
echo creating an image.vhd file that can expand up to 80 GB max.
echo.
echo NOTE: the physical hard disk that you deploy the resulting
echo image.vhd onto should be at least 2.5 times the VHD file size
echo that you specified, so, for example, in the above case the
echo image.vhd file you've capped at 80 GB should be deployed to a
echo physical volume that has at least 2.5x80 or 200 GB free.
echo.
echo Thanks for using Steadier State, I hope it's useful.
echo -- Mark Minasi help@minasi.com www.steadierstate.com
:end