Hej, Jag hittade ett c# exempel på hur man från kod skapar en ny webbsite. Hoppas det fungerar. Tackar så mycket, Varsågod. Om det fungerar, återkom med en artikel istället ;-)Skapa en virtuell katalog med script
Jag håller på att automatisera en installation av en webapplikation och undrar om det finns någon teknik man kan använda sig av för att lägga in en ny virtuell katalog i IIS 5/IIS 6 (Så att man slipper göra det manuellt varje gång man installerar applikationen).
/StefanSv: Skapa en virtuell katalog med script
Exempel i c#
http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspx
samt för vb.net
Imports System.Xml
Imports System.DirectoryServices
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
<Assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode:=True)>
Public Class purchase
Inherits System.Web.UI.Page
<DllImport("C:\\Windows\\System32\\advapi32.dll")> _
Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As Integer) As Boolean
End Function
<DllImport("C:\\Windows\\System32\\Kernel32.dll")> _
Public Shared Function GetLastError() As Integer
End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'The Windows NT user token.
Dim token1 As Integer
'Get the user token for the specified user, machine, and password using the unmanaged LogonUser method.
'The parameters for LogonUser are the user name, computer name, password,
'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider (LOGON32_PROVIDER_DEFAULT),
'and user token.
Dim loggedOn As Boolean = LogonUser("administrator", "NEXUS", "tempPass", 3, 0, token1)
'Call GetLastError to try to determine why logon failed if it did not succeed.
Dim ret As Integer = GetLastError()
If ret <> 0 Then
Throw New Exception("Error code (126 == ""Specified module could not be found""): " + ret.ToString())
End If
'Starting impersonation here:
Dim token2 As IntPtr = New IntPtr(token1)
Dim mWI2 As WindowsIdentity = New WindowsIdentity(token2)
'Impersonate the user.
Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()
'Setup IIS web and ftp virtual directories
Dim vdir As String = Request.QueryString("sld") + "-" + Request.QueryString("tld")
Dim path As String = "c:\inetpub\wwwroot\" + vdir
Dim deRoot As New DirectoryEntry("IIS://localhost/W3SVC/1/Root")
deRoot.RefreshCache()
Dim deNewVDir As DirectoryEntry = deRoot.Children.Add(vdir, "IIsWebVirtualDir")
deNewVDir.Properties("Path").Insert(0, path)
deNewVDir.CommitChanges()
deRoot.CommitChanges()
'Create a Application
deNewVDir.Invoke("AppCreate", True)
'Save Changes
deNewVDir.CommitChanges()
deRoot.CommitChanges()
deNewVDir.Close()
deRoot.Close()
Label1.Text += "<br><br>Web Virtual Directory " + vdir + " (" + path + ") has been created"
deRoot = New DirectoryEntry("IIS://localhost/MSFTPSVC/1/Root")
deRoot.RefreshCache()
deNewVDir = deRoot.Children.Add(vdir, "IIsFtpVirtualDir")
deNewVDir.Properties("Path").Insert(0, path)
deNewVDir.CommitChanges()
deRoot.CommitChanges()
'Save Changes
deNewVDir.CommitChanges()
deRoot.CommitChanges()
deNewVDir.Close()
deRoot.Close()
'Revert to previous identity.
mWIC.Undo()
End Sub
End Class
Sen några tips:
Radera site:
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
root.Invoke("Delete", "IIsWebServer", iWebSiteID);
Stoppa/Starta/Pausa/Continue en site
DirectoryEntry oWebSite = new DirectoryEntry("IIS://localhost/W3SVC/"+iWebSiteID);
oWebSite.Invoke("Stop"); // or Start, Pause etc
Sv: Skapa en virtuell katalog med script
Jag testar och återkommer med info om det funka ;-)
/StefanSv: Skapa en virtuell katalog med script