Link to home
Start Free TrialLog in
Avatar of Darth_helge
Darth_helge

asked on

Create website programmatically iis 6

how do i create a website programmatically in iis 6?
i am using vs 2005

is there any easy way to do this?

Here is something i want to configure on creation

website folder
description
application pool
asp.net version
host header
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


You can do all of that using the ADSI interface. There is only one slight problem with me describing it for you though, I don't do anything with VB, only VBScript.

Still, much of it is translatable sort of, I have some vague notes on C# versions of some of this somewhere, but that's probably not much help either.

That said... In some sort of order...

' Bindings (Host Header) get loaded into a collection initially, they're easy enough to alter later on but we
' need at least one to setup the site:

colBindings = Array(0)
Set colBindings(0) = objProvider.Get("ServerBinding").SpawnInstance_()
colBindings(0).IP = SERVER_IP
colBindings(0).Port = "80"
colBindings(0).Hostname = "www.test.com"

Set objProvider = objLocator.ConnectServer("<Server Name>", "root/MicrosoftIISv2")
Set objService = objProvider.Get("IIsWebService='W3SVC'")

strSiteObjectPath = objService.CreateNewSite("Site Description / Server Comment", colBindings, "C:\Path\To\Site")

' We're going to get the Metabase Path for the site from the string returned by the CreateNewSite method.
' Required to play around with some of it's settings.

Set objPath = CreateObject("WbemScripting.SWbemObjectPath")
objPath.Path = strSiteObjectPath
strSitePath = objPath.Keys.Item("")

' Re-connect to the site and set the properties

Set objSite = objProvider.Get("IIsWebServerSetting='" & strSitePath & "'")
Set objVirtualDirectory = objProvider.Get("IIsWebVirtualDirSetting='" & strSitePath & "/ROOT'")

' Application Pool Creation

Set objAppPools = GetObject("IIS://<Server Name>/W3SVC/AppPools")
Set objAppPool = objAppPools.Create("IIsApplicationPool", "<App Pool Name>")
Set objAppPool = Nothing
Set objAppPools = Nothing

' Assign the Pool to the Site

objVirtualDirectory.AppFriendlyName = strServerName
objVirtualDirectory.AppPoolID = strServerName
objVirtualDirectory.SetInfo

' If you need to rewrite Bindings they can be treated as an Array but it has to be done via ADSI not
' WMI / WBEM

objSite = GetObject("IIS://<Server Name>/" & strSitePath)
arrBindings = objSite.ServerBindings

' You now have an array of lines like this:
' SERVER_IP:HTTP_PORT:Domain_Name
' i.e. 192.168.1.1:80:www.test2.com
' If you want to add more just add them to the array and load it back in:

objSite.ServerBindings = arrBindings
objSite.SetInfo

This bit:

You can do all of that using the ADSI interface.

Should have said:

You can do all of that using the ADSI and WMI interfaces.
Avatar of Darth_helge
Darth_helge

ASKER

thank you but how do you choose which asp.net version to use? i can't see that in your code

Oh sorry, forgot to mention that. They only brought that in with ASP.Net 2.0, I just haven't had a chance to look where it's stored yet. I'll have a dig around the metabase this afternoon, doesn't matter if I break it on my machine ;)

Chris
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Oh and changing the entries in that array does make the change visible in the GUI, so it's nice and accessible afterwards.

Chris
thanks Chris-Dent. I will try this probably on Monday :=)

Great, let me know how you get on :)

Chris