[APORTE] [BATCH] WIM Resource Replacer

Iniciado por Eleкtro, 14 Abril 2014, 05:51 AM

0 Miembros y 1 Visitante están viendo este tema.

Eleкtro

Este Script sirve para automatizar la tarea de montar una imagen WIM de Windows y reemplazar recursos de imágenes (bmp/ico) en las librerías de recursos de la instalación de Windows, así como también sirve para reemplazar archivos.

Ejemplo de los recursos a los que me refiero:



Se necesitan unos requisitos y dependencias para usar este Script:
· Saber de lo que estoy hablando.
· Conocer el nombre de los recursos que quieres reemplazar y disponer de tus imágenes ya modificados (obviamente).
· Dism e ImageX (WAIK/WADK).
· Anolis Resourcer > http://www.anol.is/resourcer/


Las instrucciones de uso y un extenso tutorial lo publiqué en este otro tema: http://foro.elhacker.net/windows/guia_de_personalizacion_de_imagenes_de_implementacion_de_windows_wim_parte_4-t412468.0.html así que lo haré de nuevo de manera resumida: solo tienen que escribir en un archivo de texto delimitando por puntoycomas los recursos que quieren reemplazar, el nombre del recurso y el índice, así como los archivos que quieran reemplazar, y configurar las variables documentadas del Script (La ubicación de la imagen, el directorio de montaje, etc...)

Nota: Ver el ejemplo del archivo delimitado dentro del enlace de abajo o en las instrucciones extendidas del enlace de arriba.

Todo lo necesario está incluido en este archivo comprimido:
http://www.mediafire.com/download/ktzms7i09qt4n2b/wim+resource+replacer+0.2.rar

Imágenes:


(Código corregido, el otro que subí estaba incompleto)
WIM Resource Replacer.cmd
Código (dos) [Seleccionar]
@Echo OFF



REM =================
REM Console Settings:
REM =================

Title WIM Resource Replacer - By Elektro
Mode Con Cols=150 Lines=50
CHCP 1252 1>NUL & REM Windows-1252, Spanish-Latin.



REM =====
REM Info:
REM =====

Echo+
Echo  --------------------------------------------------------------------------------------------------
Echo  This script mounts a Windows Image ^(WIM^) and then replaces image resources inside resource files
Echo  --------------------------------------------------------------------------------------------------
Echo+



REM ====================
REM User defined values:
REM ====================

REM This value indicates whether the Windows Image should be mounted.
REM True  = 'DISM' will try to mount the 'WIM' image.
REM False = 'DISM' will not try to mount the WIM image.
REM Set this value to 'FALSE' if the 'WIM' image is already mounted in the specified mount directory,
REM Otherwise, keep it as 'TRUE'.
Set "MountImage=True"

REM This value indicates whether the Windows Image should be unmounted at the end.
REM True  = 'DISM' will try to unmount the 'WIM' image.
REM False = 'DISM' will not try to unmount the WIM image.
REM Set this value to 'FALSE' if you want to keep the image mounted to make other changes,
REM Otherwise, keep it as 'TRUE'.
Set "UnmountImage=True"

REM This value indicates whether the Windows Image should be rebuilt at the end.
REM True  = 'ImageX' will try to rebuild the 'WIM' image.
REM False = 'ImageX' will keep the 'WIM' image as is.
REM Set this value to 'FALSE' if you want to keep the image with normal size to make other changes,
REM Otherwise, keep it as 'TRUE'.
Set "Rebuild=True"

REM This value indicates whether the Windows Image should be cleaned-up at the end.
REM True  = 'Dism' will try to CleanUp the 'WIM' image components.
REM False = 'Dism' will keep the 'WIM' image as is.
REM Set this value to 'FALSE' if you want to keep the image with normal size to make other changes,
REM Otherwise, keep it as 'TRUE'.
Set "CleanUp=True"

REM This value indicates the 'WIM' image to be mounted.
Set "WindowsImage=%UserProfile%\Desktop\win 8 .1\win\sources\Boot.wim"

REM This value indicates the Image Index of the 'WIM' image.
Set "ImageIndex=2"

REM This value indicates the directory where to mount the 'WIM' image.
Set "MountDir=%HomeDrive%\WinBootMount"

REM This value indicates the ubication of Anolis Resourcer 'Anolis.exe' file.
Set "Anolis=%CD%\Tools\Anolis.exe"

REM This value indicates the textfile that contains the Recource files to replace.
Set "ResourceList=%CD%\Resources.txt"

REM This value indicates the ubication of 'ImageX.exe' file.
Set "ImageX=%CD%\Tools\Dism\ImageX.exe"

