Advertisement
aveyo

esd_to_iso

Apr 3rd, 2018 (edited)
2,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 12.98 KB | None | 0 0
  1. @echo off & title ESD to ISO & rem using just built-in tools and scripting - v2
  2. color 1f
  3. echo.
  4. echo      ---------------------------------------------------------------------
  5. echo     :                             ESD to ISO                              :
  6. echo     :---------------------------------------------------------------------:
  7. echo     :      Place in the same directory as the downloaded .esd file        :
  8. echo     :    Make sure you have enough free disk space (10GB recommended)     :
  9. echo     :    Close all other programs because it is a very intensive task     :
  10. echo      ---------------------------------------------------------------------
  11. echo.
  12.  
  13. :: uncomment to skip sha1, make install.wim not install.esd, add specified editions (ex: all non-N) and/or skip iso create
  14. rem set ESDISO_SKIP_SHA=1
  15. rem set ESDISO_MAKE_WIM=1
  16. rem set ESDISO_EDITIONS=Core CoreSingleLanguage Education Enterprise Professional
  17. rem set ESDISO_SKIP_ISO=1
  18.  
  19. :: check 1st parameter or search current directory and automatically select the newest .esd found
  20. set "0=%~f0"& set "ESD=%~dpn1.esd"& set "ISO=%~dpn1.iso"& pushd "%~dp1"& if not exist "%~dpn1.esd" set "ESD="
  21. if "%ESD%"=="" pushd "%~dp0"& for /f delims^=^ eol^= %%i in ('dir /b /oD *.esd 2^>nul') do set "ESD=%%~nxi"& set "ISO=%%~ni.iso"
  22. if not exist "%ESD%" echo [ERROR] No .ESD source file found in "%~dp0" & pause & exit /b                        
  23. echo;%ESD%
  24.  
  25. :: Disable console quickedit feature
  26. reg add HKCU\Console\ESDISO /f /v QuickEdit /d 0 /t reg_dword >nul 2>nul
  27.  
  28. :: self elevate because dism requires admin rights
  29. fltmc>nul || (set _=start "ESDISO" cmd /d/x/r call "%~f0" %*& powershell -nop -c start -verb runas cmd \"/d/x/r $env:_\"& exit /b)
  30.  
  31. :: verify dism support
  32. dism /English /Export-Image /? | findstr SourceImageFile >nul || (echo [ERROR] old DISM, install Windows ADK& pause& exit /b)  
  33.  
  34. :: mark start time
  35. call :time set
  36.  
  37. :: WIM_INFO w_5=wim_5th b_5=build_5th p_5=patch_5th a_5=arch_5th l_5=lang_5th e_5=edi_5th d_5=desc_5th i_5=edi_5th i_Core=index
  38. set snippet=powershell -nop -c iex ([io.file]::ReadAllText($env:0)-split'#[:]wim_info[:]')[1]; WIM_INFO $env:ESD 0 0  
  39. for /f "tokens=1-7 delims=," %%i in ('"%snippet%"') do (set w_%%i=%%i,%%j,%%k,%%l,%%m,%%n,%%o& set /a w_count=%%i
  40. set b_%%i=%%j& set p_%%i=%%k& set a_%%i=%%l& set l_%%i=%%m& set e_%%i=%%n& set d_%%i=%%o& set i_%%n=%%i& set i_%%i=%%n)
  41.  
  42. :: print wim_info
  43. echo;------------------------------------------------------------------------------------
  44. for /l %%i in (1,1,%w_count%) do call echo;%%w_%%i%%
  45. echo;------------------------------------------------------------------------------------
  46.  
  47. :: print SHA1 hash
  48. for %%i in (%ESDISO_SKIP_SHA%) do if %%i equ 1 goto skip_sha1
  49. set "h=([BitConverter]::ToString([Security.Cryptography.HashAlgorithm]::Create('SHA1').ComputeHash($f))-replace'-').ToLower()"
  50. for /f %%h in ('powershell -nop -c "$f=([IO.StreamReader]$env:ESD).BaseStream; $h=%h%; $f.Close(); return $h"') do (set sha1=%%h)
  51. echo;SHA1: %sha1%
  52. echo;------------------------------------------------------------------------------------
  53. :skip_sha1
  54.  
  55. :: use wim_info
  56. setlocal EnableDelayedExpansion
  57. set boot=ESDFiles\Sources\boot.wim& set install=ESDFiles\Sources\install.esd
  58. set O1=/Compress:max& set O2=/Compress:max /CheckIntegrity /Bootable& set O3=/Compress:recovery
  59. for /f "tokens=1-3 delims=." %%i in ("%ESD%") do set /a b1=%%i& set /a b2=%%j& set /a b3=%%i%%j
  60. set build=0& if %b1% gtr 0 if %b3% gtr %b1% set /a build=%b1%& set /a patch=%b2%
  61. set SkipISO=0& set label=%build%.%patch%_ESD& set export=&set single=0  
  62. set media=0& set winpe=0& set setup=0& set /a found=0
  63. for /l %%i in (1,1,%w_count%) do (
  64.   if "!d_%%i:Windows Setup Media=!" neq "!d_%%i!" set /a media=%%i
  65.   if "!d_%%i:Microsoft Windows PE=!" neq "!d_%%i!" set /a winpe=%%i
  66.   if "!d_%%i:Microsoft Windows Setup=!" neq "!d_%%i!" set /a setup=%%i
  67.   if !b_%%i! gtr !build! set /a build=!b_%%i! & if !p_%%i! gtr !patch! set /a patch=!p_%%i!
  68.   if !a_%%i! equ arm64 set ESDISO_MAKE_WIM=1
  69.   if "!e_%%i!" equ "Core" set label=!build!.!patch!_CONSUMER_!a_%%i!_!l_%%i!
  70.   if "!e_%%i!" equ "Enterprise" set label=!build!.!patch!_BUSINESS_!a_%%i!_!l_%%i!
  71. )
  72. if %media% gtr 0 if %winpe% gtr 0 if %setup% gtr 0 set /a found=1
  73. if %found% equ 0 echo; ERROR! Source ESD is not in the correct format or it might be encrypted& pause& exit /b
  74. for %%i in (%ESDISO_MAKE_WIM%) do if /i %%i equ 1 set install=ESDFiles\Sources\install.wim& set O3=/Compress:max
  75. for /l %%i in (1,1,%w_count%) do if %%i neq %media% if %%i neq %winpe% if %%i neq %setup% set export=!export! %%i
  76. for %%i in (%ESDISO_EDITIONS%) do if !i_%%i! gtr 0 set export=& set /a single+=1
  77. for %%i in (%ESDISO_EDITIONS%) do if !i_%%i! gtr 0 set export=!export! !i_%%i!
  78. set selected=& for %%i in (%export%) do set selected=!selected! !e_%%i!& set /a last=%%i
  79. if %single% equ 1 for %%i in (%selected%) do set label=!label:CONSUMER=%%i!& set label=!label:BUSINESS=%%i!& rem
  80. set finalize=%label%.iso& for %%i in (%ESDISO_SKIP_ISO%) do if %%i equ 1 set finalize=%install%
  81. echo;Exporting:%selected%
  82. echo;to %finalize%
  83.  
  84. :: [optional] switch to high performance power scheme
  85. rem call :power 1
  86.  
  87. :: process
  88. (del /f/s/q ESDFiles& rmdir /s/q ESDFiles& mkdir ESDFiles) >nul 2>nul
  89. dism /English /Apply-Image /ImageFile:"%ESD%" /Index:%media% /ApplyDir:"ESDFiles"
  90. echo;Applied: !d_%media%! to ESDFiles\
  91. dism /English /Export-Image /SourceImageFile:"%ESD%" /SourceIndex:%winpe% /DestinationImageFile:%boot% %O1%
  92. echo;Exported: !d_%winpe%! to %boot%
  93. dism /English /Export-Image /SourceImageFile:"%ESD%" /SourceIndex:%setup% /DestinationImageFile:%boot% %O2%
  94. echo;Exported: !d_%setup%! to %boot%
  95. for %%i in (%export%) do (
  96.   if %%i equ !last! set O3=!O3! /CheckIntegrity
  97.   dism /English /Export-Image /SourceImageFile:"%ESD%" /SourceIndex:%%i /DestinationImageFile:%install% !O3!
  98.   echo;Exported: !d_%%i! to %install%
  99. )
  100.  
  101. :: create iso
  102. for %%i in (%ESDISO_SKIP_ISO%) do if %%i equ 1 goto skip_iso
  103. call :DIR2ISO ESDFiles %label%.iso %label%  
  104. (del /f/s/q ESDFiles& rmdir /s/q ESDFiles) >nul 2>nul
  105. :skip_iso
  106.  
  107. :: [optional] restore power scheme
  108. rem call :power 0
  109.  
  110. :: done! print run time and pause
  111. call :time print
  112. pause
  113. exit /b
  114.  
  115.  
  116. :power [USAGE]: call :power 1  ;  call :power 0
  117. if 1%1 equ 10 (powercfg /s %h\p% & exit/b) else for /f "tokens=2 delims=:(" %%s in ('powercfg /getactivescheme') do set h\p=%%s
  118. powercfg /s SCHEME_MIN & exit/b AveYo: toggle high performance snippet
  119.  
  120.  
  121. :time [USAGE]: call :time set  ;  call :time print  ;  call :time print 0  &  echo %!!||%  ;  call :time %TIME_Input%
  122. if /i "%~1"=="set" (set "time|set=%TIME: =0%" & exit/b) else set "time|end=%TIME: =0%" & setlocal EnableDelayedExpansion
  123. if /i "%~1" neq "print" (set "time|set=%~1" & set "time|set=!time|set: =0!") else if "%!!|%"=="" endlocal & exit/b
  124. for /f "tokens=1-6 delims=0123456789" %%i in ("%!!|%%!!|%") do set "CE=%%i"&set "DE=%%k" &set "CS=%%l"&set "DS=%%n"
  125. set "TE=!time|end:%DE%=%%100)*100+1!" & set "TS=!time|set:%DS%=%%100)*100+1!"
  126. set /A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set /A "T=!T:-=8640000-!"
  127. set /A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
  128. set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
  129. endlocal & set "|time|=%value%" & set "time|end=" & set "time|set=" & exit /b locale-safe 24h timer snippet by AveYo, 2017
  130.  
  131.  
  132. #:WIM_INFO:# [PARAMS]: "file" [optional]Index or 0 = all  Output 0 = txt 1 = xml 2 = file.txt 3 = file.xml 4 = xml object
  133. set ^ #=$f0=[io.file]::ReadAllText($env:0); $0=($f0-split '#[:]WIM_INFO[:]' ,3)[1]; $1=$env:1-replace'([`@$])','`$1'; iex($0+$1)
  134. set ^ #=& set "0=%~f0"& set 1=;WIM_INFO %*& powershell -nop -c "%#%"& exit /b %errorcode%
  135. function WIM_INFO ($file = 'install.esd', $index = 0, $out = 0) { :info while ($true) {
  136.   $block = 2097152; $bytes = new-object 'Byte[]' ($block); $begin = [uint64]0; $final = [uint64]0; $limit = [uint64]0
  137.   $steps = [int]([uint64]([IO.FileInfo]$file).Length / $block - 1); $enc = [Text.Encoding]::GetEncoding(28591); $delim = @()
  138.   foreach ($d in '/INSTALLATIONTYPE','/WIM') {$delim += $enc.GetString([Text.Encoding]::Unicode.GetBytes([char]60+ $d +[char]62))}
  139.   $f = new-object IO.FileStream ($file, 3, 1, 1); $p = 0; $p = $f.Seek(0, 2)
  140.   for ($o = 1; $o -le $steps; $o++) {
  141.     $p = $f.Seek(-$block, 1); $r = $f.Read($bytes, 0, $block); if ($r -ne $block) {write-host invalid block $r; break}
  142.     $u = [Text.Encoding]::GetEncoding(28591).GetString($bytes); $t = $u.LastIndexOf($delim[0], [StringComparison]::Ordinal)
  143.     if ($t -lt 0) { $p = $f.Seek(-$block, 1)} else { [void]$f.Seek(($t -$block), 1)
  144.       for ($o = 1; $o -le $block; $o++) { [void]$f.Seek(-2, 1); if ($f.ReadByte() -eq 0xfe) {$begin = $f.Position; break} }
  145.       $limit = $f.Length - $begin; if ($limit -lt $block) {$x = $limit} else {$x = $block}
  146.       $bytes = new-object 'Byte[]' ($x); $r = $f.Read($bytes, 0, $x)
  147.       $u = [Text.Encoding]::GetEncoding(28591).GetString($bytes); $t = $u.IndexOf($delim[1], [StringComparison]::Ordinal)
  148.       if ($t -ge 0) {[void]$f.Seek(($t + 12 -$x), 1); $final = $f.Position} ; break } }
  149.   if ($begin -gt 0 -and $final -gt $begin) {
  150.     $x = $final - $begin; [void]$f.Seek(-$x, 1); $bytes = new-object 'Byte[]' ($x); $r = $f.Read($bytes, 0, $x)
  151.     if ($r -ne $x) {$f.Dispose(); break} else {[xml]$xml = [Text.Encoding]::Unicode.GetString($bytes); $f.Dispose()}
  152.   } else {$f.Dispose()} ; break :info }
  153.   if ($out -eq 1) {[console]::OutputEncoding=[Text.Encoding]::UTF8; $xml.Save([Console]::Out); ''; return}
  154.   if ($out -eq 3) {try{$xml.Save(($file-replace'esd$','xml'))}catch{}; return}; if ($out -eq 4) {return $xml}
  155.   $txt = ''; foreach ($i in $xml.WIM.IMAGE) {if ($index -gt 0 -and $($i.INDEX) -ne $index) {continue}; [int]$a='1'+$i.WINDOWS.ARCH
  156.   $txt+= $i.INDEX+','+$i.WINDOWS.VERSION.BUILD+','+$i.WINDOWS.VERSION.SPBUILD+','+$(@{10='x86';15='arm';19='x64';112='arm64'}[$a])
  157.   $txt+= ','+$i.WINDOWS.LANGUAGES.LANGUAGE+','+$i.WINDOWS.EDITIONID+','+$i.NAME+[char]13+[char]10}; $txt=$txt-replace',(?=,)',', '
  158.   if ($out -eq 2) {try{[io.file]::WriteAllText(($file-replace'esd$','txt'),$txt)}catch{}; return}; if ($out -eq 0) {return $txt}
  159. } #:WIM_INFO:# Quick WIM SWM ESD ISO info - lean and mean snippet by AveYo, 2021
  160.  
  161.  
  162. #:DIR2ISO:#  [PARAMS]: "directory" "file.iso" [optional]"label"
  163. set ^ #=$f0=[io.file]::ReadAllText($env:0); $0=($f0-split '#[:]DIR2ISO[:]' ,3)[1]; $1=$env:1-replace'([`@$])','`$1'; iex($0+$1)
  164. set ^ #=& set "0=%~f0"& set 1=;DIR2ISO %*& powershell -nop -c "%#%"& exit /b %errorcode%
  165. function DIR2ISO ($dir,$iso,$label='DVD_ROM') {if (!(test-path -Path $dir -pathtype Container)) {"[ERR] $dir"; return 1}; $code=@"
  166.  using System; using System.IO; using System.Runtime.Interop`Services; using System.Runtime.Interop`Services.ComTypes;
  167.  public class dir2iso {public int AveYo=2021; [Dll`Import("shlwapi",CharSet=CharSet.Unicode,PreserveSig=false)]
  168.  internal static extern void SHCreateStreamOnFileEx(string f,uint m,uint d,bool b,IStream r,out IStream s);
  169.  public static int Create(string file, ref object obj, int bs, int tb) { IStream dir=(IStream)obj, iso;
  170.  try {SHCreateStreamOnFileEx(file,0x1001,0x80,true,null,out iso);} catch(Exception e) {Console.WriteLine(e.Message); return 1;}
  171.  int d=tb>1024 ? 1024 : 1, pad=tb%d, block=bs*d, total=(tb-pad)/d, c=total>100 ? total/100 : total, i=1, MB=(bs/1024)*tb/1024;
  172.  Console.Write("\r\n{0,2}%  {1}MB {2}  DIR2ISO",0,MB,file); if (pad > 0) dir.CopyTo(iso, pad * block, Int`Ptr.Zero, Int`Ptr.Zero);
  173.  while (total-- > 0) {dir.CopyTo(iso, block, Int`Ptr.Zero, Int`Ptr.Zero); if (total % c == 0) {Console.Write("\r{0,2}%",i++);}}
  174.  iso.Commit(0); Console.WriteLine("\r{0,2}%  {1}MB {2}  DIR2ISO",100,MB,file); return 0;} }
  175. "@; & { $cs = new-object CodeDom.Compiler.CompilerParameters; $cs.GenerateInMemory = 1 #:: ` used to silence ps eventlog
  176.  $compile = (new-object Microsoft.CSharp.CSharpCodeProvider).CompileAssemblyFromSource($cs, $code)
  177.  $BOOT = @(); $bootable = 0; $mbr_efi = @(0,0xEF); $images = @('boot\etfsboot.com','efi\microsoft\boot\efisys.bin') #:: _noprompt
  178.  if (test-path $(join-path $dir -child 'efi\boot\bootaa64.efi')) {$images[0] = 'arm64-detected-use-only-efi-mode'}
  179.  0,1|% { $bootimage = join-path $dir -child $images[$_]; if (test-path -Path $bootimage -pathtype Leaf) {
  180.  $bin = new-object -ComObject ADODB.Stream; $bin.Open(); $bin.Type = 1; $bin.LoadFromFile($bootimage)
  181.  $opt = new-object -ComObject IMAPI2FS.BootOptions;$opt.AssignBootImage($bin.psobject.BaseObject); $opt.PlatformId = $mbr_efi[$_]
  182.  $opt.Emulation = 0; $bootable = 1; $opt.Manufacturer = 'Microsoft'; $BOOT += $opt.psobject.BaseObject } }
  183.  $fsi = new-object -ComObject IMAPI2FS.MsftFileSystemImage; $fsi.FileSystemsToCreate = 4; $fsi.FreeMediaBlocks = 0
  184.  if ($bootable) {$fsi.BootImageOptionsArray = $BOOT}; $TREE = $fsi.Root; $TREE.AddTree($dir,$false); $fsi.VolumeName = $label
  185.  $obj = $fsi.CreateResultImage(); $ret = [dir2iso]::Create($iso,[ref]$obj.ImageStream,$obj.BlockSize,$obj.TotalBlocks) }
  186.  [GC]::Collect(); return $ret
  187. } #:DIR2ISO:#  export directory as (bootable) udf iso - lean and mean snippet by AveYo, 2021
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement