ADSI Examples

For our examples, we will be creating a series of simple ASP statements that contain some useful routines you may want to include in your own pages. These examples are commented so that they can be understood clearly. After progressing through a few simple examples, we will present a complete ASP page you can test for yourself.

Our first objective in any page using ADSI is to invoke the GetObject method to create an instance of our IIsComputer object. This object is the basis of all information on an ASP page:

<%
Set objComp = GetObject("IIS://MachineName/W3SVC")
%>
 

After creating IIsComputer, one of the more commonly used functions is one that produces a list of sites that exist on the server. The following code illustrates this:

<%
Response.Write "<table>"
Response.Write "<tr><td><b> Index </b></td><td><b> Description </b></td></tr>"
For Each strItem in objComp
Response.write "<tr><td>"
'ServerComment displays an easy to read title for the server
Response.write strItem.Name & "</td><td>" & strItem.ServerComment
Select case strItem.ServerState
case &H00000002: Response.Write " (Server Running)"
case &H00000004: Response.Write " (Server Stopped)"
case &H00000006: Response.Write " (Server Paused)"
case else: Response.Write " (Server State Unknown)" & strItem.ServerState & "<BR>"
end Select
Response.write "</td></tr>"
Next
Response.Write "</table>"
%>
 

Another useful tool is a form that allows the user to add sites and select site options. This can be implemented using standard HTML code and a command that triggers the function Add() when the page is refreshed. Using the <pre> tag, we can easily format the form's contents:

<form method="post" action="example2.asp?action=1" onSubmit="return chkInput(this)">
<pre>
Site <input type="text" size="35" name="strComment">
Path <input type="text" size="50" name="strPath">
Allow Anonymous <input type="radio" name="AllowAnon" value="no"> No
<input type="radio" name="AllowAnon" value="yes"> Yes
Access Permissions <input type="checkbox" name="Read" value="yes"> Read
<input type="checkbox" name="Write" value="yes"> Write
<input type="checkbox" name="Execute" value="yes"> Execute
<input type="checkbox" name="Script" value="yes"> Script
<input type="submit" value="Add a Site">
<input type="reset" value="Reset">
</pre>
</form>
 

A simple subroutine in Visual Basic® that illustrates adding a new site to the server could be called from the form above:

<%
Sub Add()
Dim idxItem, idxNext, objNew, objRoot, svrComment, svrPath

svrComment = Request.Form("strComment")
svrPath = Request.Form("strPath")

Set objComp = GetObject("IIS://MachineName/W3SVC")

For Each idxItem in objComp
If isNumeric(idxItem.Name) Then
If idxNext < idxItem.Name Then
idxNext = idxItem.Name
End If
End If
Next
idxNext = idxNext + 1
Set objNew = objComp.Create ("IIsWebServer", idxNext)
objNew.ServerComment = svrComment
objNew.SetInfo

set objRoot = objNew.Create("IIsWebVirtualDir", "Root")
objRoot.Path = svrPath
objRoot.SetInfo

objNew.Start
End Sub
%>
 

As you can see, by using your knowledge of ADSI and a little resourcefulness, you can generate some very useful ASP pages. By looking deeper into the IIsComputer hierarchy, you can produce numerous applications that can control all aspects of IIS.

Below you will find a simple page that allows you to create a basic Web site on a server. This code lightly touches on the possibilities of using ADSI and IIS together in your ASP pages. The code is fully commented to help you understand the procedures involved.

Example2.asp

<html>
<head>
<title>IIS/ADSI Example</title>
<script language="JavaScript">
<!--
function chkInput(strIn)
{
if (strIn.strComment.value == null || strIn.strComment.value.length == 0)
{
alert("Site cannot be blank.");
return false;
}
if (strIn.strPath.value == null || strIn.strPath.value.length == 0)
{
alert("Path cannot be blank.");
return false;
}

}
// -->
</script>
</head>

<body>

<b>Current Sites on Server:</b><br>

<%

'Add this to prevent errors from the Filters and Info objects
On Error Resume Next

Dim strAction
Dim objComp, strItem

strAction = Request.QueryString("action")

