Labels

Wednesday, December 28, 2011

Batch script to extract updates from WSUS content folder

This script will extract/copy particular updates from WSUS content folder. WSUS stores updates in \WSUSContent folder using SHA1 hash for file names and this script will attempt to decrypt SHA1 hash for file names by querying  WSUS SQL/MSDE database and then exports those updates as well.

Command line switches to run the script

SYNTAX:   WSUSExtract.cmd <-f:search_string> [-l:lang] [-t:target]

-f:search_string   - Part of update KB to search for.

-l:lang      - Update language. If omitted, all languages will be processed.

-t:target  - Target folder to copy updates to. If omitted, script assumes 'View Only' mode.

Examples

The command below will Copy downloaded English updates corresponding 896358 KB article to 'c:\temp' folder

WSUSExtract.cmd -f:896358 -l:enu -t:c:\temp

The command below will output/display/View all downloaded updates corresponding to 896358 KB article:

WSUSExtract.cmd  -f:896358

Copy the code below in a text file and name it as 'WSUSExtract.cmd'.

::======== start of script ========

::  Batch script to 'extract' particular updates from WSUS content folder.
:: 
::  The problem with content folder is that WSUS stores updates using SHA1 hash for
::  file names so it is difficult to determine what file belongs to what
::  update. The script determines a human-readable name of update file byquerying 
::  WSUS SQL/MSDE database.
:: 
::  ====================================================================
::  SYNTAX:   WSUSExtract.cmd <-f:search_string> [-l:lang] [-t:target]
:: 
::  -f:search_string - Part of update KB to search for.
:: 
::  -l:lang    - Update language. If omitted, all languages will be processed.
:: 
::  -t:target  - Target folder to copy updates to. If omitted, script assumes 'View Only' mode.
:: 
::  EXAMPLES:
::  Copy downloaded english updates corresponding 896358 KB article to 'c:\temp'folder:
::      WSUSExtract.cmd -f:896358 -l:enu -t:c:\temp
:: 
::  View all downloaded updates corresponding to 896358 KB article:
::      WSUSExtract.cmd  -f:896358
::  ====================================================================
:: 

