FirstWay:
AppPool Creation:
Private Sub CreateAppPool(ByVal appPoolMetabasePath As String, ByVal appPoolName As String)
Try
Dim newpool As DirectoryEntry
Dim apppools As DirectoryEntry
If appPoolMetabasePath.EndsWith("/W3SVC/AppPools") Then
apppools = Me.GetIISDirectoryEntry(appPoolMetabasePath)
newpool = apppools.Invoke("Create", "IIsApplicationPool", appPoolName)
newpool.CommitChanges()
Else
Throw New Exception("Failed in CreateAppPool(): Application pools can only be created in the */W3SVC/AppPools node.")
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Function To Fetch Next SiteID:
Private Function GetNextSiteId(ByVal objService As DirectoryEntry) As String
Dim strNewSiteId As String = String.Empty
Dim siteIDNum As Integer = 1
For Each e As DirectoryEntry In objService.Children
If e.SchemaClassName = "IIsWebServer" Then
Dim ID As Integer = Convert.ToInt32(e.Name)
If ID >= siteIDNum Then
siteIDNum = ID + 1
End If
End If
Next
strNewSiteId = siteIDNum.ToString()
Return strNewSiteId
End Function
Craete Site and Assign To the AppPool:
Public Sub CreateSite(ByVal localPath As String, ByVal domainName As String, _
ByVal siteName As String, ByVal appPoolName As String)
Dim iisRootDirectory As DirectoryEntry
Dim sites As DirectoryEntries
Dim className As String
Dim websiteIdentificationName As String
Dim iisUnderNT As Boolean = False
Dim newSiteID As String
Dim iisAdminUserName As String
Dim iisAdminPassword As String
Try
iisAdminUserName = Utility.GetAppSettingsValue(Of String)(Utility.AppSettingsKey.IISAdminName)
iisAdminPassword = Utility.GetAppSettingsValue(Of String)(Utility.AppSettingsKey.IISAdminPwd)
' Impersonate the User with Admin User. ImpersonationManager is separete class.
Using impersonationManager As New ImpersonationManager(iisAdminUserName, iisAdminPassword, domainName)
If impersonationManager.impersonateValidUser() Then
iisRootDirectory = Me.GetIISDirectoryEntry("IIS://Localhost/W3SVC")
sites = iisRootDirectory.Children
className = iisRootDirectory.SchemaClassName.ToString()
'Determine version of IIS
Using iisRootSchema As DirectoryEntry = Me.GetIISDirectoryEntry("IIS://Localhost/Schema/AppIsolated")
If iisRootSchema.Properties.Item("Syntax").Value.ToString.ToUpper = "BOOLEAN" Then
iisUnderNT = True
End If
End Using
'SiteName for Ex: yahoo,google
websiteIdentificationName = siteName
If className.EndsWith("Service") Then
'1. Get New Site for the site to be created on IIS
newSiteID = Me.GetNextSiteId(iisRootDirectory)
'2. Create a new website and add to IISRoot and Autostart website
Dim newSite As DirectoryEntry = sites.Add(newSiteID, (className.Replace("Service", "Server")))
'3. Autostart Website
newSite.Invoke("Put", "ServerAutoStart", 1)
newSite.CommitChanges()
'4. Configure Host header information for subdomains configuration
Dim hostHeaders As Object() = {":80:" & websiteIdentificationName, ":80:www." & websiteIdentificationName}
newSite.Invoke("Put", "ServerBindings", hostHeaders)
newSite.CommitChanges()
'5. Set Authorisation flags
newSite.Invoke("Put", "AuthFlags", "1") '1 = AuthAnonymous, 2 = BasicAuth, 4 = AuthNTLM - integrated windows auth,16 = AuthMDS, 64 = AuthPassport
newSite.CommitChanges()
newSite.Invoke("SetInfo")
newSite.CommitChanges()
'6. Set .Net 2.0 Framework Version
Dim scriptMapVals As PropertyValueCollection = newSite.Properties("ScriptMaps")
Dim objScriptMaps As New ArrayList()
For Each scriptMapVal As String In scriptMapVals
If scriptMapVal.IndexOf("Framework") > 0 Then
Dim version As String = scriptMapVal.Substring(scriptMapVal.IndexOf("Framework") + 10, 9)
Dim strNewValue As String = scriptMapVal.Replace(version, "v2.0.50727")
' UGLY .. but framework version wasnt returning this value
objScriptMaps.Add(strNewValue)
Else
objScriptMaps.Add(scriptMapVal)
End If
Next
newSite.Properties("ScriptMaps").Value = objScriptMaps.ToArray()
newSite.CommitChanges()
'7. Set website name and description
Me.SetMetabaseSingleProperty(newSite, "ServerComment", websiteIdentificationName)
'8. Set Application Name
Me.SetMetabaseSingleProperty(newSite, "AppFriendlyName", websiteIdentificationName)
'9. Set Applcation Pool
Dim poolRoot As New DirectoryEntry("IIS://localhost/W3SVC/AppPools")
Dim pool As DirectoryEntry = poolRoot.Children.Add(appPoolName, "IIsApplicationPool")
'*****************************************************************
'Important: To set App Pool to run in Integrated Mode in IIS 7.0
'*****************************************************************
pool.InvokeSet("ManagedPipelineMode", New Object() {0})
pool.CommitChanges()
'10. Set Scripts Execute Permissions
Me.SetMetabaseSingleProperty(newSite, "AccessExecute", True)
'11. Set Default documents
Me.SetMetabaseSingleProperty(newSite, "DefaultDoc", "default.aspx, default.htm, index.htm, index.html")
Using createdSite As DirectoryEntry = newSite.Children.Add("Root", "IIsWebVirtualDir")
'12. Set Source Path
Me.SetMetabaseSingleProperty(createdSite, "Path", localPath)
'13. Create the webapplication on website root
If iisUnderNT Then
createdSite.Invoke("AppCreate", False)
createdSite.CommitChanges()
Else
createdSite.Invoke("AppCreate", 1)
createdSite.CommitChanges()
End If
'14. Assign Website Root to AppPool
Me.AssignWebsiteToAppPool(createdSite, IISDirectoryType.VirtualDirectory, appPoolName)
End Using
newSite.Invoke("SetInfo")
newSite.CommitChanges()
newSite.Close()
Else
Throw New Exception("A site can only be created in a
End If
Else
Throw New Exception(String.Format("User Impersonation Failed: unable to impersonate user <{0}> for creating website", iisAdminUserName))
End If
End Using
Catch ex As Exception
ExceptionManager.Publish("Failed in CreateSite() with following exception: ", ex)
End Try
Exit Sub
End Sub
Thats it with System.DirectoryServices
SecondWay:
This is newer version and most simpler one compared to the Firstway. Add referecnce to the Microsoft.Web.Administration dll from DotNet components.
Let the real professionals handle this matter for you. You just have to provide them the details they would need to come up with a website according to your preference.
ReplyDelete