@echo off
set /a OS_Index=1
call |time>x:\starttime.txt
@echo Running command to active high-performance power scheme
@echo powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
@echo. 
set folderPath=%WinPESource%Images\
set ImageFileName=
for %%F in ("%folderPath%*.wim") do (  
    set ImageFileName=%%~nxF
    goto :found  
)   
 
:found  
@echo Check %folderPath%%ImageFileName%
@echo.
@echo Try to find a fixed internal DISK
if not exist %WinPESource%images\%ImageFileName% echo Not Found Images in %WinPESource%images\%ImageFileName%  && goto :ERROR
@echo.

for /l %%i in (3,-1,0) do (
    echo select disk %%i > x:\detdisk.txt
	echo detail disk >> x:\detdisk.txt
	diskpart /s x:\detdisk.txt > x:\det.txt
	find /i "Type   : NVME" x:\det.txt
	if not errorlevel 1 set Disk=%%i
)
if defined Disk goto :FORMAT

for /l %%i in (3,-1,0) do (
    echo select disk %%i > x:\detdisk.txt
	echo detail disk >> x:\detdisk.txt
	diskpart /s x:\detdisk.txt > x:\det.txt
	find /i "Type   : SATA" x:\det.txt
	if not errorlevel 1 set Disk=%%i
)
if defined Disk goto :FORMAT

for /l %%i in (3,-1,0) do (
    echo select disk %%i > x:\detdisk.txt
	echo detail disk >> x:\detdisk.txt
	diskpart /s x:\detdisk.txt > x:\det.txt
	find /i "Type   : SD" x:\det.txt
	if not errorlevel 1 set Disk=%%i
)
if defined Disk goto :FORMAT

if not defined Disk echo Cannot find a fixed internal DISK. && goto :ERROR

:FORMAT
echo.
echo Using disk %Disk%
echo.
echo select disk "%Disk%" > x:\winpart.txt
echo clean >> x:\winpart.txt
echo convert gpt >> 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
echo create partition msr size=16 >> x:\winpart.txt
echo create partition primary >> x:\winpart.txt
echo format quick fs=ntfs label="Windows" >> x:\winpart.txt
echo assign letter="W" >> x:\winpart.txt
echo shrink desired=1200 >> x:\winpart.txt
echo create partition primary >> x:\winpart.txt
echo format quick fs=ntfs label="Recovery" >> x:\winpart.txt
echo assign letter="R" >> x:\winpart.txt
echo set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac" >> x:\winpart.txt
echo gpt attributes=0x8000000000000001 >> x:\winpart.txt
echo exit >> x:\WinPart.txt
echo Winpart.txt now contains.....
echo.
type x:\winpart.txt
echo.
echo diskpart /s x:\winpart.txt
diskpart /s x:\winpart.txt
ping 127.0.0.1 -n 3 1>nul
if not exist W:\ echo Diskpart failed to format disk. && goto :ERROR

@echo.
@echo Making directories...
@echo w:\recycler\scratch
@echo r:\recovery\windowsre
md w:\recycler\scratch
if errorlevel 1 echo Failed to make w:\recycler\scratch directory && goto :ERROR
md r:\recovery\windowsre
if errorlevel 1 echo Failed to make r:\recovery\windowsre directory && goto :ERROR

@echo.
@echo Applying image to Windows partition
::if /i "%IMAGETYPE%"=="WIM" (
@echo DISM /Apply-Image /ImageFile:%WinPESource%images\%ImageFileName% /Index:%OS_Index% /ApplyDir:w: /EA /ScratchDir:w:\recycler\SCRATCH
    DISM /Apply-Image /ImageFile:%WinPESource%images\%ImageFileName% /Index:%OS_Index% /ApplyDir:w: /EA /ScratchDir:w:\recycler\SCRATCH
    if errorlevel 1 echo Failed to apply image. && goto :ERROR
::) else (
	::DISM /Apply-Image /ImageFile:%WinPESource%images\install.swm /SwmFile:%WinPESource%images\install*.swm /Index:%OS_Index% /ApplyDir:w: /EA /ScratchDir:w:\recycler\SCRATCH
	::if errorlevel 1 echo Failed to apply image. && goto :ERROR
::)

