[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1668
  • Last Modified:

Programmatically create IIS host header using VBScript / ASP?

I am creating a website that will run on IIS 6 and will have a lot of subdomains.  I wanted to find a method to use wildcard subdomains, but IIS doesn't seem to support this.  I only have one IP address, and host other sites on the same server so I can't dedicate an IP to this site and do the subdomain redirection programmatically.

I have created a wildcard A record in my DNS zone so abc.domain.com, xyz.domain.com, whatever.domain.com, anything.domain.com, etc will all resolve to the IP of my IIS server.

I need to find a way to programmatically add host headers for new subdomains to the site in IIS.  For example, I want to be able to have a setup script that I can use to add subdomains without having to manually add the host headers in IIS.  

It seems like there must be a way to modify the host headers assigned to a particular IIS website using VBScript, but I'm striking out trying to find it.
1 Solution
Chris DentPowerShell DeveloperCommented:

You may find this quite useful if you end up doing this kind of thing on a regular basis:


Host Headers are held in the ServerBindings property, it can be set using both ADSI and WMI. The following is ADSI because the interface is generally simpler.

Something of an untested example below, hopefully it's enough to get you started!


' This represents the site level node in the metabase. Properties for the site are 
' split between this and the "root" virtual directory beneath.
Set objWebSite = GetObject("IIS://ServerName/W3SVC/<SiteID>")
' SiteID is numeric, in IIS 6 it is semi-random. IIS 5 and IIS 7 use incrementing
' IDs. For IIS 6 you'll either need to know this in advance, or need to search 
' the metabase for it. The Default Web Site has SiteID 1.

' To get the current ServerBindings you can call the Get method
arrServerBindings = objWebSite.Get("ServerBindings")
' Also accessible with:
arrServerBindings = objWebSite.ServerBindings

' Each Binding string in the array takes the form:
' IPAddress:PortNumber:HostName
strNewServerBinding = ""

' Either extend the current array (declare a new array, with one extra element 
' and copy existing entries). Then a simply put the new array into the property:

objWebSite.Put "ServerBindings", arrNewServerBindings

' Or use the ADS_PROPERTY_APPEND to add an entry to the existing array
objWebSite.PutEx ADS_PROPERTY_APPEND, "ServerBindings", Array(strNewServerBinding)

Open in new window

FWestonAuthor Commented:
This worked perfectly.  I couldn't get the ADS_PROPERTY_APPEND method to work, but creating a new array and then saving that works just fine.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now