HOWTO: List all Virtual Directories and Paths of a List of Servers


Question:


I need to audit web servers in my domain, and would like to be able to connect to each server, and enumerate the virtual directories — ultimately leading to a link to each web site hosted by the server. Can this code be modified to get that information?


Thanks.


Answer:


Yes, you can modify that code to get this information, but if you just want a list of virtual directories on a server, you don’t need to write any script code to do it. At the end of this blog entry is one way, using a simple batch file, to get this information using ADSUTIL.VBS, a built-in script. Just make sure to provide the right filepath for CMD_ADSUTIL. And of course, the user running the script must have administrator privileges to enumerate the IIS metabase on all required servers.


This batch file accepts one optional input parameter.



  • If you provide no parameter, it will enumerate all vdirs and their respective physical paths of the local computer
  • If you provide a computer name, it will enumerate all vdirs of that computer
  • If you provide a filepath, it will treat each line of the file as a computer name and enumerate all its vdirs

Since I often see this feature requested, I decided to show one simple way to turn a script which takes a server name as input into one that loops through a list of server names stored in a text file, one server name on each line. This should hopefully be illustrative enough of the powerful combination of both VBScript/JScript and Batch script.


Sample usage:

C:\>enumvdirs -?
enumvdirs [servername | file-list]

Where:
servername is the name of the server to query. DAVIDWANG by default
file-list is filepath to text file containing list of servers, one per line

C:\>enumvdirs DAVIDWANG
DAVIDWANG/W3SVC/1/ROOT = “c:\inetpub\wwwroot”
DAVIDWANG/W3SVC/1/ROOT/IISHelp = “c:\windows\help\iishelp”
DAVIDWANG/W3SVC/1/ROOT/Printers = “C:\WINDOWS\web\printers”
DAVIDWANG/W3SVC/1/ROOT/Scripts = “C:\Inetpub\Scripts”

C:\>ECHO %COMPUTERNAME% > ListOfServers.txt

C:\>TYPE ListOfServers.txt
DAVIDWANG

C:\>enumvdirs ListOfServers.txt
DAVIDWANG/W3SVC/1/ROOT = “c:\inetpub\wwwroot”
DAVIDWANG/W3SVC/1/ROOT/IISHelp = “c:\windows\help\iishelp”
DAVIDWANG/W3SVC/1/ROOT/Printers = “C:\WINDOWS\web\printers”
DAVIDWANG/W3SVC/1/ROOT/Scripts = “C:\Inetpub\Scripts”


Enjoy.

@IF NOT DEFINED _ECHO ECHO OFF
SETLOCAL
SET CMD_ADSUTIL=CSCRIPT.EXE //Nologo %SYSTEMDRIVE%\Inetpub\Adminscripts\ADSUTIL.VBS
SET PROPERTY_TO_FIND=Path

SET SERVERS=”%1″
IF ?%1? EQU ?? SET SERVERS=”%COMPUTERNAME%”
IF EXIST %SERVERS% SET SERVERS=%SERVERS:~1,-1%

SET NEED_HELP=%SERVERS:?=%
IF /I “%NEED_HELP%” NEQ “%SERVERS%” GOTO :Help

FOR /F %%A IN ( %SERVERS% ) DO (
FOR /F “usebackq skip=1 tokens=*” %%I IN ( `%CMD_ADSUTIL% FIND %PROPERTY_TO_FIND% -s:%%A` ) DO (
FOR /F “usebackq tokens=3,*” %%J IN ( `%CMD_ADSUTIL% GET %%I/%PROPERTY_TO_FIND% -s:%%A` ) DO (
ECHO %%A/%%I = %%K
)
)
)

ENDLOCAL
GOTO :EOF

:Help
ECHO %0 [servername ^| file-list]
ECHO.
ECHO Where:
ECHO servername is the name of the server to query. %COMPUTERNAME% by default
ECHO file-list is filepath to text file containing list of servers, one per line

GOTO :EOF


//David

Comments (12)

  1. Deathtospam says:

    David —

    I’m not sure how to get this script to work.  I’ve saved all of the blue code in a file called "enumvdirs.vbs", but when I try to call "enumvdirs -?" from a command prompt (Windows XP), it crashes with a VBScript error (invalid character on line 1).  What am I doing wrong?

  2. John says:

    @Deathtospam

    It’s not actually a VBScript. It’s a DOS batch file. Save it as "enumvdirs.bat"

    Worked great for me. Thanks David.

  3. Makhan says:

    Hi David,

    Thanks for that script, but could you please describe the below loop that you are implementing. I dint get the logic entirely. I have used the script though 😀

    FOR /F %%A IN ( %SERVERS% ) DO (

       FOR /F "usebackq skip=1 tokens=*" %%I IN ( `%CMD_ADSUTIL% FIND %PROPERTY_TO_FIND% -s:%%A` ) DO (

           FOR /F "usebackq tokens=3,*" %%J IN ( `%CMD_ADSUTIL% GET %%I/%PROPERTY_TO_FIND% -s:%%A` ) DO (

               ECHO %%A/%%I = %%K

           )

       )

    )

    Thanks!

  4. David.Wang says:

    Makhan – what part are you having trouble understanding?

    It is standard FOR /F  logic — please see the output of FOR /? for the actual syntax to determine what is going on.

    The quickest translation of the logic is:

    For each servername from each line in the filename %SERVERS%

       Use adsutil to look for all instances of the property "Path" on the server and parse out the metabase path

           Retrieve the value of the metabase path and print it out

    The entire magic is based on the premise that the Path metabase property determines a virtual directory mapping. Thus, when you list out all values of that property, you are listing out all virtual directories and their associated path.

    I then added "how to scale an operation over a list of servers" just to illustrate how easy it is to scale an operation with simple, proper tools.

    //David

  5. Junu says:

    How can we include URLs also in the output?

    Regards,

    Junu

  6. David.Wang says:

    Junu – what do you mean by "URL" ?

    The output already includes the URL on the left hand side and the physical path on the right hand side.

    If you want W3SVC/1/ROOT to resolve to something else, use this script to enumerate the websites and associated host headers.

    http://blogs.msdn.com/david.wang/archive/2005/07/13/HOWTO_Enumerate_IIS_Website_Configuration.aspx

    //David

  7. Jas says:

    Hello David,

    Nice Article.

    Is there way to detect if website has been stopped.

    Becuase we have aspx applciation with background threads.

    Applcaition does not shutdown properly if someone expilicitly stop the website.

    Thanks

    Jas

  8. Junu says:

    CScript C:WINDOWSsystem32iisweb.vbs /query

    The above command will display all website and its status.  But I dont know how to include Virtual Directories also in that output.   Any idea?

    Thanks,

    Junu.

  9. JCollum says:

    Thanks man, this saved me a ton of time today.

  10. Garry says:

    Thanks, I was looking for something quick that worked with IIS5 and this did the trick.

  11. Ram Prasad says:

    Hi,

    Can i restrict  directories enumeration

  12. Dom says:

    Hello,

    It is excellent to list the Web Sites but what about the Virtual Directories?

    Thanks,

    DOm