@echo *****************************************************************
@echo Moving Winre.wim to  Recovery Partition
@echo *****************************************************************
@echo.
set /a cpyWinre = 0
if EXIST w:\Windows\System32\recovery\winre.wim (
	attrib w:\Windows\System32\recovery\winre.wim -s -h -a -r
    @echo move w:\Windows\System32\recovery\winre.wim r:\recovery\windowsre
    move w:\Windows\System32\recovery\winre.wim r:\recovery\windowsre
	set /a cpyWinre = 1
)
::if errorlevel 1 echo Failed to move winre.wim to R:\recovery\windowsre && goto :ERROR
@echo if EXIST %WinPESource%Images\Winre\Winre.wim xcopy %WinPESource%Images\Winre\Winre.wim r:\recovery\windowsre  /y
if EXIST %WinPESource%Images\Winre\winre.wim (
    @echo xcopy %WinPESource%Images\Winre\winre.wim r:\recovery\windowsre  /y
	xcopy %WinPESource%Images\Winre\winre.wim r:\recovery\windowsre  /y
	set /a cpyWinre = 2
)
if %cpyWinre%==0 (
	@echo.
	@echo Not found Winre.wim file, error code:%cpyWinre%
	@echo.
	goto :ERROR
)
@echo.
@echo *****************************************************************
@echo Setting BCD on EFI partition
@echo *****************************************************************
@echo.
@echo W:\WINDOWS\SYSTEM32\BCDBOOT w:\WINDOWS /s s: /f uefi
W:\WINDOWS\SYSTEM32\bcdboot.exe w:\WINDOWS /s s: /f uefi
if errorlevel 1 echo Failed to set BCD on EFI partition. && goto :ERROR

@echo.
@echo *****************************************************************
@echo  Copying over NON Scanstate Packages, if they exist.
@echo  IF YOU COPY OVER SCANSTATE PPKG, THIS CAUSE IMAGE TO FAIL DURING RECOVERY
@echo *****************************************************************
@echo if EXIST %WinPESource%images\usmt.ppkg xcopy %WinPESource%images\usmt.ppkg w:\Recovery\Customizations\   /y
if EXIST %WinPESource%images\usmt.ppkg xcopy %WinPESource%images\usmt.ppkg w:\Recovery\Customizations\ /y
@echo if EXIST w:\Recovery\Customizations\usmt.ppkg DISM /Apply-CustomDataImage /CustomDataImage:w:\Recovery\Customizations\usmt.ppkg /ImagePath:w:\ /SingleInstance
if EXIST w:\Recovery\Customizations\usmt.ppkg DISM /Apply-CustomDataImage /CustomDataImage:w:\Recovery\Customizations\usmt.ppkg /ImagePath:w:\ /SingleInstance
@echo.
echo.
@echo *****************************************************************
@echo  Setting the recovery environment
@echo *****************************************************************
@echo Updating WinRE association
@echo w:\windows\system32\reagentc /SetREImage /Path r:\RECOVERY\WINDOWSRE /target w:\windows
w:\windows\system32\reagentc /SetREImage /Path r:\RECOVERY\WINDOWSRE /target w:\windows
if errorlevel 1 echo Failed to set recovery enviroment. && goto :ERROR
@echo.
mkdir S:\EFI\OEM
xcopy /e /h /y S:\EFI\Microsoft\* S:\EFI\OEM\
del /a S:\EFI\OEM\Boot\BCD
bcdedit /createstore S:\BCD
bcdedit /store S:\BCD /create {bootmgr} /d "Windows Boot Manager"
bcdedit /store S:\BCD /set {bootmgr} device partition=S:
bcdedit /store S:\BCD /set {bootmgr} locale en-us
bcdedit /store S:\BCD /set {bootmgr} integrityservices Enable

bcdedit /store S:\BCD /create {11111111-1111-1111-1111-111111111111} /d "Windows Recovery" /device
bcdedit /store S:\BCD /set {11111111-1111-1111-1111-111111111111} ramdisksdidevice partition=R:
bcdedit /store S:\BCD /set {11111111-1111-1111-1111-111111111111} ramdisksdipath \Recovery\WindowsRE\boot.sdi