Select case strAction
case 1
Add()
case else
Response.Write "?"
End Select

Set objComp = GetObject("IIS://MachineName/W3SVC")

Response.Write "<table>"
Response.Write "<tr><td><b> Index </b></td><td><b> Description </b></td></tr>"
For Each strItem in objComp
Response.write "<tr><td>"
Response.write strItem.Name & "</td><td>" & strItem.ServerComment
Select case strItem.ServerState
case &H00000002:
Response.Write " (Server Running)"
case &H00000004:
Response.Write " (Server Stopped)"
case &H00000006:
Response.Write " (Server Paused)"
case else:
Response.Write " (State Unknown)" & strItem.ServerState & "<BR>"
End Select
Response.write "</td></tr>"
Next
Response.Write "</table>"
%>
<hr>

<form method="post" action="example2.asp?action=1" onSubmit="return chkInput(this)">
<pre>
Site <input type="text" size="35" name="strComment">
Path <input type="text" size="50" name="strPath">
Allow Anonymous <input type="radio" name="AllowAnon" value="no" Checked> No
<input type="radio" name="AllowAnon" value="yes"> Yes
Access Permissions <input type="checkbox" name="Read" value="yes" Checked> Read
<input type="checkbox" name="Write" value="yes" Checked> Write
<input type="checkbox" name="Execute" value="yes"> Execute
<input type="checkbox" name="Script" value="yes"> Script
</pre>
<table><tr>
<td align="top"><input type="submit" value="Add Site"></td><td valign="top"><input type="reset" value="Restore"></td>
</form>
<td valign="top">
<form method="post" action="example2.asp"><input type="submit" value="Refresh">
</form>
</td></tr></table>
</body>
</html>
<%
Sub Add()
Dim idxItem, idxNext, objNew, objRoot
Dim svrComment, svrPath

svrComment = Request.Form("strComment")
svrPath = Request.Form("strPath")

Response.Write "Adding a Site...<BR>"
Response.Write "Getting the Next Available Server Index..."
Set objComp = GetObject("IIS://MachineName/W3SVC")

For Each idxItem in objComp
If isNumeric(idxItem.Name) Then
If idxNext < idxItem.Name Then
idxNext = idxItem.Name
End If
End If
Next
idxNext = idxNext + 1
Response.Write " (" & idxNext & ")<BR>"

Response.Write "Creating the New Site...<BR>"
Set objNew = objComp.Create ("IIsWebServer", idxNext)
objNew.ServerComment = svrComment
objNew.SetInfo

Response.Write "Creating Site Root...<BR>"
set objRoot = objNew.Create("IIsWebVirtualDir", "Root")
objRoot.Path = svrPath
objRoot.SetInfo

Response.Write "Starting Server...<BR>"
objNew.Start

Response.Write "Setting up Server...<BR>"

If StrComp(Request.Form("AllowAnon"), "YES" , 1) = 0 Then
objRoot.AuthAnonymous = True
Else
objRoot.AuthAnonymous = False
End If

If StrComp(Request.Form("Read"), "YES", 1) = 0 Then
objRoot.AccessRead = True
End If
If StrComp(Request.Form("Write"), "YES", 1) = 0 Then
objRoot.AccessWrite = True
End If
If StrComp(Request.Form("Execute"), "YES", 1) = 0 Then
objRoot.AccessExecute = True
End If
If StrComp(Request.Form("Script"), "YES", 1) = 0 Then
objRoot.AccessScript = True
End If

objRoot.SetInfo

End Sub
%>
 

To try this out, place the file into your server's root directory, and call http://localhost/example2.asp from your browser. You will see a list of sites on your server. Enter a site name and path, click the Add Site button, and after the site is created, your browser will refresh the page, displaying the new list of sites that exist on your server.

Windows 2000 Server will ship with ADSI installed on the machine, allowing you to use this technology without installing it. In the meantime, you can download a copy of the most recent ADSI distribution at http://www.microsoft.com/NTWorkstation/downloads/Other/ADSI25.asp .


Source MSDN: http://www.microsoft.com/technet/treeview/default.asp?url=/TechNet/prodtechnol/iis/maintain/optimize/adsi3.asp.