Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VBScript

How to List all Websites in IIS

3.33/5 (3 votes)
23 Mar 2010CPOL1 min read 1  
How to List all Websites in IIS

It's been a while since I posted something, that is because my laptop gave up on me. Anyways, I'm back in action but still no laptop… I was tasked to get to list all websites that we had on the organization as we are implementing an Enterprise Architecture tool which needs to be populated (the EA tool is called Troux). Now my dilemma is that I don't have anything on the desktop I have now, just a standard installation of OS and that's it! So what's the best way to achieve the solution?

Aha!!!! Use the old school style VBS scripting.

So how do I achieve that? First is that you should know what your servers are and list them on a spreadsheet. In my case, it's good as we have it already pre-populated on the EA tool that we have, so I'll just export it. So I have to create a code that will loop though that spreadsheet and check what websites are in there. Here is how I do it.

VBScript
Set oExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
 On Error GoTo 0
 Wscript.Echo "You need to install an Excel Application"
 Wscript.Quit
End If
On Error GoTo 0

sExcelPath = "C:\Scripts\SERVERS-new.xls"

' Open Spreadsheet and Use First Worksheet.
oExcel.WorkBooks.Open sExcelPath
Set objSheet = oExcel.ActiveWorkbook.Worksheets(1)

' Loop through all the items in Speadsheet, 1st row is a Header Row
intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
 strServerName = objSheet.Cells(intRow, 1).Value
 On Error Resume Next
 ' This takes care if you don't have IIS installed on the server

 Dim oW3SVC, oWebSite

 'Get the IIS Server Object
 Set oW3SVC = GetObject("IIS://" & strServerName & "/W3SVC")
 If (Err <> 0) Then

 Else
 For Each oWebSite In oW3SVC
 If oWebSite.class = "IIsWebServer" Then

 Set oFile = CreateObject("Scripting.FileSystemObject")
 Set oTextFile = oFile.OpenTextFile("C:\Scripts\IIS.txt", 8, True)
 'Get the Name of the Website
 oTextFile.WriteLine(strServerName & "," & oWebSite.ServerComment)
 oTextFile.Close

 Set oTextFile = Nothing
 Set oFile = Nothing

 End If
 Next
 End If
 intRow = intRow + 1
Loop

' Close Workbook and Excel.
oExcel.ActiveWorkbook.Close
oExcel.Application.Quit

' Clean up.
Set oExcel = Nothing
Set objSheet = Nothing
Set objUser = Nothing
Set oW3SVC = Nothing
Set oWebSite = Nothing
Wscript.Echo "Done"

Save it as [Filename].vbs and click the file, then get your results. Simple yet effective, especially when you have more than 200 websites on your organization, it saves you time jotting them down and you never know you might discover new websites.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)