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
Anonymous
February 06, 2008
The comment has been removedAnonymous
February 09, 2008
@Deathtospam It's not actually a VBScript. It's a DOS batch file. Save it as "enumvdirs.bat" Worked great for me. Thanks David.Anonymous
March 14, 2008
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 :D 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!Anonymous
March 17, 2008
The comment has been removedAnonymous
April 29, 2008
How can we include URLs also in the output? Regards, JunuAnonymous
April 30, 2008
The comment has been removedAnonymous
June 03, 2008
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 JasAnonymous
June 20, 2008
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.Anonymous
November 12, 2008
Thanks man, this saved me a ton of time today.Anonymous
April 27, 2010
Thanks, I was looking for something quick that worked with IIS5 and this did the trick.Anonymous
June 20, 2010
Hi, Can i restrict directories enumerationAnonymous
February 07, 2012
Hello, It is excellent to list the Web Sites but what about the Virtual Directories? Thanks, DOm