::*************************************************************************
:: Name:           WSUSExtract.cmd
::
:: Purpose:        Finds and copies particular update(s) from WSUS content folder.
::
:: Syntax:         WSUSExtract.cmd <-f:search_string> [-l:language] [-t:target_folder]
::
:: Version:        1.1
:: Technology:     NT Batch
::
:: Requirements:   Windows 2000+
::                 REG.EXE (in case of Windows 2000, install Support Tools or download REG.EXE from
::
http://www.dynawell.com/reskit/microsoft/win2000/reg.zip)
::                 OSQL.EXE Utility (included in MSDE)
::
:: Authors:        Alexander Suhovey
::
:: History:        08/07/2005 - Database server address is now determined from registry
::                 08/05/2005 - First release.
::*************************************************************************
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
Echo.

::========= OS Check ============
if not "%OS%" == "Windows_NT" goto BADOS
if "%APPDATA%" == "" goto BADOS

::====== Parse command line =====
If "%1"=="" GOTO USAGE
If "%1"=="/?" GOTO USAGE
If "%1"=="-?" GOTO USAGE
:PARSE
set arg=%1
shift /1
If not defined arg GOTO NEXT
If "%arg:~0,3%"=="-f:" (set fstr=%arg:~3%&GOTO PARSE)
If "%arg:~0,3%"=="-l:" (set lang=%arg:~3%&GOTO PARSE)
If "%arg:~0,3%"=="-t:" (set target=%arg:~3%&GOTO PARSE)
Echo ERROR: Unknown argument: %arg%
GOTO USAGE
:NEXT
If defined target (
    set target=%target:"=%\
    If not exist "%target%" echo ERROR: Cannot find target folder: "%target%" & goto :eof
) Else (
    set viewonly=1
    Echo Target folder not defined. Assuming View Only mode.
    echo.
)

::== Find WSUS content folder ===
::=== and SQL server address  ===
set regkey="HKLM\SOFTWARE\Microsoft\Update Services\Server\Setup"
for /f "tokens=2* delims= " %%i in ('reg query %regkey% /v ContentDir ^| find "ContentDir"') do set source=%%j
If not defined source Echo ERROR: Cannot find WSUS content folder in registry. Check if WSUS is installed. & goto :eof
set source=%source%\WsusContent
for /f "tokens=2* delims= " %%i in ('reg query %regkey% /v SqlServerName ^| find "SqlServerName"') do set server=%%j
If not defined server Echo ERROR: Cannot find WSUS database server in registry. Check if WSUS is installed. & goto :eof
set osqlcommand=osql.exe -w 500 -h-1 -E -d SUSDB -S %server%

::====== Test SQL database ======
::========= connection ==========
for /f "delims=" %%i in ('%osqlcommand% -Q') do (
    Echo ERROR: Cannot connect to WSUS SQL server '%server%'
    goto :eof
)

::=== Format osql.exe query =====
set query=select FileDigest,FileName from tbFile
If defined fstr set query=%query% where FileName like '%%%fstr%%%'
If defined lang (
    If defined fstr (set query=!query! and) Else (set query=!query! where)
    set query=!query! FileName like '%%%lang%%%'
)

::======== Main section =========
set num1=0
set num2=0
echo ========================================
for /f "tokens=1,2" %%i in ('%osqlcommand% -Q "%query%" ^| find /v "rows affected"') do (
    set /a num1+=1
    set dstfile=%%j
    set srcfile=%%i
    set srcfilepath="%source%\!srcfile:~-2!\!srcfile:~2!.!dstfile:~-3!"
    set dstfilepath="%target%%%j"
    If exist !srcfilepath! (
        set /a num2+=1
        If defined viewonly (
            echo !dstfile!
        ) Else (
            If exist !dstfilepath! set dstfilepath="%target%%%~nj(Copy !random!)%%~xj"
            set /p foo="Copying !dstfile! ... "<nul
            copy !srcfilepath! !dstfilepath! >nul 2>&1
            If errorlevel 1 (echo FAILED.) Else (echo Done.)
        )
    )
)
echo ========================================
echo Updates found in content folder    : %num2%
echo Total matching updates in database : %num1%
goto :eof

::========= Echo syntax =========
:USAGE
echo.
Echo %~nx0
Echo Finds and copies particular update(s) from WSUS content folder.
Echo Renames updates to human-readable format using information
Echo obtained from local WSUS SQL database.
Echo.
Echo SYNTAX:   %~nx0 ^<-f:search_string^> [-l:lang] [-t:target]
Echo.
Echo -f:search_string - Part of update name to search for.
Echo                    Naming format for OS updates: OS-KBNUMBER-HW-LANG.EXT
Echo                    OS       - Operating system (eg 'WindowsXP')
Echo                    KBNUMBER - MSKB number (e.g 'KB896358')
Echo                    HW       - Hardware technology ('x86', 'ia64' etc)
Echo                    LANG     - OS language (see -l switch below)
Echo                    EXT      - Extention ('exe', 'cab' etc)
Echo -l:lang          - Update language. If omitted, all languages will
Echo                    be processes. Examples are:
Echo                    rus - russian
Echo                    enu - english
Echo                    fra - french
Echo                    deu - deutch
Echo -t:target        - Target folder to copy updates to.
Echo                    If omitted, script assumes 'View Only' mode.
Echo                    Do not use trailing slash.
Echo.
Echo EXAMPLES:
Echo.
Echo Copy downloaded english updates corresponding
Echo to 896358 KB article to 'c:\temp' folder:
Echo.
Echo     %~nx0 -f:896358 -l:enu -t:c:\temp
Echo.
Echo View all downloaded updates corresponding to 896358 KB article:
Echo.
Echo     %~nx0 -f:896358
Echo.
goto :eof

::===== If Bad OS detected ======
:BADOS
echo.
echo ERROR: This script needs Windows 2000 or better.
goto :eof

::======== End of script ========

No comments:

Post a Comment