Skip to content

Instantly share code, notes, and snippets.

@rip747
Created August 6, 2010 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rip747/511689 to your computer and use it in GitHub Desktop.
Save rip747/511689 to your computer and use it in GitHub Desktop.
' This script will add a host header to an existing site in IIS6
' First save this script to a file
' Then run simply by calling the script from the command line passing in the arguments:
' 1) the Site Name in IIS (can be found under the description header)
' 2) the IPAddress of the host header you
' 3) the Port number for the host header (usually 80)
' 4) the full qualified host (domain) name
' EXAMPLE: wscript addHostHeader.vbs mytestsite 127.0.0.1 80 site3.example.com
Const IISService = "IIS://localhost/w3svc"
Const ADS_PROPERTY_APPEND = 3
Dim msg, siteID, siteTitle, ip, port, host
Set ArgObj = WScript.Arguments
If ArgObj.Count < 4 Then
msg = "You must pass in the following 4 arguments:" & VBCrlf
msg = msg & "Site Name in IIS" & VBCrlf
msg = msg & "Ipaddress" & VBCrlf
msg = msg & "Port" & VBCrlf
msg = msg & "Host" & VBCrlf
Wscript.Echo msg
WScript.quit
End If
' store the arguments
siteTitle = ArgObj(0)
ip = ArgObj(1)
port = ArgObj(2)
host = ArgObj(3)
Set ArgObj = Nothing
siteID = FindSite(siteTitle)
If (siteID = "") Then
WScript.Echo "cannot find site."
WScript.Quit
End If
' add the host header
AddHostHeader siteID, ip, port, host
Function AddHostHeader(siteID, ip, port, host)
Dim pathing, bindings, hostheader, a
hostheader = ip & ":" & port & ":" & host
pathing = IISService & "/" & siteID
Set objWebsite = getObject(pathing)
bindings = objWebsite.ServerBindings
If (CheckForHost(host, bindings) = true) Then
WScript.Echo "host header already exists"
WScript.Quit 0
End If
a = UBound(bindings)
a = a + 1
ReDim Preserve bindings(a)
bindings(a) = hostheader
objWebSite.Put "ServerBindings", bindings
objWebSite.SetInfo
End Function
Function GetDescription(SiteNumber)
Dim pathing
pathing = IISService & "/" & SiteNumber
Set IISWebSite = getObject(pathing)
Set IISWebSiteRoot = getObject(pathing & "/root")
GetDescription = IISWebSite.ServerComment
Set IISWebSiteRoot = nothing
Set IISWebSite = Nothing
End Function
Function FindSite(Title)
FindSite = ""
Set IISOBJ = getObject(IISService)
for each Web in IISOBJ
if (Web.Class = "IIsWebServer") then
If (GetDescription(Web.Name) = Title) Then
FindSite = Web.name
End If
end if
next
Set IISOBj=Nothing
End Function
Function CheckForHost(host, bindings)
Dim a, b
CheckForHost = False
' check to see if the host exists
For Each a In bindings
b = Split(a, ":")
If (b(2) = host) Then
CheckForHost = True
Exit Function
End If
Next
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment