Re: IIS Create Site in asp.net

From: Chris Hood (Tech_at_computertuter.com)
Date: 02/27/04


Date: Fri, 27 Feb 2004 11:54:43 -0500

IN CASE ANYONE WOULD LIKE TO KNOW. hERE IS THE SOLUTION TO MY PROBLEM.

Public Class SiteCreator

Public Function STARTENEWSITE(ByVal servername As String)

Try

Dim scope As New ManagementScope("root\MicrosoftIISv2")

scope.Connect()

Dim iisSite As New ManagementObject(scope, New ManagementPath(servername),
Nothing)

iisSite.InvokeMethod("Start", New Object() {1})

Catch t As Exception

Return ("Failed to connect: " & t.Message)

End Try

End Function

Public Function Main()

Dim rootVirtualDir As String = "c:\NewSite"

Dim site As String = "MySpecialSite"

Dim oConn As New ConnectionOptions()

'oConn.Username = "webAdministrator"

'oConn.Password = "adminpwd"

Dim myPath As New ManagementPath()

myPath.Server = "N"

myPath.NamespacePath = "root\MicrosoftIISv2"

myPath.RelativePath = "IIsWebService='W3SVC'"

Dim scope As New ManagementScope(myPath, oConn)

Dim nac As New ManagementObject(scope, myPath, Nothing)

Try

' create a ServerBinding WMI object

Dim Bindings As ManagementObject = CreateInstance(oConn)

Bindings("IP") = ""

Bindings("Hostname") = ""

Bindings("Port") = "8084"

Bindings.Put() 'to commit the new instance.

Dim ServerBindings(1) As ManagementBaseObject

ServerBindings(0) = Bindings

' create a ServerBinding WMI object

Dim Bindings1 As ManagementObject = CreateInstance(oConn)

Bindings1("IP") = ""

Bindings1("Hostname") = ""

Bindings1("Port") = "8523"

Bindings1.Put() 'to commit the new instance.

ServerBindings(1) = Bindings1

Dim ret As String = CreateNewSite(nac, rootVirtualDir, ServerBindings, site)

Dim string1 As String = STARTENEWSITE(ret)

'Dim iis As New IISWMI()

'iis.StartWebSite(site)

Return ret

Finally

nac.Dispose()

End Try ' continue to configure and eventually manage the web server (start,
stop

End Function 'Main

' Create bindings object in WMI repository

Public Shared Function CreateInstance(ByVal oConn As ConnectionOptions) As
ManagementObject

Dim mScope As ManagementScope = Nothing

Dim mPath As New ManagementPath()

mPath.ClassName = "ServerBinding"

mPath.NamespacePath = "root\MicrosoftIISv2"

mScope = New ManagementScope(mPath, oConn)

mScope.Path.Server = "."

Return New ManagementClass(mScope, mPath, Nothing).CreateInstance()

End Function 'CreateInstance

' Create new site

Public Shared Function CreateNewSite(ByVal nac As ManagementObject, ByVal
PathOfRootVirtualDir As String, ByVal ServerBindings() As
ManagementBaseObject, ByVal ServerComment As String) As String

', int ServerId

Dim inP As ManagementBaseObject = Nothing

inP = nac.GetMethodParameters("CreateNewSite")

inP("PathOfRootVirtualDir") = PathOfRootVirtualDir

inP("ServerBindings") = ServerBindings

inP("ServerComment") = ServerComment

Dim OPM As ManagementBaseObject = nac.InvokeMethod("CreateNewSite", inP,
Nothing)

' inP["ServerId"] = ServerId; // Let the system create an ID (calculated
hash of ManagementBaseObject outP = nac.InvokeMethod("CreateNewSite", inP,
null);

'inp.

Return System.Convert.ToString(OPM.Properties("ReturnValue").Value)

End Function 'CreateNewSite ' Returns something like:
IIsWebServer='W3SVC/1273343373'

'SiteCreator ' this can be used to further configure/customize the webserver

End Class

"Chris Hood" <Tech@computertuter.com> wrote in message
news:OPEvNbJ$DHA.3452@TK2MSFTNGP11.phx.gbl...
> This is a copy of my class file....
>
> Imports System.Collections
>
> imports System.Web.HttpUtility
>
> Imports System.Object
>
> Imports System.Data
>
> Imports System.Data.SqlClient
>
> Imports System.Data.SqlTypes
>
> Imports System.Management
>
> Imports System
>
> Imports System.ComponentModel
>
> Imports System.Drawing
>
> Imports System.Web
>
> Imports System.Web.SessionState
>
> Imports System.Web.UI
>
> Public Class IISWMI
>
> Inherits System.Web.UI.UserControl
>
> Public Class SiteCreator
>
> Shared Sub Main()
>
> Dim rootVirtualDir As String = "c:\NewSite"
>
> Dim site As String = "MySpecialSite"
>
> Dim oConn As New ConnectionOptions()
>
> 'oConn.Username = "webAdministrator"
>
> 'oConn.Password = "adminpwd"
>
> Dim myPath As New ManagementPath()
>
> myPath.Server = "N"
>
> myPath.NamespacePath = "root\MicrosoftIISv2"
>
> myPath.RelativePath = "IIsWebService.Name='W3SVC'"
>
> Dim scope As New ManagementScope(myPath, oConn)
>
> Dim nac As New ManagementObject(scope, myPath, Nothing)
>
> Try
>
> ' create a ServerBinding WMI object
>
> Dim Bindings As ManagementObject = CreateInstance(oConn)
>
> Bindings("IP") = ""
>
> Bindings("Hostname") = ""
>
> Bindings("Port") = "8083"
>
> Bindings.Put() 'to commit the new instance.
>
> Dim ServerBindings(1) As ManagementBaseObject
>
> ServerBindings(0) = Bindings
>
> Dim ret As String = CreateNewSite(nac, rootVirtualDir, ServerBindings,
site)
>
> Console.WriteLine("Return: {0} ", ret)
>
> Finally
>
> nac.Dispose()
>
> End Try ' continue to configure and eventually manage the web server
(start,
> stop
>
> End Sub 'Main
>
> ' Create bindings object in WMI repository
>
> Public Shared Function CreateInstance(ByVal oConn As ConnectionOptions) As
> ManagementObject
>
> Dim mScope As ManagementScope = Nothing
>
> Dim mPath As New ManagementPath()
>
> mPath.ClassName = "ServerBinding"
>
> mPath.NamespacePath = "root\MicrosoftIISv2"
>
> mScope = New ManagementScope(mPath, oConn)
>
> mScope.Path.Server = "."
>
> Return New ManagementClass(mScope, mPath, Nothing).CreateInstance()
>
> End Function 'CreateInstance
>
> ' Create new site
>
> Public Shared Function CreateNewSite(ByVal nac As ManagementObject, ByVal
> PathOfRootVirtualDir As String, ByVal ServerBindings() As
> ManagementBaseObject, ByVal ServerComment As String) As String
>
> ', int ServerId
>
> Dim inP As ManagementBaseObject = Nothing
>
> inP = nac.GetMethodParameters("CreateNewSite")
>
> inP("PathOfRootVirtualDir") = PathOfRootVirtualDir
>
> inP("ServerBindings") = ServerBindings
>
> inP("ServerComment") = ServerComment
>
> ' inP["ServerId"] = ServerId; // Let the system create an ID (calculated
> hash of ManagementBaseObject outP = nac.InvokeMethod("CreateNewSite", inP,
> null);
>
> Return System.Convert.ToString(inP.Properties("ReturnValue").Value)
>
> End Function 'CreateNewSite ' Returns something like:
> IIsWebServer='W3SVC/1273343373'
>
> End Class 'SiteCreator ' this can be used to further configure/customize
the
> webserver
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Server Error in '/' Application.
> --------------------------------------------------------------------------

--
> ----
>
> Object reference not set to an instance of an object.
> Description: An unhandled exception occurred during the execution of the
> current web request. Please review the stack trace for more information
> about the error and where it originated in the code.
>
> Exception Details: System.NullReferenceException: Object reference not set
> to an instance of an object.
>
> Source Error:
>
>
>
>       An unhandled exception was generated during the execution of the
> current web request. Information regarding the origin and location of the
> exception can be identified using the exception stack trace below.
>
>
>
> Stack Trace:
>
>
>
> [NullReferenceException: Object reference not set to an instance of an
> object.]
>    System.Management.PropertyData.MapValueToWmiValue(Object val, CimType
> type, Boolean isArray) +1505
>    System.Management.PropertyData.set_Value(Object value) +57
>    System.Management.ManagementBaseObject.SetPropertyValue(String
> propertyName, Object propertyValue) +96
>    System.Management.ManagementBaseObject.set_Item(String propertyName,
> Object value) +44
>
> [ManagementException: Object reference not set to an instance of an
object.]
>    System.Management.ManagementException.ThrowWithExtendedInfo(Exception
e)
> +167
>    System.Management.ManagementBaseObject.set_Item(String propertyName,
> Object value) +54
>    FormAuthentication.SiteCreator.CreateNewSite(ManagementObject nac,
String
> PathOfRootVirtualDir, ManagementBaseObject[] ServerBindings, String
> ServerComment) in C:\Documents and
>
Settings\Administrator.INTERNALW\VSWebCache\cpanel.computertuter.com\membera
> rea\Comp\IISWMI.vb:206
>    FormAuthentication.SiteCreator.Main() in C:\Documents and
>
Settings\Administrator.INTERNALW\VSWebCache\cpanel.computertuter.com\membera
> rea\Comp\IISWMI.vb:182
>    FormAuthentication.iisbasic.Linkbutton4_Click(Object sender, EventArgs
e)
> in C:\Documents and
>
Settings\Administrator.INTERNALW\VSWebCache\cpanel.computertuter.com\membera
> rea\Comp\iisbasic.ascx.vb:185
>    System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +108
>
>
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.Rai
> sePostBackEvent(String eventArgument) +57
>    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
> sourceControl, String eventArgument) +18
>    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+138
>    System.Web.UI.Page.ProcessRequestMain() +1277
>
>
>
>
>
>
>
>
>
>
>
> This is the error i am getting.  The lines the error refrences are off
> beacuse i shorten.
>
> the line it is refrencince is this one
>
> inP("ServerBindings") = ServerBindings
>
>
>
> Someone please help.  I am lost.  Been trying all kinda things to make it
> work and i cant.
>
>
>
>
>
>
>
>
>
>