REM This value indicates the ubication of a custom 'DISM.exe' file if needed.
REM Default ubication: "%SystemRoot%\System32\Dism.exe"
Set "Dism=%CD%\Tools\Dism\Dism.exe"

REM This value indicates the ubication of the logfile that will record 'DISM' errors.
Set "DismLogfile=%CD%\%~n0 DISM.log"

REM This value indicates the logging-level of the 'DISM' process.
REM 1 = Errors only
REM 2 = Errors and warnings
REM 3 = Errors, warnings, and informational
REM 4 = All of the information listed previously, plus debug output
Set /A "DismLogLevel=2"

REM This value indicates the ubication of the logfile that will record Main information.
Set "Logfile=%CD%\%~n0.log"



REM =====
REM Main:
REM =====

:: Call Methods.
Call :CheckErrors
Call :CreateLogs
Call :CreateMountDir
Call :GetWIMSizeBefore
Call :StartTimer
Call :Mount
CLS
Call :ParseResources
Call :Unmount
Call :StopTimer
Call :GetWIMSizeAfter
Call :EndLog
Pause&Exit



REM ========
REM Methods:
REM ========

:CreateLogs
:: Create the 'DISM' logfile with 'ANSI' encoding,
:: this is necessary to record a non-default 'DISM' loglevel such as '1' or '2'.
Echo+ >"%Logfile%"

:: Create the Main Logfile and record starting info on it.
(
Echo+
Echo    WIM Resource Replacer
Echo ===========================
Echo   %DATE% ^| %TIME:~0,-3%
Echo /\/\/\/\/\/\/\/\/\/\/\/\/\/
Echo+
Echo [i] Mount Image?...: %MountImage%
Echo [i] Unmount Image?.: %UnmountImage%
Echo [i] CleanUp Image?.: %CleanUp%
Echo [i] Rebuild Image?.: %Rebuild%
Echo+
Echo [i] Windows Image..: %WindowsImage%
Echo [i] Image Index....: %ImageIndex%
Echo [i] Mount Directory: %MountDir%
Echo+
Echo [i] ImageX Path....: %ImageX%
Echo [i] Dism Path......: %Dism%
Echo [i] Dism Log File..: %DismLogfile%
Echo [i] Dism Log Level.: %DismLogLevel%
Echo+
Echo [i] Anolis Path....: %Anolis%
Echo [i] Resource List..: %ResourceList%
Echo [i] Log File.......: %LogFile%
Echo+
Echo ===========================
)>"%Logfile%"

:: Display starting log info.
Type "%Logfile%"

:: Continue writting log.
(
Echo+
Echo Resource conflicts:
Echo ===================
Echo+
)>>"%LogFile%"

Goto:EOF


:EndLog
(
Echo+
Echo [i] Done!
Echo [i] WIM Size Before....: %WIMSizeBefore%
Echo [i] WIM Size After.....: %WIMSizeAfter%
Echo [i] Resources Success..: %ResourceSuccessCount% files.
Echo [i] Resources Failed...: %ResourceFailsCount% files.
Echo [i] Resources Not Found: %ResourceNotFoundCount% files.
Echo [i] Files Added........: %FilesAddedCount% files.
Echo [i] Files Failed.......: %FilesFailsCount% files.
Echo [i] Files Not Found....: %FilesNotFoundCount% files.
Echo [i] Elapsed Time.......: %ElapsedTime%
Echo+
)>>"%LogFile%"

:: Display ending information.
CLS
Type "%LogFile%"
Goto:EOF


:CreateMountDir
:: Create the mount directory.
(
If Not Exist "%MountDir%" If /I "%MountImage%" EQU "True" (
MkDir "%MountDir%" 2>NUL
)
) || (
Echo+
Echo [x] Error trying to create the directory "%MountDir%" to mount the Windows Image.
Echo [i] Ensure that you have Admin rights to create the directory.
Echo [i] Ensure that the directory name does not contains illegal characters.
Echo+
Pause&Exit
)
Goto:EOF


:Mount
:: Mount the Windows Image into the directory.
Echo+
Echo [+] Mounting WIM Image...
If /I "%MountImage%" EQU "True" (

"%Dism%" /Mount-Image /ImageFile:"%WindowsImage%" /Index:"%ImageIndex%" /MountDir:"%MountDir%" /English /LogPath:"%DismLogfile%" /LogLevel:"%DismLogLevel%" || (
Echo+
Echo [x] Error mounting the Image Index "%ImageIndex%" in "%MountDir%"
Echo [i] Ensure that the Image Index exist.
Echo [i] Ensure that you have Admin rights to write inside the directory.
Echo [i] Ensure that the directory is not already mounted.
Echo [i] Ensure that the directory is fully empty.
Echo+
REM Try to unmount the failed image and discard changes.
If /I "%UnmountImage%" EQU "True" (
Echo [+] Unmounting and discarding changes...
"%Dism%" /Unmount-WIM /Discard /MountDir:"%MountDir%"
)
Pause&Exit
)

)
Goto:EOF


