Created
June 13, 2018 12:38
-
-
Save redneck-f25/9cdab842971214b304dbab8d4171ff40 to your computer and use it in GitHub Desktop.
Capture WIM images of all dos-like volumes from VSS snapshot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off & setlocal enableextensions enabledelayedexpansion | |
rem *************************************************************************** | |
rem * * | |
rem * Copyright (C) 2018 BitCtrl Systems GmbH * | |
rem * * | |
rem * Capture WIM images of all dos-like volumes from VSS snapshot. * | |
rem * * | |
rem * @author Daniel Hammerschmidt <[email protected]> * | |
rem * @version 2018-06-13-001 * | |
rem * * | |
rem *************************************************************************** | |
goto __early__ | |
:__config__ | |
goto __main__ | |
:__early__ | |
net session >nul 2>nul || ( | |
where SuRun.exe >nul 2>nul && ( | |
SuRun.exe "%~f0" | |
exit /b | |
) | |
color 4f | |
echo\Need administrative permissions. | |
pause | |
color | |
exit /b 1 | |
) | |
goto __config__ | |
:_snapshot_create | |
set "TAB= " | |
set "SHADOW_COPY_BASE=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy" | |
set "shadow_id=" | |
set "shadow_volume=" | |
set "shadow_drive=%~1" | |
set "link=%~2" | |
>nul mklink /d "%link%" . && rmdir "%link%" || ( | |
echo/Cannot create symlink. | |
exit /b 2 | |
) | |
for /f "usebackq tokens=1,2,* delims=;=" %%a in ( `wmic shadowcopy call create "ClientAccessible"^,"%shadow_drive%\"` ) do ( | |
set "k=%%a" | |
set "k=!k:%TAB%=!" | |
set "k=!k: =!" | |
set "v=%%b" | |
set "v=!v: =!" | |
if "!k!"=="ReturnValue" ( | |
if not "!v!"=="0" ( | |
echo\Cannot create snapshot ^(!k! = !v!^). | |
exit /b 1 | |
) | |
) else if "!k!"=="ShadowID" ( | |
set "shadow_id=!v:"=!" | |
break | |
) | |
) | |
if "%shadow_id"=="" ( | |
echo\Cannot create snapshot. | |
exit /b 1 | |
) | |
for /f "usebackq tokens=1,* delims=:" %%a in ( `vssadmin list shadows "/shadow=%shadow_id%"` ) do ( | |
set "k=%%a" | |
set "k=!k:%TAB%=!" | |
set "k=!k: =!" | |
set "v=%%b" | |
set "v=!v: =!" | |
set "n=!v:%SHADOW_COPY_BASE%=!" | |
if not "!n!"=="!v!" ( | |
set "shadow_volume=%SHADOW_COPY_BASE%!n!" | |
break | |
) | |
) | |
if "%shadow_volume%"=="" ( | |
echo\Cannot find snapshot volume. | |
exit /b 1 | |
) | |
mklink /d "%link%" "%shadow_volume%\" || ( | |
vssadmin delete shadows "/shadow=%shadow_id%" /quiet | |
) | |
exit /b | |
:_snapshot_remove | |
set "shadow_id=" | |
set "shadow_volume=" | |
set "shadow_drive=" | |
set "link=%~1" | |
if "%link:~-1%"=="\" set "link=%link:~0,-1%" | |
set "link_dir=%~dp1" | |
set "link_name=%~nx1" | |
if not exist "%link%" ( | |
echo\File does not exist. | |
exit /b | |
) | |
for /f "usebackq tokens=1,2,3,4,*" %%a in ( `dir "%link_dir%"` ) do ( | |
if "%%c:%%d"=="<SYMLINKD>:%link_name%" ( | |
set "shadow_volume=%%e" | |
rem remove square brackets and trailing backslash | |
set "shadow_volume=!shadow_volume:~1,-2!" | |
) | |
) | |
if "%shadow_volume%"=="" ( | |
echo\File is not a directory symlink. | |
exit /b 1 | |
) | |
for /f "usebackq tokens=1,* delims=:" %%a in ( `vssadmin list shadows` ) do ( | |
set "k=%%a" | |
set "k=!k:%TAB%=!" | |
set "k=!k: =!" | |
set "v=%%b" | |
set "v=!v: =!" | |
if "!v!"=="%shadow_volume%" ( | |
set "shadow_id=!q!" | |
set "shadow_drive=!p:~1,2!" | |
break | |
) | |
set "q=!p!" | |
set "p=!v!" | |
) | |
if "%shadow_id%"=="" ( | |
exit /b 1 | |
) | |
>nul vssadmin delete shadows "/shadow=%shadow_id%" /quiet | |
rmdir "%link%" | |
echo\Done. | |
exit /b | |
:__main__ | |
echo\%DATE% %TIME: =0% | |
for /f "usebackq tokens=1,* delims=:" %%a in (`vssadmin list volumes ^| findstr /v "?" ^| findstr ":"`) do ( | |
for %%c in ( %%b ) do ( | |
if not "%%c"=="%~d0\" ( | |
set drive=%%~dc | |
set snaplink=%~dp0volume_!drive::=.snapshot! | |
set snapimg=%~dp0volume_!drive::=.wim! | |
echo\Capture Volume \\.\!drive! | |
call :_snapshot_create !drive! !snaplink! | |
dism /capture-image /imagefile:!snapimg! /name:!snapimg! /capturedir:!snaplink! | |
call :_snapshot_remove !snaplink! | |
echo\!DATE! !TIME: =0! | |
echo\ | |
) | |
) | |
) | |
echo\Done. | |
pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment