Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off & title ESD to ISO & rem using just built-in tools and scripting - v2
- color 1f
- echo.
- echo ---------------------------------------------------------------------
- echo : ESD to ISO :
- echo :---------------------------------------------------------------------:
- echo : Place in the same directory as the downloaded .esd file :
- echo : Make sure you have enough free disk space (10GB recommended) :
- echo : Close all other programs because it is a very intensive task :
- echo ---------------------------------------------------------------------
- echo.
- :: uncomment to skip sha1, make install.wim not install.esd, add specified editions (ex: all non-N) and/or skip iso create
- rem set ESDISO_SKIP_SHA=1
- rem set ESDISO_MAKE_WIM=1
- rem set ESDISO_EDITIONS=Core CoreSingleLanguage Education Enterprise Professional
- rem set ESDISO_SKIP_ISO=1
- :: check 1st parameter or search current directory and automatically select the newest .esd found
- set "0=%~f0"& set "ESD=%~dpn1.esd"& set "ISO=%~dpn1.iso"& pushd "%~dp1"& if not exist "%~dpn1.esd" set "ESD="
- if "%ESD%"=="" pushd "%~dp0"& for /f delims^=^ eol^= %%i in ('dir /b /oD *.esd 2^>nul') do set "ESD=%%~nxi"& set "ISO=%%~ni.iso"
- if not exist "%ESD%" echo [ERROR] No .ESD source file found in "%~dp0" & pause & exit /b
- echo;%ESD%
- :: Disable console quickedit feature
- reg add HKCU\Console\ESDISO /f /v QuickEdit /d 0 /t reg_dword >nul 2>nul
- :: self elevate because dism requires admin rights
- fltmc>nul || (set _=start "ESDISO" cmd /d/x/r call "%~f0" %*& powershell -nop -c start -verb runas cmd \"/d/x/r $env:_\"& exit /b)
- :: verify dism support
- dism /English /Export-Image /? | findstr SourceImageFile >nul || (echo [ERROR] old DISM, install Windows ADK& pause& exit /b)
- :: mark start time
- call :time set
- :: 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
- set snippet=powershell -nop -c iex ([io.file]::ReadAllText($env:0)-split'#[:]wim_info[:]')[1]; WIM_INFO $env:ESD 0 0
- 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
- 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)
- :: print wim_info
- echo;------------------------------------------------------------------------------------
- for /l %%i in (1,1,%w_count%) do call echo;%%w_%%i%%
- echo;------------------------------------------------------------------------------------
- :: print SHA1 hash
- for %%i in (%ESDISO_SKIP_SHA%) do if %%i equ 1 goto skip_sha1
- set "h=([BitConverter]::ToString([Security.Cryptography.HashAlgorithm]::Create('SHA1').ComputeHash($f))-replace'-').ToLower()"
- for /f %%h in ('powershell -nop -c "$f=([IO.StreamReader]$env:ESD).BaseStream; $h=%h%; $f.Close(); return $h"') do (set sha1=%%h)
- echo;SHA1: %sha1%
- echo;------------------------------------------------------------------------------------
- :skip_sha1
- :: use wim_info
- setlocal EnableDelayedExpansion
- set boot=ESDFiles\Sources\boot.wim& set install=ESDFiles\Sources\install.esd
- set O1=/Compress:max& set O2=/Compress:max /CheckIntegrity /Bootable& set O3=/Compress:recovery
- for /f "tokens=1-3 delims=." %%i in ("%ESD%") do set /a b1=%%i& set /a b2=%%j& set /a b3=%%i%%j
- set build=0& if %b1% gtr 0 if %b3% gtr %b1% set /a build=%b1%& set /a patch=%b2%
- set SkipISO=0& set label=%build%.%patch%_ESD& set export=&set single=0
- set media=0& set winpe=0& set setup=0& set /a found=0
- for /l %%i in (1,1,%w_count%) do (
- if "!d_%%i:Windows Setup Media=!" neq "!d_%%i!" set /a media=%%i
- if "!d_%%i:Microsoft Windows PE=!" neq "!d_%%i!" set /a winpe=%%i
- if "!d_%%i:Microsoft Windows Setup=!" neq "!d_%%i!" set /a setup=%%i
- if !b_%%i! gtr !build! set /a build=!b_%%i! & if !p_%%i! gtr !patch! set /a patch=!p_%%i!
- if !a_%%i! equ arm64 set ESDISO_MAKE_WIM=1
- if "!e_%%i!" equ "Core" set label=!build!.!patch!_CONSUMER_!a_%%i!_!l_%%i!
- if "!e_%%i!" equ "Enterprise" set label=!build!.!patch!_BUSINESS_!a_%%i!_!l_%%i!
- )
- if %media% gtr 0 if %winpe% gtr 0 if %setup% gtr 0 set /a found=1
- if %found% equ 0 echo; ERROR! Source ESD is not in the correct format or it might be encrypted& pause& exit /b
- for %%i in (%ESDISO_MAKE_WIM%) do if /i %%i equ 1 set install=ESDFiles\Sources\install.wim& set O3=/Compress:max
- 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
- for %%i in (%ESDISO_EDITIONS%) do if !i_%%i! gtr 0 set export=& set /a single+=1
- for %%i in (%ESDISO_EDITIONS%) do if !i_%%i! gtr 0 set export=!export! !i_%%i!
- set selected=& for %%i in (%export%) do set selected=!selected! !e_%%i!& set /a last=%%i
- if %single% equ 1 for %%i in (%selected%) do set label=!label:CONSUMER=%%i!& set label=!label:BUSINESS=%%i!& rem
- set finalize=%label%.iso& for %%i in (%ESDISO_SKIP_ISO%) do if %%i equ 1 set finalize=%install%
- echo;Exporting:%selected%
- echo;to %finalize%
- :: [optional] switch to high performance power scheme
- rem call :power 1
- :: process
- (del /f/s/q ESDFiles& rmdir /s/q ESDFiles& mkdir ESDFiles) >nul 2>nul
- dism /English /Apply-Image /ImageFile:"%ESD%" /Index:%media% /ApplyDir:"ESDFiles"
- echo;Applied: !d_%media%! to ESDFiles\
- dism /English /Export-Image /SourceImageFile:"%ESD%" /SourceIndex:%winpe% /DestinationImageFile:%boot% %O1%
- echo;Exported: !d_%winpe%! to %boot%
- dism /English /Export-Image /SourceImageFile:"%ESD%" /SourceIndex:%setup% /DestinationImageFile:%boot% %O2%
- echo;Exported: !d_%setup%! to %boot%
- for %%i in (%export%) do (
- if %%i equ !last! set O3=!O3! /CheckIntegrity
- dism /English /Export-Image /SourceImageFile:"%ESD%" /SourceIndex:%%i /DestinationImageFile:%install% !O3!
- echo;Exported: !d_%%i! to %install%
- )
- :: create iso
- for %%i in (%ESDISO_SKIP_ISO%) do if %%i equ 1 goto skip_iso
- call :DIR2ISO ESDFiles %label%.iso %label%
- (del /f/s/q ESDFiles& rmdir /s/q ESDFiles) >nul 2>nul
- :skip_iso
- :: [optional] restore power scheme
- rem call :power 0
- :: done! print run time and pause
- call :time print
- pause
- exit /b
- :power [USAGE]: call :power 1 ; call :power 0
- 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
- powercfg /s SCHEME_MIN & exit/b AveYo: toggle high performance snippet
- :time [USAGE]: call :time set ; call :time print ; call :time print 0 & echo %!!||% ; call :time %TIME_Input%
- if /i "%~1"=="set" (set "time|set=%TIME: =0%" & exit/b) else set "time|end=%TIME: =0%" & setlocal EnableDelayedExpansion
- if /i "%~1" neq "print" (set "time|set=%~1" & set "time|set=!time|set: =0!") else if "%!!|%"=="" endlocal & exit/b
- for /f "tokens=1-6 delims=0123456789" %%i in ("%!!|%%!!|%") do set "CE=%%i"&set "DE=%%k" &set "CS=%%l"&set "DS=%%n"
- set "TE=!time|end:%DE%=%%100)*100+1!" & set "TS=!time|set:%DS%=%%100)*100+1!"
- set /A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set /A "T=!T:-=8640000-!"
- set /A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
- set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
- endlocal & set "|time|=%value%" & set "time|end=" & set "time|set=" & exit /b locale-safe 24h timer snippet by AveYo, 2017
- #:WIM_INFO:# [PARAMS]: "file" [optional]Index or 0 = all Output 0 = txt 1 = xml 2 = file.txt 3 = file.xml 4 = xml object
- set ^ #=$f0=[io.file]::ReadAllText($env:0); $0=($f0-split '#[:]WIM_INFO[:]' ,3)[1]; $1=$env:1-replace'([`@$])','`$1'; iex($0+$1)
- set ^ #=& set "0=%~f0"& set 1=;WIM_INFO %*& powershell -nop -c "%#%"& exit /b %errorcode%
- function WIM_INFO ($file = 'install.esd', $index = 0, $out = 0) { :info while ($true) {
- $block = 2097152; $bytes = new-object 'Byte[]' ($block); $begin = [uint64]0; $final = [uint64]0; $limit = [uint64]0
- $steps = [int]([uint64]([IO.FileInfo]$file).Length / $block - 1); $enc = [Text.Encoding]::GetEncoding(28591); $delim = @()
- foreach ($d in '/INSTALLATIONTYPE','/WIM') {$delim += $enc.GetString([Text.Encoding]::Unicode.GetBytes([char]60+ $d +[char]62))}
- $f = new-object IO.FileStream ($file, 3, 1, 1); $p = 0; $p = $f.Seek(0, 2)
- for ($o = 1; $o -le $steps; $o++) {
- $p = $f.Seek(-$block, 1); $r = $f.Read($bytes, 0, $block); if ($r -ne $block) {write-host invalid block $r; break}
- $u = [Text.Encoding]::GetEncoding(28591).GetString($bytes); $t = $u.LastIndexOf($delim[0], [StringComparison]::Ordinal)
- if ($t -lt 0) { $p = $f.Seek(-$block, 1)} else { [void]$f.Seek(($t -$block), 1)
- for ($o = 1; $o -le $block; $o++) { [void]$f.Seek(-2, 1); if ($f.ReadByte() -eq 0xfe) {$begin = $f.Position; break} }
- $limit = $f.Length - $begin; if ($limit -lt $block) {$x = $limit} else {$x = $block}
- $bytes = new-object 'Byte[]' ($x); $r = $f.Read($bytes, 0, $x)
- $u = [Text.Encoding]::GetEncoding(28591).GetString($bytes); $t = $u.IndexOf($delim[1], [StringComparison]::Ordinal)
- if ($t -ge 0) {[void]$f.Seek(($t + 12 -$x), 1); $final = $f.Position} ; break } }
- if ($begin -gt 0 -and $final -gt $begin) {
- $x = $final - $begin; [void]$f.Seek(-$x, 1); $bytes = new-object 'Byte[]' ($x); $r = $f.Read($bytes, 0, $x)
- if ($r -ne $x) {$f.Dispose(); break} else {[xml]$xml = [Text.Encoding]::Unicode.GetString($bytes); $f.Dispose()}
- } else {$f.Dispose()} ; break :info }
- if ($out -eq 1) {[console]::OutputEncoding=[Text.Encoding]::UTF8; $xml.Save([Console]::Out); ''; return}
- if ($out -eq 3) {try{$xml.Save(($file-replace'esd$','xml'))}catch{}; return}; if ($out -eq 4) {return $xml}
- $txt = ''; foreach ($i in $xml.WIM.IMAGE) {if ($index -gt 0 -and $($i.INDEX) -ne $index) {continue}; [int]$a='1'+$i.WINDOWS.ARCH
- $txt+= $i.INDEX+','+$i.WINDOWS.VERSION.BUILD+','+$i.WINDOWS.VERSION.SPBUILD+','+$(@{10='x86';15='arm';19='x64';112='arm64'}[$a])
- $txt+= ','+$i.WINDOWS.LANGUAGES.LANGUAGE+','+$i.WINDOWS.EDITIONID+','+$i.NAME+[char]13+[char]10}; $txt=$txt-replace',(?=,)',', '
- if ($out -eq 2) {try{[io.file]::WriteAllText(($file-replace'esd$','txt'),$txt)}catch{}; return}; if ($out -eq 0) {return $txt}
- } #:WIM_INFO:# Quick WIM SWM ESD ISO info - lean and mean snippet by AveYo, 2021
- #:DIR2ISO:# [PARAMS]: "directory" "file.iso" [optional]"label"
- set ^ #=$f0=[io.file]::ReadAllText($env:0); $0=($f0-split '#[:]DIR2ISO[:]' ,3)[1]; $1=$env:1-replace'([`@$])','`$1'; iex($0+$1)
- set ^ #=& set "0=%~f0"& set 1=;DIR2ISO %*& powershell -nop -c "%#%"& exit /b %errorcode%
- function DIR2ISO ($dir,$iso,$label='DVD_ROM') {if (!(test-path -Path $dir -pathtype Container)) {"[ERR] $dir"; return 1}; $code=@"
- using System; using System.IO; using System.Runtime.Interop`Services; using System.Runtime.Interop`Services.ComTypes;
- public class dir2iso {public int AveYo=2021; [Dll`Import("shlwapi",CharSet=CharSet.Unicode,PreserveSig=false)]
- internal static extern void SHCreateStreamOnFileEx(string f,uint m,uint d,bool b,IStream r,out IStream s);
- public static int Create(string file, ref object obj, int bs, int tb) { IStream dir=(IStream)obj, iso;
- try {SHCreateStreamOnFileEx(file,0x1001,0x80,true,null,out iso);} catch(Exception e) {Console.WriteLine(e.Message); return 1;}
- 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;
- 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);
- while (total-- > 0) {dir.CopyTo(iso, block, Int`Ptr.Zero, Int`Ptr.Zero); if (total % c == 0) {Console.Write("\r{0,2}%",i++);}}
- iso.Commit(0); Console.WriteLine("\r{0,2}% {1}MB {2} DIR2ISO",100,MB,file); return 0;} }
- "@; & { $cs = new-object CodeDom.Compiler.CompilerParameters; $cs.GenerateInMemory = 1 #:: ` used to silence ps eventlog
- $compile = (new-object Microsoft.CSharp.CSharpCodeProvider).CompileAssemblyFromSource($cs, $code)
- $BOOT = @(); $bootable = 0; $mbr_efi = @(0,0xEF); $images = @('boot\etfsboot.com','efi\microsoft\boot\efisys.bin') #:: _noprompt
- if (test-path $(join-path $dir -child 'efi\boot\bootaa64.efi')) {$images[0] = 'arm64-detected-use-only-efi-mode'}
- 0,1|% { $bootimage = join-path $dir -child $images[$_]; if (test-path -Path $bootimage -pathtype Leaf) {
- $bin = new-object -ComObject ADODB.Stream; $bin.Open(); $bin.Type = 1; $bin.LoadFromFile($bootimage)
- $opt = new-object -ComObject IMAPI2FS.BootOptions;$opt.AssignBootImage($bin.psobject.BaseObject); $opt.PlatformId = $mbr_efi[$_]
- $opt.Emulation = 0; $bootable = 1; $opt.Manufacturer = 'Microsoft'; $BOOT += $opt.psobject.BaseObject } }
- $fsi = new-object -ComObject IMAPI2FS.MsftFileSystemImage; $fsi.FileSystemsToCreate = 4; $fsi.FreeMediaBlocks = 0
- if ($bootable) {$fsi.BootImageOptionsArray = $BOOT}; $TREE = $fsi.Root; $TREE.AddTree($dir,$false); $fsi.VolumeName = $label
- $obj = $fsi.CreateResultImage(); $ret = [dir2iso]::Create($iso,[ref]$obj.ImageStream,$obj.BlockSize,$obj.TotalBlocks) }
- [GC]::Collect(); return $ret
- } #:DIR2ISO:# export directory as (bootable) udf iso - lean and mean snippet by AveYo, 2021
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement