Need help Dual booting Windows 10 and WinPE via script (updated)

ThoughTComposer

Prominent
Jul 26, 2017
4
0
510
Hi all

I have been trying all week to automate applying an image to my computer via batch script, then setting up a functional dual boot of Windows 10 and a custom WinPE. The purpose is that I have a 20GB recovery partition with my custom .WIM stored on it that I can then load into, format C:, then Re-image my Windows partition with the working version of the image.

I have 5 partitions, though I am not sure all are necessary:

Partition 1: Recovery (generic 300mb recovery partition)
partition 2: System bcd store
Partition 3: which randomly gets generated from Win10
Partition 4: Windows partion
Partition 5: 20GB recovery partition

My script, so far, applies the WIM files, then finds the model and injects the drivers, then it goes to set up the actual dual boot. Everything succeeds, but the bootloader breaks and I don't quite know why. I know there has to be an error in the process/script and I really need some help searching it out.

Script so far:

@ECHO ON
START /WAIT DISKPART.exe /S Win10FranDiskpart.txt
powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
Dism /apply-image /imagefile:"%CD%\ImageBackup\partition1.wim" /index:1 /ApplyDir:T:\
Dism /apply-image /imagefile:"%CD%\ImageBackup\partition2.wim" /index:1 /ApplyDir:S:\
DISM /apply-image /imagefile:"%CD%\ImageBackup\Install.wim" /index:1 /ApplyDir:W:\
DISM /apply-image /imagefile:"%CD%\ImageBackup\partition5.wim" /index:1 /ApplyDir:R:\
for /f "tokens=2 delims==" %%f in ('wmic computersystem get model /value ^| find "="') do set "myVar=%%f"
dism.exe /Image:W:\ /Add-Driver /Driver:"%CD%\Drivers\%myvar%" /Recurse /ForceUnsigned /english /scratchdir:W:\
bcdboot.exe w:\windows /s s: /f all


xcopy D:\EFI\* S:\EFI\* /cherkyfs
xcopy D:\sources\* S:\sources\* /cherkyfs
copy D:\boot\boot.sdi S:\sources\
copy W:\windows\boot\EFI\*.efi S:\EFI\Microsoft\Boot\*
del S:\EFI\Microsoft\Boot\BCD /f

REM Create a new system store
bcdedit -createstore S:\EFI\Microsoft\Boot\BCD

REM Create new entries in the system store for bootmgr, globalsettings, dbgsettings, ramdiskoptions, and Windows PE.
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {bootmgr} /d "Boot Manager"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {globalsettings} /d "globalsettings"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {dbgsettings} /d "debugsettings"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {ramdiskoptions} /d "ramdiskoptions"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create /d "Windows PE" -application osloader>GUID.txt
REM Assign GUID to Variable NEWGUID
For /F "tokens=2 delims={}" %%i in (GUID.txt) do (set newguid=%%i)

REM Set the default entry that the boot manager selects when the timeout expires. Substitute <GUID> with the GUID value for the Windows PE store
bcdedit -store S:\EFI\Microsoft\Boot\BCD /default {%newguid%}

REM Run the following commands to configure bootmgr:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} device partition=s:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} path \EFI\Microsoft\Boot\bootmgfw.efi
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} locale en-us
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} timeout 3

REM Run the following commands to configure the boot loader:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} device ramdisk=[S:]\sources\boot.wim,{ramdiskoptions}
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} path \windows\system32\winload.efi
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} osdevice ramdisk=[S:]\sources\boot.wim,{ramdiskoptions}
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} systemroot \windows
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} winpe yes
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} nx optin
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} detecthal yes
bcdedit -store S:\EFI\Microsoft\Boot\BCD -displayorder {Default} -addfirst

REM Run the following commands to configure dbgsettings:
REM bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {dbgsettings} debugtype Serial
REM bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {dbgsettings} debugport 1
REM bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {dbgsettings} baudrate 115200

REM Run the following commands to configure ramdiskoptions:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {ramdiskoptions} ramdisksdidevice partition=S:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {ramdiskoptions} ramdisksdipath \boot\boot.sdi
pause
exit


I am not sure if it's best to store WinPE on the system partition [S:] or the recovery partition [R:]
or even the Windows Partition [W:]. Any help would be greatly appreciated! I'm desperate.

UPDATE: I made my first attempt a Spoiler if anyone wants to check that, but I have an updated version that gets me farther.

This is my Diskpart Script:
select disk 0
clean
convert GPT
rem === 1. Create the EFI system partition ===
create partition EFI size=200
format quick fs=fat32 label=EFI
assign letter=S
rem === 2. Create the Microsoft reserved partition ===
create partition MSR size=128
rem === 3. Create the Windows PE partition ===
rem (The partition size is set to 400 MB, though a smaller partition size may be used.)
create partition primary size=20000
format quick fs=ntfs label=WinPE
assign letter=C
rem === 4. Create a Windows or data partition ===
rem (This partition fills the rest of the disk.)
create partition primary
format quick fs=ntfs label=Windows
assign letter=W
set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
list vol
exit


This is my batch script:
@ECHO ON
START /WAIT DISKPART.exe /S DiskpartHope.txt

xcopy D:\EFI\* S:\EFI\* /cherkyfs
xcopy D:\sources\* C:\sources\* /cherkyfs
copy D:\boot\boot.sdi C:\sources\
copy D:\windows\boot\EFI\*.efi S:\EFI\Microsoft\Boot\*
del S:\EFI\Microsoft\Boot\BCD /f

bcdedit -createstore S:\EFI\Microsoft\Boot\BCD

bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {bootmgr} /d "Boot Manager"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {globalsettings} /d "globalsettings"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {dbgsettings} /d "debugsettings"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create {ramdiskoptions} /d "ramdiskoptions"
bcdedit -store S:\EFI\Microsoft\Boot\BCD -create /d "Windows PE" -application osloader>GUID.txt
REM Assign GUID to Variable NEWGUID
For /F "tokens=2 delims={}" %%i in (GUID.txt) do (set newguid=%%i)
bcdedit -store S:\EFI\Microsoft\Boot\BCD /default {%newguid%}

bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} device partition=s:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} path \EFI\Microsoft\Boot\bootmgfw.efi
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} locale en-us
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {bootmgr} timeout 5

bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} device ramdisk=[C:]\sources\boot.wim,{ramdiskoptions}
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} path \windows\system32\winload.efi
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} osdevice ramdisk=[C:]\sources\boot.wim,{ramdiskoptions}
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} systemroot \windows
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} winpe yes
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} nx optin
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {Default} detecthal yes
bcdedit -store S:\EFI\Microsoft\Boot\BCD -displayorder {Default} -addfirst

bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {dbgsettings} debugtype Serial
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {dbgsettings} debugport 1
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {dbgsettings} baudrate 115200

bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {ramdiskoptions} ramdisksdidevice partition=C:
bcdedit -store S:\EFI\Microsoft\Boot\BCD -set {ramdiskoptions} ramdisksdipath \sources\boot.sdi

XCOPY "%CD%\ImageBackup\Install.wim" "C:\"
REM DISM /apply-image /imagefile:"%CD%\ImageBackup\Install.wim" /index:1 /ApplyDir:W:\
REM bcdboot.exe w:\windows /s s: /f all
pause
exit


So I set up the actual winpe partition and create it's boot store before applying any wims and this works. The problem is, it boots me into WinPE as the default image. It's remarked off, but if I apply the image at the end and run bcdboot, it will default to Windows but the sysprep fails. Also I have no idea how to get back to my WinPE again. I have to find a way to setup dual boot at this point. Any ideas?

p.s. instead of calling my script "Restore_image.bat" I now call it "Restore_Hope.bat"

I thought that was funny...
 
Solution
You might get a faster answer on here: https://www.tenforums.com/

someone may know answer but tenforums have more software guys over there :)