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