:CleanUp
Echo [+] CleaningUp WIM Image...
"%Dism%" /CleanUp-Image /StartComponentCleanUp /Image:"%MountDir%" /English /LogPath:"%DismLogfile%" /LogLevel:"%DismLogLevel%" /Quiet
Echo+
Goto:EOF


:Unmount
:: Unmount the Windows Image commiting changes.
Echo [+] Unmounting WIM Image...
If /I "%UnmountImage%" EQU "True" (
"%Dism%" /Unmount-WIM /Commit /MountDir:"%MountDir%" /English /LogPath:"%DismLogfile%" /LogLevel:"%DismLogLevel%"
)
Goto:EOF


:Rebuild
If /I "%Rebuild%" EQU "False" (Goto:EOF)

Echo+
Echo [+] Rebuilding WIM image...

("%ImageX%" /Compress Maximum /Export "%WindowsImage%" "*" "%TEMP%\Rebuilt Image.wim" /Check) && (
Del /Q /F "%WindowsImage%" 1>NUL
Move /Y "%TEMP%\Rebuilt Image.wim" "%WindowsImage%" 1>NUL
) || (
Echo [x] Error rebuilding Image "%WindowsImage%" to "%TEMP%\Rebuilt Image.wim".
)

Goto :EOF


:StartTimer
Set "StartingDate=%Date%"
Set "StarttingTime=%Time:~0,-3%"
Goto :EOF


:StopTimer
(
Echo Minutes = DateDiff^("n", "%StartingDate% %StarttingTime%", Now^)
Echo WScript.Echo Minutes ^& " Minutes"
)>"%TEMP%\%~n0 MinuteDiff.vbs"

For /F "Tokens=*" %%# In (
'Cscript.exe /Nologo "%TEMP%\%~n0 MinuteDiff.vbs"'
) Do (
Set "ElapsedTime=%%#"
)

Goto:EOF


:WriteConvertBytesScript
If Exist "%TEMP%\%~n0 Convert Bytes.vbs" (Goto:EOF)
(
Echo Suffix = Array^("Bytes", "KB", "MB", "GB", "TB"^)
Echo+
Echo Function Convert^(Bytes^)
Echo Convert = convert0^(Bytes, 0^)
Echo End Function
Echo+
Echo Function Convert0^(Bytes, Level^)
Echo If Bytes ^>= 1024 Then
Echo Bytes = Round^(Bytes / 1024, 2^)
Echo Level = Level + 1
Echo Convert0 = Convert0^(Bytes, Level^)
Echo End If
Echo Convert0 = Bytes ^& " " ^& Suffix^(Level^)
Echo End Function
Echo+
Echo WScript.Echo Convert^(WScript.Arguments^(0^)^)
)>"%TEMP%\%~n0 Convert Bytes.vbs"
Goto:EOF


:GetWIMSizeBefore
Call :WriteConvertBytesScript
For %%# in ("%WindowsImage%") Do (Set "WIMSizeBefore=%%~z#")
For /F "Tokens=*" %%# In (
'Cscript.exe /Nologo "%TEMP%\%~n0 Convert Bytes.vbs" "%WIMSizeBefore%"'
) Do (
Set "WIMSizeBefore=%%#"
)
Del /Q "%TEMP%\%~n0 Convert Bytes.vbs"
Goto:EOF


:GetWIMSizeAfter
Call :WriteConvertBytesScript
For %%# in ("%WindowsImage%") Do (Set "WIMSizeAfter=%%~z#")
For /F "Tokens=*" %%# In (
'Cscript.exe /Nologo "%TEMP%\%~n0 Convert Bytes.vbs" "%WIMSizeAfter%"'
) Do (
Set "WIMSizeAfter=%%#"
)
Del /Q "%TEMP%\%~n0 Convert Bytes.vbs"
Goto:EOF


:TakeOwn
(
Takeown.exe /F "%~f1"
ICacls.exe "%~f1" /Grant "%UserName%":"F"
Attrib.exe -R -A -S -H -I "%~f1"
)>NUL || (Exit /B 1)
Goto:EOF


:ParseResources
Set /A "ResourceSuccessCount=0"
Set /A "ResourceFailsCount=0"
Set /A "ResourceNotFoundCount=0"
Set /A "FilesAddedCount=0"
Set /A "FilesFailsCount=0"
Set /A "FilesNotFoundCount=0"

