Here is 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 displays a list of virtual directory identifiers, as well as a "friendly name" and a path for each, for example:
/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 transfer the output to a text file, first make sure that it does not exist, and then add >> filename.txt to the command above. (ex: DEL sites.txt & ... >> sites.txt )
Here's a breakdown of how the confusing command works:
@ has a prefix for each statement to avoid repeating the statement itself, which would pollute the output.
@FOR /F "delims=[]" %A IN ('@cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs ENUM /P /w3svc') DO
Calls AdsUtil.vbs , which is installed with IIS6 (and reads the metabase on our behalf.
- The
ENUM /P /w3svc tells him to list all sites and virtual directory identifiers, starting with the root of the node. - The nologo switch suppresses the normal CScript copyright preamble to display only the result of interest to us. A double backslash is used to escape the slash character, since we are inside the string.
The output of the part 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 to FOR /F , which goes through each line. delims=[] says FOR to treat square brackets as delimiters. Everything after DO will be executed once for each line, and the %A variable is set to everything between the square brackets. (If it was a batch file, you would use %%A ).
@FOR /F delims^=^"^ tokens^=2 %B IN ('@cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/ServerComment') DO
This second FOR block starts AdsUtil with the GET parameter to get the ServerComment property for this site / virtual directory. This is a human-friendly name, as shown in IIS. Unfortunately, the result is a little more difficult to parse. For example, for /w3svc/1 you will return:
ServerComment : (STRING) "Default Web Site"
touching trick parses text between quotes.
Please note that nodes that we are not interested in (AppPools, Filters and Info) do not have the ServerComment property and give the result without quotes, for example:
The path requested could not be found. ErrNumber: -2147024893 (0x80070003) Error Trying To GET the Object (GetObject Failed): w3svc/Filters
Thus, the rest of the command line is not invoked for them.
@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 extracts the physical path, and then displays all three pieces of the analyzed information on the console.
source share