以下为Main.cmd脚本内容
@ECHO off
@REM This script is called by x:\Windows\System32\Startnet.cmd - WinPE
@ECHO.
@ECHO.
@ECHO.
@ECHO Running Intel Optimization powercfg for improved imaging times
@ECHO.
@ECHO.
@REM *****************************************************************
@REM List all the disks, then search the volumes of each one to see
@REM which disks may have "Winpe" in the label. We will ignore that
@REM disk and use the first disk without the Winpe label that we find.
@REM *****************************************************************
@REM create diskpart script for list disk
@ECHO.>x:\listdisk.txt
@ECHO list disk>>x:\listdisk.txt
@ECHO exit>>x:\listdisk.txt
@ECHO x:\listdisk.txt
@ECHO call diskpart /s x:\listdisk.txt
CALL diskpart /s x:\listdisk.txt>x:\Disks.txt
@ECHO.
@REM Set Disk equal to blank to start
SET disk=
@ECHO.
@REM
@REM This line will write a diskpart script to select the disk and detail it's contents.
@REM We use this output to find a disk that does NOT have Winpe as the volume label.
@REM That disk will have the OS installed to it. This way you can exclude USB from diskpart.
@REM
@REM This works around the issue where the USB key is sometimes enumerated as disk 0 and the
@REM SSD is disk 1.
@REM 依次取磁盘0,1,2,。。。,1.如果盘符不是WINPE设置disk的值;2.如果不是WINPE又没有磁盘,设置disk=0,后面又要讨论disk 0是否为U盘,为U盘error
FOR /f "skip=8 tokens=2" %%A IN (x:\Disks.txt) DO (
@ECHO sel disk %%A>x:\Par.txt
@ECHO det disk>>x:\Par.txt
@ECHO exit>>x:\Par.txt
IF NOT "%%A"=="DiskPart..." CALL :LISTPAR %%A
)
IF "%Disk%"=="" ECHO "No disk found without a volume label of WINPE" && SET disk=0
@rem IF "%Disk%"=="" ECHO "No disk found without a volume label of WINPE" && GOTO :ERROR
@ECHO Using Disk "%Disk%"
@ECHO.
@rem if disk is removable, case 1 no disk,case 2 the removable device name is not winPE
@ECHO sel disk %Disk% >x:\Par_1.txt
@ECHO det disk>>x:\Par_1.txt
@ECHO exit>>x:\Par_1.txt
CALL DISKPART /S X:\Par_1.txt>X:\ParOutput_1.txt
FOR /f "skip=26 tokens=6" %%A IN (x:\ParOutput_1.txt) DO (
IF /i "%%A"=="removable" ECHO Disk "%Disk%" is removable device GOTO :ERROR && GOTO :ERROR
)
@ECHO.
@ECHO.
@REM *****************************************************************
@REM Detect if System is booted into UEFI mode or Bios mode
@REM *****************************************************************
FOR /f "tokens=2* delims= " %%A IN ('reg query HKLM\System\CurrentControlSet\Control /v PEFirmwareType') DO SET Firmware=%%B
@ECHO.
IF %Firmware%==0x1 GOTO :BIOSMODE
IF %Firmware%==0x2 GOTO :UEFIMODE
GOTO :END
:UEFIMODE
@ECHO.
@REM ************************
@REM UEFI SECTION
@REM ************************
@ECHO The PC is booted in UEFI mode
@ECHO.>x:\winpart.txt
@ECHO select disk "%Disk%">>x:\winpart.txt
@ECHO clean>>x:\winpart.txt
@ECHO convert gpt>>x:\winpart.txt
@ECHO create partition primary size=300>>x:\winpart.txt
@ECHO format quick fs=ntfs label="Windows RE tools">>x:\winpart.txt
@ECHO assign letter="T">>x:\winpart.txt
@ECHO set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac">>x:\winpart.txt
@ECHO gpt attributes=0x8000000000000001>>x:\winpart.txt
@ECHO create partition efi size=100>>x:\winpart.txt
@ECHO format quick fs=fat32 label="System">>x:\winpart.txt
@ECHO assign letter="S">>x:\winpart.txt
rem @ECHO create partition msr size=128>>x:\winpart.txt
@ECHO create partition primary>>x:\winpart.txt
rem @ECHO shrink minimum=15000>>x:\winpart.txt
@ECHO format quick fs=ntfs label="Windows">>x:\winpart.txt
@ECHO assign letter="C">>x:\winpart.txt
rem @ECHO create partition primary>>x:\winpart.txt
rem @ECHO format quick fs=ntfs label="Recovery image">>x:\winpart.txt
rem @ECHO assign letter="R">>x:\winpart.txt
rem @ECHO set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac">>x:\winpart.txt
rem @ECHO gpt attributes=0x8000000000000001>>x:\winpart.txt
@ECHO list partition>>x:\winpart.txt
@ECHO list volume>>x:\winpart.txt
@ECHO exit>>x:\winpart.txt
@ECHO.
@REM *****************************************************************
@REM Map drive letter for Install.wim
@REM *****************************************************************
@ECHO.>x:\ListVol.txt
@ECHO List volume>>x:\ListVol.txt
@ECHO exit>>x:\ListVol.txt
@ECHO call diskpart /s x:\ListVol.txt
CALL DISKPART /s x:\ListVol.txt>x:\Output.txt
@ECHO.
@ECHO.
@REM Go through each drive letter, looking for the \Scripts\Main.cmd file
FOR /f "skip=8 tokens=3" %%A IN (x:\Output.txt) DO (
IF EXIST %%A:\Scripts\Main.cmd SET InstallPath=%%A&& ECHO "Found device with images folder and Main.cmd file"
)
rem == Apply the image to the Windows partition ==
dism /Apply-Image /ImageFile:"%InstallPath%:\Scripts\install_win8_1_x64_UEFI_CN.wim" /Index:1 /ApplyDir:C:\
rem == Copy boot files to the System partition ==
C:\Windows\System32\bcdboot C:\Windows
rem @ECHO copy %InstallPath%:\Scripts\ApplyImage-UEFI.bat %cd%
rem copy %InstallPath%:\Scripts\ApplyImage-UEFI.bat %cd%
rem @ECHO ApplyImage-UEFI.bat %InstallPath%:\Scripts\install.wim
rem ApplyImage-UEFI.bat %InstallPath%:\Scripts\install.wim
GOTO :WINPARTSECTION
:BIOSMODE
@REM ************************
@REM BIOS SECTION
@REM ************************
@ECHO The PC is booted in BIOS mode
@ECHO The PC is booted in BIOS mode>x:\Bios.txt
@ECHO.>x:\winpart.txt
@ECHO select disk "%Disk%">>x:\winpart.txt
@ECHO clean>>x:\winpart.txt
@ECHO create partition primary size=100>>x:\winpart.txt
@ECHO active>>x:\winpart.txt
@ECHO format quick fs=fat32 label="System">>x:\winpart.txt
@ECHO assign letter="S">>x:\winpart.txt
@ECHO create partition primary>>x:\winpart.txt
@ECHO format quick fs=ntfs label="Windows">>x:\winpart.txt
@ECHO assign letter="C">>x:\winpart.txt
rem @ECHO shrink desired=15000>>x:\winpart.txt
rem @ECHO create partition primary>>x:\winpart.txt
rem @ECHO format quick fs=ntfs label="Recovery image">>x:\winpart.txt
rem @ECHO assign letter="R">>x:\winpart.txt
rem @ECHO set id=27>>x:\winpart.txt
@ECHO list partition>>x:\winpart.txt
@ECHO list volume>>x:\winpart.txt
@ECHO exit>>x:\winpart.txt
@ECHO.
@REM *****************************************************************
@REM Map drive letter for Install.wim
@REM *****************************************************************
@ECHO.>x:\ListVol.txt
@ECHO List volume>>x:\ListVol.txt
@ECHO exit>>x:\ListVol.txt
@ECHO call diskpart /s x:\ListVol.txt
CALL DISKPART /s x:\ListVol.txt>x:\Output.txt
@ECHO.
@ECHO.
@REM Go through each drive letter, looking for the \Scripts\Main.cmd file
FOR /f "skip=8 tokens=3" %%A IN (x:\Output.txt) DO (
IF EXIST %%A:\Scripts\Main.cmd SET InstallPath=%%A&& ECHO "Found device with images folder and Main.cmd file"
)
rem == Apply the image to the Windows partition ==
dism /Apply-Image /ImageFile:"%InstallPath%:\Scripts\install_win7_x64_legacy_CN.wim" /Index:1 /ApplyDir:C:\
rem == Copy boot files to the System partition ==
C:\Windows\System32\bcdboot C:\Windows
rem @ECHO copy %InstallPath%:\Scripts\ApplyImage-BIOS.bat %cd%
rem copy %InstallPath%:\Scripts\ApplyImage-BIOS.bat %cd%
rem @ECHO ApplyImage-BIOS.bat %InstallPath%:\Scripts\install.wim
rem ApplyImage-BIOS.bat %InstallPath%:\Scripts\install.wim
GOTO :WINPARTSECTION
:WINPARTSECTION
@ECHO *****************************************************************
@ECHO Image deployment COMPLETE. Type EXIT from Command
@ECHO *****************************************************************
@ECHO.
GOTO :REBOOT
:LISTPAR
@REM This section is to bring a disk number and use it to List Partitions
@ECHO.
SET TempDisk=%1
DISKPART /s x:\Par.txt>x:\ParOutput.txt
FOR /f "skip=26 tokens=4" %%A IN (x:\ParOutput.txt) DO (
IF /i NOT "%%A"=="Winpe" SET Disk=%TempDisk%
)
GOTO :EOF
:REBOOT
@REM
@REM Get finish time so we can evaluate total WinPE phase time.
@REM
CALL |time>x:\StopTime.txt
@ECHO.
@ECHO Deployment complete, system restart.
pause
WPEUTIL REBOOT
@ECHO.
:ERROR
@ECHO.
@ECHO An error has been detected.
COLOR 4F
@ECHO.
GOTO :END
:END
@REM
@REM Get finish time so we can evaluate total WinPE phase time.
@REM
CALL |time>x:\StopTime.txt
@ECHO.
FOR /f "tokens=5" %%A IN (x:\starttime.txt) DO ECHO Script Start Time is %%A
FOR /f "tokens=5" %%A IN (x:\stoptime.txt) DO ECHO Script Stop Time is %%A
@ECHO.
main.cmd结束
startnet.cmd脚本内容如下
@ECHO off
@REM
@REM Initialize WinPE
@ECHO wpeinit
WPEINIT
@ECHO.
@ECHO *****************************************************************
@ECHO call wpeutil UpdateBootInfo
WPEUTIL UpdateBootInfo
@ECHO.
SET WinPEREG="HKLM\System\CurrentControlSet\Control"
SET WinPEKey=PEBootRamdiskSourceDrive
SET WinPESource=
@ECHO.
@REM Get volume letter of USB Key
@ECHO for /f "skip=2 tokens=3" %%A in ('call Reg query %WinPEREG% /v %WinPEKEY%') do set WinPESource=%%A
FOR /f "skip=2 tokens=3" %%A IN ('call Reg query %WinPEREG% /v %WinPEKEY%') DO SET WinPESource=%%A
@ECHO WinPESource is drive letter "%WinPESource%"
@ECHO.
@ECHO.
IF "%WinPESource%"=="" ECHO Drive letter NOT found.&& CALL :FINDDRIVE
@ECHO.
@ECHO.
ECHO WinPESource is "%WinPESource%"
@ECHO.
@ECHO *****************************************************************
@ECHO Call and run main.cmd on the USB Key
@ECHO *****************************************************************
@ECHO call %WinPESource%Scripts\main.cmd
CALL %WinPESource%Scripts\main.cmd
@ECHO.
GOTO :END
:ERROR
@ECHO.
@ECHO An error has been detected.
COLOR 4F
@ECHO.
GOTO :END
:FINDDRIVE
@ECHO.
@ECHO Trying to find drive letter using diskpart
@ECHO.
@ECHO Lis Vol>x:\FindVol.txt
@ECHO.
@ECHO Running Diskpart to get Volume letters
@ECHO.
DISKPART /s x:\FindVol.txt>x:\VolumeList.txt
@ECHO.
@ECHO Parsing list to find WinPE
@ECHO.
FOR /f "skip=8 tokens=3-4" %%A IN (x:\VolumeList.txt) DO (
@ECHO Checking drive letter %%A has volume label of %%B
IF /i "%%B"=="WINPE" SET WinPESource=%%A
)
SET WinPESource=%WinPESource%:\
GOTO :EOF