Script to Add Host Header to IIS

I've been asked to add about 100 host header to a site, since I really hate repetitive tasks I tried to use a script....

I tried to use ADSUTIL but I found 2 limitations:
1) it doesn't append to the list, it replaces the list.
2) the command prompt is limited to 9 arguments.

So I decided to write my own vbscript

' ***************************************************************
' * AddHostHeader.vbs By Vittorio Pavesi (www.vittorio.tk) *
' * *
' * Add Host Header to a specific IIS Site *
' ***************************************************************

Dim objWWW, WebSitePath
Dim BindingArray()
Dim i

if Wscript.arguments.count<3 then
Wscript.echo "Script can't run without correct argument"
Wscript.echo "Correct usage: Cscript AddHostHeader.vbs SiteID Port HostHeader"
Wscript.echo "Correct usage: Cscript AddHostHeader.vbs 21 80 www.mySite.com"
Wscript.quit
end if

ID = Wscript.arguments.Item(0)
Port = Wscript.arguments.Item(1)
NewHostHeader = Wscript.arguments.Item(2)

i = 0
Set objWWW = GetObject("IIS://localhost/W3SVC/" & ID)
For each item in objWWW.ServerBindings
redim preserve BindingArray(i)
BindingArray(i) = item
i = i + 1
next

redim preserve BindingArray(i)
BindingArray(i) = ":" & port & ":" & NewHostHeader

objWWW.ServerBindings = BindingArray
objWWW.SetInfo()

wscript.echo "Successfully added binding for: " & port & ":" & ucase(NewHostHeader)
Share on Google Plus

About Vittorio Pavesi

    Blogger Comment
    Facebook Comment

1 commenti:

Anonymous said...

exellent thank you.
just what I needed.
converted your code to vb.net and automated a boring process.