Wednesday, March 14, 2007

Programmatic IIS Administration - Selecting a Site

Configuring a web server can be complicated, particularly if it is running multiple web sites, with multiple virtual directories under each site. Such a complex IIS tree requires many, many mouse clicks in order to configure according to specifications.

And what happens if the server catches on fire? It may be a good idea to script the configuration. VBScript and the IIS ADSI objects offer a multitude of possibilities.

Here is a simple VBscript function for selecting a site by name (which you can then configure with proper directory security, isolation level, host headers, etc...)

Function GetWebSiteByName(WebSiteName)
dim rootObj,count,website
set rootObj=GetObject("IIS://LocalHost/W3SVC")

for each website in rootObj
if LCase(website.class) = LCase("IIsWebServer") then
if LCase(website.ServerComment)=LCase(WebSiteName) then
set GetWebSiteByName=website
exit function
end if
end if
next

end Function

Refer back for more Automated IIS Administration examples, using low-tech script :)

No comments: