HOWTO: Search and Replace any IIS metabase property value automatically


I found this interesting question on the newsgroup, and came up with a fast code example to illustrate how much one can do with simple batch scripting and existing IIS commandline tools.


Question:


I know in IIS6 you can do a Search and Replace quite easily on the XML-based metabase, but since IIS versions below 6.0 do not have an XML-based metabase, does anyone have or know of any VBscript apps that can do a search and replace on the IIS5 metabase. I've looked at everything through MetaBase Editor but you can't do a search and replace in Metabase Editor, only a search.


I've looked around and haven't found anything specific to this need. We have hundreds of vir redirects changes and IP changes on many load-balanced servers and doing a Search and Replace on the metabase is the best option. BTW, upgrading to 6.0 and then doing it within the XML-based metabase is not an option.


If anyone can provide the script to do this in IIS5 or point me in the right direction, I'd appreciate it.


Thanks in Advance!


Answer:


While the metabase prior to IIS6 is not text, this does not mean that search-and-replace is not possible. The following batch script shows how to do search-and-replace that works on IIS4, IIS5, IIS5.1, and IIS6.

The key pieces of information you must provide are:


  • SET CMD_ADSUTIL - must point to adsutil.vbs launched by cscript
  • SET PROPERTY - this is the property whose value you want to search-and-replace
  • SET NEW_VALUE=%ORIGINAL_VALUE:D:=Z:% - currently it changes all instances of D: in ORIGINAL_VALUE to Z:
  • Remove "ECHO" from the following line to allow code execution:
    ECHO     %CMD_ADSUTIL% SET %METABASE_PATH% %NEW_VALUE%

Enjoy.


//David

@ECHO OFF

SETLOCAL
SET CMD_ADSUTIL=CSCRIPT %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs
SET PROPERTY=Path

FOR /F "usebackq tokens=*" %%I IN (`%CMD_ADSUTIL% FIND /W3SVC/%PROPERTY%`) DO (
FOR /F "usebackq tokens=1,3,*" %%J IN (`%CMD_ADSUTIL% GET %%I/%PROPERTY%`) DO (
IF /I ?%%J? EQU ?%PROPERTY%? (
CALL :Replace "%%I/%%J" %%L
)
)
)

ENDLOCAL
GOTO :EOF

:Replace
SETLOCAL
SET METABASE_PATH=%1
SET ORIGINAL_VALUE=%2
SET NEW_VALUE=%ORIGINAL_VALUE:D:=Z:%

ECHO [%METABASE_PATH%] %ORIGINAL_VALUE% -^> %NEW_VALUE%
ECHO %CMD_ADSUTIL% SET %METABASE_PATH% %NEW_VALUE%

ENDLOCAL
GOTO :EOF