bcdedit /store S:\BCD /create {22222222-2222-2222-2222-222222222222} /d "Windows Recovery Environment" /application osloader
bcdedit /store S:\BCD /set {bootmgr} default {22222222-2222-2222-2222-222222222222}
bcdedit /store S:\BCD /set {bootmgr} displayorder {22222222-2222-2222-2222-222222222222}

bcdedit /store S:\BCD /set {default} device ramdisk=[R:]\Recovery\WindowsRE\winre.wim,{11111111-1111-1111-1111-111111111111}
bcdedit /store S:\BCD /set {default} path \Windows\System32\winload.efi
bcdedit /store S:\BCD /set {default} locale en-us
bcdedit /store S:\BCD /set {default} displaymessage "Recovery"
bcdedit /store S:\BCD /set {default} osdevice ramdisk=[R:]\Recovery\WindowsRE\winre.wim,{11111111-1111-1111-1111-111111111111}
bcdedit /store S:\BCD /set {default} systemroot \Windows
bcdedit /store S:\BCD /set {default} nx OptIn
bcdedit /store S:\BCD /set {default} bootmenupolicy Standard
bcdedit /store S:\BCD /set {default} winpe Yes

xcopy /h /y S:\BCD* S:\EFI\OEM\Boot\.
del /a S:\BCD*
@echo.
attrib r:\Recovery\Windowsre\winre.wim +s +h +a +r
@echo.
@echo.
@echo *****************************************************************
@echo  Copying over Unattend.xml to use on factory floor if it exists
@echo *****************************************************************
@echo if EXIST %WinPESource%Scripts\Unattend.xml xcopy %WinPESource%Scripts\Unattend.xml w:\Windows\Panther\ /y
if EXIST %WinPESource%Scripts\Unattend.xml xcopy %WinPESource%Scripts\Unattend.xml w:\Windows\Panther\  /y 
@echo.
@echo.
@echo xcopy %WinPESource%Scripts\Recovery\* w:\RECOVERY\  /e /y  
xcopy %WinPESource%Scripts\Recovery\* w:\Recovery\ /e /y 
attrib w:\RECOVERY +s +h +a +r
@echo.
@echo xcopy %WinPESource%Scripts\Recovery\OEM\Info\*.* w:\Windows\System32\oobe\Info\ /e /y 
xcopy  %WinPESource%Scripts\Recovery\OEM\Info\*.* w:\Windows\System32\oobe\Info\ /e /y 
@echo.
@echo.
@echo *****************************************************************
@echo  Copying over resetconfig.xml and assets, if they exist.
@echo *****************************************************************
::@echo if EXIST %WinPESource%Scripts\Recovery\OEM\resetconfig.xml xcopy %WinPESource%Scripts\Recovery\*.* w:\Recovery\ /e /y
::if EXIST %WinPESource%Scripts\Recovery\OEM\resetconfig.xml xcopy %WinPESource%Scripts\Recovery\*.* w:\Recovery\ /e /y
if EXIST W:\Recovery\OEM\Wallpaper\img0.jpg copy W:\Recovery\OEM\Wallpaper\img0.jpg  w:\Windows\Web\   /y 
@echo.
@echo.
if EXIST w:\recycler rmdir /s /q w:\recycler
::rmdir /s /q W:\PerfLogs
if EXIST %WinPESource%Drivers echo Dism /image:W: /Add-Driver /driver:%WinPESource%Drivers\x64 /recurse
if EXIST %WinPESource%Drivers Dism /image:W: /Add-Driver /driver:%WinPESource%Drivers\x64 /recurse
@echo.
color 2f
@echo ****************************************************
@echo   Image deployment COMPLETED. Type EXIT to shutdown.
@echo   Type WPEUTIL to shutdown the system.
@echo ****************************************************
goto END
:ERROR
@echo.
color 0c
@echo *************************************************************
@echo   An error has been detected,the procedure cannot continue.
@echo *************************************************************
:END
::Dism /image:w:\ /set-allIntl:en-us
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
pause
WPEUTIL shutdown