With IIS7 there is an easy API to work with IIS entries. But if you still have a server running on IIS6 and manage allot of virtual sites then it does come to a point where manually adding the sites becomes. .. tedious.
Many examples demonstrate how to create the IIS entries and a new Virtual Directory. In my case I already have directories created, using another automated process for a staging domain that points to the root folder containing all my sites.
So, I just want to add a new IIS entry with the correct permissions and accept domain connections on the specific name directly to the root of an existing directory.
For reference it is good to look at this page. But it was still pretty difficult to understand exactly how to set properties and invoke actions. End of the day.. or two... a few lines of code does the trick :-)
Many examples demonstrate how to create the IIS entries and a new Virtual Directory. In my case I already have directories created, using another automated process for a staging domain that points to the root folder containing all my sites.
So, I just want to add a new IIS entry with the correct permissions and accept domain connections on the specific name directly to the root of an existing directory.
For reference it is good to look at this page. But it was still pretty difficult to understand exactly how to set properties and invoke actions. End of the day.. or two... a few lines of code does the trick :-)
'''''' Create a New Site in IIS ''' ''' "domainName">Domain name and domain without http or www. ie 'kula.org.uk' ''' "homeDirectory">Direcotry on server of containing folder '''An IIS id for reference ''' Public Shared Function CreateWebsite(ByVal domainName As String, ByVal homeDirectory As String) As String Dim serverBindings As Object() = New Object(1) {} serverBindings(0) = ":80:" & domainName serverBindings(1) = ":80:" & "www." & domainName Dim IISDomainName As String = Char.ToUpper(domainName(0)) + domainName.Split(CChar("."))(0).Substring(1) & "SuperSite" 'I have imnpersonate setup in my web.config - Administrator rights are requried. Dim w3svc As New DirectoryEntry("IIS://localhost/W3SVC") Try ''http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/37f0867a-54bb-44f1-bdb0-5c8090dde519.mspx?mfr=true 'Create new Site in IIS with default configuration Dim newSite As Object() = New Object() {IISDomainName, serverBindings, homeDirectory} Dim newSiteId As Object = w3svc.Invoke("CreateNewSite", newSite) 'Use the new sites ID and configure as neede 'Impersonate does not seem to apply here? So just supply password again for admin account. w3svc = New DirectoryEntry("IIS://localhost/W3SVC/" & newSiteId.ToString, "username", "password!") w3svc.Properties("EnableDefaultDoc")(0) = True w3svc.Properties("DefaultDoc").Value = "home.aspx,adminhome.aspx" w3svc.Properties("AppFriendlyName")(0) = "MySite" w3svc.Properties("AppIsolated")(0) = 2 w3svc.Properties("AccessRead")(0) = True w3svc.Properties("AccessWrite")(0) = False w3svc.Properties("AccessScript")(0) = True w3svc.Properties("AccessFlags").Value = 513 w3svc.Properties("AppPoolId").Value = "mySitePoolName" w3svc.Properties("AuthNTLM")(0) = True w3svc.Properties("AuthAnonymous")(0) = True w3svc.Properties("ServerAutoStart")(0) = True w3svc.CommitChanges() 'Start the site w3svc.Invoke("Start", Nothing) Catch ex As Exception If ex.ToString.Contains("0x800700B7") Then Return "Website already exists." Throw New Exception("Website already exists!") Else Return ex.ToString Throw New Exception("Exception:" & vbNewLine & ex.ToString) End If End Try Return w3svc.Name End Function
Comments