Comments (5)

  1. Calvin says:

    I seem to have issues when using this with a value that has spaces. Any suggestions on why this would be?

  2. David.Wang says:

    You can debug things yourself by removing the @ECHO OFF statement and seeing what is problematic with the actual command that uses your value with spaces.

    Otherwise, please do give the exact values of what you are using as inputs into the script as well as output/metabase values that give problems.

    I currently do not see any problems searching nor replacing with values that contain spaces.

    //David

  3. laxmibai says:

    thank Q for clearing the doubt

  4. Alan Levine says:

    This does not appear to work in IIS 6.0.  The batch is picking up banner information from the Windows scripting host even though I set it to batch mode.

    C:temp>REM @ECHO OFF

    C:temp>SETLOCAL

    C:temp>SET CMD_ADSUTIL=CSCRIPT C:inetpubadminscriptsadsutil.vbs

    C:temp>SET PROPERTY=HTTPREDIRECT

    C:temp>ECHO Wed 12/19/2007 13:11:08.92  1>c:tempmetabase.txt

    C:temp>FOR /F "usebackq tokens=*" %I IN (`CSCRIPT C:inetpubadminscriptsad

    il.vbs FIND /W3SVC/HTTPREDIRECT`) DO (FOR /F "usebackq tokens=1,3,*" %J IN (`

    RIPT C:inetpubadminscriptsadsutil.vbs GET %I/HTTPREDIRECT`) DO (IF /I ?%J?

    U ?HTTPREDIRECT? (CALL :Replace "%I/%J" %L ) ) )

    C:temp>(FOR /F "usebackq tokens=1,3,*" %J IN (`CSCRIPT C:inetpubadminscrip

    adsutil.vbs GET Microsoft (R) Windows Script Host Version 5.6/HTTPREDIRECT`)

    (IF /I ?%J? EQU ?HTTPREDIRECT? (CALL :Replace "Microsoft (R) Windows Script H

    Version 5.6/%J" %L ) ) )

    C:temp>(IF /I ?Microsoft? EQU ?HTTPREDIRECT? (CALL :Replace "Microsoft (R) W

    ows Script Host Version 5.6/Microsoft" Script Host Version 5.6 ) )

    C:temp>(IF /I ?Copyright? EQU ?HTTPREDIRECT? (CALL :Replace "Microsoft (R) W

    ows Script Host Version 5.6/Copyright" Corporation 1996-2001. All rights rese

    d. ) )

    C:temp>(IF /I ?Input? EQU ?HTTPREDIRECT? (CALL :Replace "Microsoft (R) Windo

    Script Host Version 5.6/Input" not find script file "C:inetpubadminscripts

    util.vbs". ) )

    C:temp>(FOR /F "usebackq tokens=1,3,*" %J IN (`CSCRIPT C:inetpubadminscrip

    adsutil.vbs GET Copyright (C) Microsoft Corporation 1996-2001. All rights res

    ed./HTTPREDIRECT`) DO (IF /I ?%J? EQU ?HTTPREDIRECT? (CALL :Replace "Copyrigh

    C) Microsoft Corporation 1996-2001. All rights reserved./%J" %L ) ) )

    C:temp>(IF /I ?Microsoft? EQU ?HTTPREDIRECT? (CALL :Replace "Copyright (C) M

    osoft Corporation 1996-2001. All rights reserved./Microsoft" Script Host Vers

    5.6 ) )

    C:temp>(IF /I ?Copyright? EQU ?HTTPREDIRECT? (CALL :Replace "Copyright (C) M

    osoft Corporation 1996-2001. All rights reserved./Copyright" Corporation 1996

    01. All rights reserved. ) )

    C:temp>(IF /I ?Input? EQU ?HTTPREDIRECT? (CALL :Replace "Copyright (C) Micro

    t Corporation 1996-2001. All rights reserved./Input" not find script file "C:

    etpubadminscriptsadsutil.vbs". ) )

    C:temp>(FOR /F "usebackq tokens=1,3,*" %J IN (`CSCRIPT C:inetpubadminscrip

    adsutil.vbs GET Input Error: Can not find script file "C:inetpubadminscript

    dsutil.vbs"./HTTPREDIRECT`) DO (IF /I ?%J? EQU ?HTTPREDIRECT? (CALL :Replace

    put Error: Can not find script file "C:inetpubadminscriptsadsutil.vbs"./%J

    L ) ) )

    C:temp>(IF /I ?Microsoft? EQU ?HTTPREDIRECT? (CALL :Replace "Input Error: Ca

    ot find script file "C:inetpubadminscriptsadsutil.vbs"./Microsoft" Script

    t Version 5.6 ) )

    C:temp>(IF /I ?Copyright? EQU ?HTTPREDIRECT? (CALL :Replace "Input Error: Ca

    ot find script file "C:inetpubadminscriptsadsutil.vbs"./Copyright" Corpora

    n 1996-2001. All rights reserved. ) )

    C:temp>(IF /I ?Input? EQU ?HTTPREDIRECT? (CALL :Replace "Input Error: Can no

    ind script file "C:inetpubadminscriptsadsutil.vbs"./Input" not find script

    le "C:inetpubadminscriptsadsutil.vbs". ) )

    C:temp>ENDLOCAL

    C:temp>GOTO :EOF

  5. Alan Levine says:

    DISREGARD MY LAST POST.  

    The issue is with the location of ADSUTIL.VBS.  

    My bad!!

Skip to main content