DOS command to list all virtual directories in IIS 6

rkagerer picture rkagerer · Nov 4, 2012 · Viewed 8.4k times · Source

I'm looking for a DOS command to list all sites and virtual directories in IIS 6 under Windows 2003. I know there are ways to do this using Powershell / WMI, VBS, C#, etc. But all I want is a quick and dirty, no-fuss way to do it from DOS, without the need to create any new files on the Webserver.

EDIT: While researching for this question I managed to come up with a one-liner that does it, but please do suggest an alternative if you have a more elegant solution that fits the criteria above.

Answer

rkagerer picture rkagerer · Nov 4, 2012

Here's what I came up with:

@FOR /F "delims=[]" %A IN ('@cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs ENUM /P /w3svc') DO @FOR /F delims^=^"^ tokens^=2 %B IN ('@cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs GET %A/ServerComment') DO @FOR /F delims^=^"^ tokens^=2 %C IN ('@cscript %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/Root/Path') DO @ECHO %A %B "%C"

The command outputs a list of virtual directory ID's, along with the "friendly name" and path for each, e.g.:

/w3svc/1 Default Web Site "c:\inetpub\wwwroot"
/w3svc/1236224994 FunWidgets "C:\Inetpub\wwwroot\FunWidgets"
/w3svc/1359392326 JimSmith.com "C:\Inetpub\wwwroot\JimSmith"
/w3svc/1835917338 BouncyToys "C:\Inetpub\wwwroot\bouncytoys"
/w3svc/198968327 AvalonWest "C:\Inetpub\wwwroot\AvWest"

If you want to pipe the output to a text file, first make sure it doesn't exist, then append >> filename.txt to the command above. (e.g.: DEL sites.txt & ... >> sites.txt)

Here's a breakdown of how the admittedly convoluted command works:

  1. @ is prefixed to every statement to avoid echoing the statement itself, which would pollute the output.

  2. @FOR /F "delims=[]" %A IN ('@cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs ENUM /P /w3svc') DO

    Invokes AdsUtil.vbs, which is installed with IIS6 (and reads the metabase on our behalf).

    • The ENUM /P /w3svc parameter tells it to spit out a list of all sites and virtual directory ID's starting at the root node.
    • The nologo switch suppresses the usual CScript copyright preamble, to render only the output we're interested in. A double-backslash is used to escape the slash character, since we're inside a string.
    • The output of the portion in single quotes resembles the following:

      [/w3svc/1]
      [/w3svc/1236224994]
      [/w3svc/1359392326]
      [/w3svc/1835917338]
      [/w3svc/198968327]
      [/w3svc/AppPools]
      [/w3svc/Filters]
      [/w3svc/Info]
      

    This is passed into FOR /F, which loops through each line. delims=[] tells FOR to treat square brackets as delimiters. Everything after the DO will be executed once for each line, with the %A variable set to whatever is between the square brackets. (If this was a batch file you'd use %%A instead).

  3. @FOR /F delims^=^"^ tokens^=2 %B IN ('@cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/ServerComment') DO

    This second FOR block runs AdsUtil with the GET parameter to retrieve the ServerComment property for the given site / virtual directory. This is the human-friendly name as seen in IIS. Unfortunately the output is a bit trickier to parse. e.g. For /w3svc/1 you get back:

    ServerComment                   : (STRING) "Default Web Site"
    

    The careting trick parses out the text between the quotes.

    Note that nodes we aren't interested (AppPools, Filters and Info) don't have the ServerComment property, and give a result devoid of quotes, e.g.:

    The path requested could not be found.
    ErrNumber: -2147024893 (0x80070003)
    Error Trying To GET the Object (GetObject Failed): w3svc/Filters
    

    Thus the remaining portion of the command line is not invoked for them.

  4. @FOR /F delims^=^"^ tokens^=2 %C IN ('@cscript %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/Root/Path') DO @ECHO %A %B "%C"

    This final FOR retrieves the physical path, then outputs all three pieces of parsed information to the console.