Question : 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.

Answer : Programmatically create IIS host header using VBScript / ASP?


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

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cde669f1-5714-4159-af95-f334251c8cbd.mspx?mfr=true

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!

HTH

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
' 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 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 = "192.168.1.1:80:somesite.domain.com"

' 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
objWebSite.SetInfo

' Or use the ADS_PROPERTY_APPEND to add an entry to the existing array
Const ADS_PROPERTY_APPEND = 3
objWebSite.PutEx ADS_PROPERTY_APPEND, "ServerBindings", Array(strNewServerBinding)
objWebSite.SetInfo
Random Solutions  
 
programming4us programming4us