VBScript: IIS Discovery

Find IIS Servers on your domain (or with modifications, your subnet) using this script

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Author: Chrissy LeMaire
' Copyright 2003 NetNerds Consulting Group
' Script is provided AS IS with no warranties or guarantees and assumes no liabilities.
' Website: http://www.netnerds.net
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next

theDomain = "mydomain" ' set this here if you are probing a different domain

Set objNet = CreateObject("WScript.Network")
myFullUsername = trim(objNet.Userdomain & "" & objNet.UserName)
myComputerName = trim(objNet.ComputerName)

if len(theDomain) = 0 then
theDomain = Trim(objNet.Userdomain)
end if
myUsername = Trim(objNet.UserName)
set objNet = nothing

'in case the groups below do not exist

ADMIN = FALSE
Set objGroup = GetObject("WinNT://" & theDomain & "/Domain Admins")
For Each objUser in objGroup.Members
If LCase(myusername) = LCase(objUser.Name) Then
ADMIN = TRUE
End If
Next
Set objGroup = Nothing

Set objGroup = GetObject("WinNT://" & theDomain & "/Enterprise Admins")
For Each objUser in objGroup.Members
If LCase(myusername) = LCase(objUser.Name) Then
ADMIN = TRUE
End If
Next
Set objGroup = Nothing

IIS = FALSE
err.Clear ' to clear any possible errors from above

Set objW3SVC = GetObject("IIS://" & myComputerName & "/W3SVC")
if err.Number = 0 Then
IIS = TRUE
end If

if ADMIN = FALSE Then
MsgBox "You are not a Domain administrator. This script will not work properly."
Wscript.Quit
End If

if IIS = FALSE Then
MsgBox "IIS is not installed. This script will not work properly."
Wscript.Quit
End If
set objW3SVC = Nothing

on error goto 0

startTime = Timer()

'Use the WinNT Directory Services
theDomain = "WinNT://" & theDomain
'Create the Domain object
Set objDomain = GetObject(theDomain)
'Search for Computers in the Domain
objDomain.Filter = Array("Computer")

On error resume next
serverCount = 0
IISServerCount = 0

For Each Computer In objDomain
theServer = Computer.Name
serverCount = ServerCount + 1
Set objW3SVC = GetObject("IIS://" & theServer & "/W3SVC")
Select Case Err.Number
' 70 = permission denied; Strong indication of IIS Server in a DMZ
' 462 = The remote server machine does not exist or is unavailable
' 429 = ActiveX component can't create object
' -2147023169 = CreateObject Failed
' -2147012889  = Name could not be resolved
case  462, 429
thehttpVersion = cint(httpVersion(theServer))
if thehttpVersion > 0 and thehttpVersion < 404 then
IISServerCount = IISServerCount + 1
IISTrue = IISTrue & "IIS Server: " & theServer & VbCrLf
else
IISFalse = IISFalse & "NOT: " & theServer & VbCrLf
end if
case 70,-2147023169,-2147012889
IISUnknown = IISUnknown & "Possibly: " & theServer & VbCrLf
case 0,-2146646000
IISServerCount = IISServerCount + 1
IISTrue = IISTrue & "IIS Server: " & theServer & VbCrLf
set objW3SVC = Nothing
case else
'wscript.echo theServer & ": " & errNumber & ", " & err.Description & "<p>" ' for debugging
end Select

Err.Clear
Next
Set objDomain = Nothing

Function httpVersion(theHost)
On Error Resume Next
Set objxmlHTTP = createobject("MSXML2.ServerxmlHTTP")
theURL= "http://" & theHost
objxmlHTTP.open "GET", theURL, false
objxmlHTTP.send()
tempVersion = objxmlHTTP.getResponseHeader("Server")
If errNumber = -2147012867 Then
NOSERVER = TRUE
Else
NOSERVER = FALSE
End If
set objxmlHTTP = nothing

If instr(tempversion,"Microsoft-IIS/") > 0 then
tempVersion = replace(tempVersion,"Microsoft-IIS/","")
httpVersion = trim(tempVersion)
else
if NOSERVER = TRUE then
httpVersion = "404" ' webserver not found ;)
err.Clear
Else 'there was a webserver there, but probably not an IIS Server
httpVersion = "0"
End If
End If
End Function

finishtime = Timer()
totalTime = finishtime - startTime

myStr = myStr &  "Total Time Taken: " & totalTime & " seconds" &  vbCrLf & vbCrLf
myStr = myStr &  "Total Servers Scanned: " & serverCount & vbCrLf
myStr = myStr &  "Total Servers Found: " & IISServerCount &  vbCrLf & vbCrLf
myStr = myStr &  IISTrue & vbCrLf
myStr = myStr &  IISUnknown & vbCrLf
'myStr = myStr &  IISFalse & vbCrLf
myStr = myStr &  "Done" & vbCrLf

wscript.echo myStr

Requirements: A Windows Domain, local IIS install, domain administrator rights.

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, and holds a number of certifications, including those relating to SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in IIS
One comment on “VBScript: IIS Discovery
  1. Kaushik Patel says:

    I am trying to connect to remote IIS instance using following code:
    SET objIISService = GetObject( “IIS://<>/W3SVC” )

    While above line execute it gives me below error:
    Error Number: 462
    Error Description: The remote server machine does not exist or is unavailable

    However above code is working fine if I disable firewall on remote server. Can anyone suggest what firewall settings I need to change to fix this issue?

Leave a Reply

Your email address will not be published. Required fields are marked *

*