For /F "UseBackQ Tokens=1,2,* Delims=;" %%a In ("%ResourceList%") Do (

If /I "%%~xa" EQU ".dll" (Call :ReplaceResource "%MountDir%\%%~a" "%%~b" "%%~c")
If /I "%%~xa" NEQ ".dll" (Call :CopyFile        "%MountDir%\%%~a" "%%~b")

)
Goto:EOF


:CopyFile
If Not Exist "%~2" (
Echo Can't locate file to copy from: "%~2">>"%LogFile%"
Set /A "FilesNotFoundCount+=1"
Goto:EOF
)

Call :TakeOwn "%~f1" || (
Echo Unable to grant file permissions in file: "%~1">>"%Logfile%"
Set /A "FilesFailsCount+=1"
Goto:EOF
)

Echo [o] Copying "%~2" to "%~1"
Copy /Y "%~f2" "%~f1" 1>NUL || (
Echo Unable to copy file "%~2">>"%Logfile%"
Set /A "FilesFailsCount+=1"
Goto:EOF
)
Set /A "FilesAddedCount+=1"
Goto:EOF


:ReplaceResource
If Not Exist "%~f3" (
Echo Can't locate the resource to copy from: "%~3">>"%LogFile%"
Set /A "ResourceNotFoundCount+=1"
Goto:EOF
)

If Not Exist "%~f1" (
Echo Can't locate the resource file to edit "%~1">>"%Logfile%"
Set /A "ResourceNotFoundCount+=1"
Goto:EOF
)

Call :TakeOwn "%~f1" || (
Echo Unable to grant file permissions in file: "%~1">>"%Logfile%"
Set /A "ResourceFailsCount+=1"
Goto:EOF
)

Echo [o] Replacing resource with name "%~2" in resource file "%~f1", with "%~3".

If /I "%~x3" EQU ".bmp" (
Start /W "Anolis" "%Anolis%" -op:upd -src:"%~f1" -type:Bitmap -name:"%~2" -lang:1033 -file:"%~f3")
)
If /I "%~x3" EQU ".ico" (
Start /W "Anolis" "%Anolis%" -op:upd -src:"%~f1" -type:Icon -name:"%~2" -lang:1033 -file:"%~f3")
)
Set /A "ResourceSuccessCount+=1"
Goto:EOF



REM ====================
REM File Error Controls:
REM ====================

:CheckErrors
:: WIM File Error-Check.
If /I "MountImage" EQU "True" If Not Exist "%WindowsImage%" (
Echo [x] Windows Image not found at: "%WindowsImage%" | MORE
Pause&Exit
)

:: ResourceList File Error-Check.
If Not Exist "%ResourceList%" (
Echo [x] Resource-List file not found in: "%ResourceList%" | MORE
Pause&Exit
)

:: 'DISM.exe' File Error-Check.
If Not Exist "%DISM%" (
Echo [x] 'DISM' process not found at: "%DISM%" | MORE
Pause&Exit
)

:: 'ImageX.exe' File Error-Check.
If Not Exist "%ImageX%" (
Echo [x] 'ImageX' process not found at: "%ImageX%" | MORE
Pause&Exit
)

:: 'MountImage' Value check.
If /I "%MountImage%" NEQ "True" If /I "%MountImage%" NEQ "False" (
Echo [x] Error parsing parameter 'MountImage',
Echo     value '%MountImage%' is not a Boolean value.
Pause&Exit
)

:: 'UnmountImage' Value check.
If /I "%UnmountImage%" NEQ "True" If /I "%UnmountImage%" NEQ "False" (
Echo [x] Error parsing parameter 'UnmountImage',
Echo     value '%UnmountImage%' is not a Boolean value.
Pause&Exit
)

:: 'Rebuild' Value check.
If /I "%Rebuild%" NEQ "True" If /I "%Rebuild%" NEQ "False" (
Echo [x] Error parsing parameter 'Rebuild',
Echo     value '%Rebuild%' is not a Boolean value.
Pause&Exit
)

:: 'CleanUp' Value check.
If /I "%CleanUp%" NEQ "True" If /I "%CleanUp%" NEQ "False" (
Echo [x] Error parsing parameter 'CleanUp',
Echo     value '%CleanUp%' is not a Boolean value.
Pause&Exit
)

:: 'DismLogLevel' Value check.
Echo "%DismLogLevel%"| Findstr.exe "^\"[^1-4]\"$" 1>NUL 2>&1 || (
Echo [x] Error parsing parameter 'DismLogLevel',
Echo     value '%DismLogLevel%' is not in range '1-4'.
Pause&Exit
)

:: Anolis Resourcer File Error-Check.
If Not Exist "%Anolis%" (
Echo [x] Anolis process not found in: "%Anolis%" | MORE
Pause&Exit
)

Goto:EOF