Using ADSI to program IIS

Source: Internet
Author: User
Tags date definition class definition execution iis connect naming convention what web server
iis| programming


Translator Note: This is an article on ADSI, translated by Sunwen. Before reading it, make sure you know at least what ad and ADSI are. I no longer apply. If you are interested in programming IIS and Exchange Server and other m$ products, (ADSI can say their interfaces), be sure to read it, because this article is wonderful! Later translation of more fluent, E-wen is not too good, hehe!
Understanding ADSI
Have you ever added a virtual directory through the VBS? (Do I want to) automatically establish an ISAPI server extension? or set up an entire site, including permissions? You can do this and more with ADSI.
ADSI (Active Directory Services Interface) is an application data interface. Microsoft's operating system supports ADSI with Windows NT 4.0 Server, Exchange, IIS, and site Server. In Win2K ADSI will become an operating system interface and may supersede the registry. ADSI's key is not in the underlying data store, but in its own interface layer. With an interface, you can configure all the server programs once you've learned them. (is not very cool Ah, hehe!) Unlike APIs, you can use
All languages or environments to access ADSI through COM. (This is something!)
Warning:
Before running the example of this article, please back up IIS4.0:
Open MMC (omit backup procedure below)
Object-oriented database
ADSI treats all data as objects. With ADSI you can manipulate all the data without learning a database query language such as SQL. Relative to a relational database, Creating an object-oriented database is simpler and faster. In a relational database, programmers have to know how an object is corrupted and stored; However, object-oriented data storage, such as ADSI, does not exist. The programmer simply knows the name of the object you want to visit.
To compare these two types of databases, imagine you want an employee's profile: personal information, departmental information, and salary information. In a relational database, these three data may be placed in three tables. and a change to the database may affect only a subset of the tables, Modifications to the database the programmer had to use a database query language such as SQL. It is very rare to notice how objects are stored (except for database administrators). While ADSI treats all information as objects, the connections between objects are part of ADSI's execution, so This part is no longer needed by programmers. Once you've learned to process an ADSI data, you learn to process all ADSI databases. As a contrast, you may need to learn every relational database language if you want to use a different database. (like FoxPro)
Data
The database structure is a hierarchical model. An object node can be the parent of another node, the sibling node, or a child node. Each child node inherits the properties of the parent node. The database has two logical sections. The first is the plan to define the data and data associations. The second part is the actual storage of the data. Defines an employee object that is placed in the planning section and the personal information is placed in the data section.
Get an instance of an object
Unlike COM, you don't have to use Server.CreateObject to get an instance of an ADSI object. You just use the GetObject method and a parameter that specifies the server and the location of the join. Example one has two etobject examples. In the first case, The Code establishes an ADSI instance of Windows NT. Each two example obtains an ADSI object for IIS.
Example 1
For Windows NT
Set Object = GetObject ("Winnt://15seconds")
For LDAP
Set Object = GetObject ("IIS://LOCALHOST/W3SVC/1")
Some ad service interfaces use a special naming convention called the X.500 luxury name. IIS does not use this rule but you have to x.500 a naming convention because you might also want to manipulate other ADSI data. You can go from ftp://src.doc.ic.ac.uk/ Rfc/rfc1484.txt get the relevant information.
Data retrieval


When we look for information on an LDAP server, we have to know whether the information is in the instance or in the Planning section. Give an example
, an object name can be contained in an object instance, but a list of object attributes will be stored in a planning department
The employee name, date of hire, and department are stored in the Employee object. The Plan section stores the definition of the employee attribute as the data type.
Example two illustrates the definition of an object instance and an object plan.
Example 2


Employee Instance
Name:bob Jones
Hire date:1/1/98
Department:information Technology

Employee Object Instance
Attribute "Name", single value, Data-type "text", MaxLength 50 "
Attribute "Hire Date", single value, Data-type "Date"
Attribute "Department", single value, Data-type "text"

For SQL programmers, the definition of an object is similar to that of a table. An object instance is like a specific row in a table. SQL
The difference from ADSI is that the data is not across multiple tables, and whether the programmer needs to manage the relationship between the data. In ADSI, connect
The mouth is responsible for these relationships.

Default Web site's pass properties
The first example can see the properties of the default Web site for IIS4.0. In Example 3, the code joins to the local machine's default Web site.
Example 3


<%
strMachineName = "localhost" ' Domain name
Strobjectpath = "W3SVC/1" ' Object name

' Construct object location in IIS
strpath = "iis://" & strMachineName & "/" & Strobjectpath
Set IIsObject = GetObject (strpath) ' Connect to IIS metabase
%>
Name = "<%= iisobject.name%>" <br>
Parent= "<%= iisobject.parent%>" <br>
schemalocation = "<%= iisobject.schema%>" <br>
Class = "<%= iisobject.class%>" <br>
Guid = "<%= iisobject.guid%>" <br>
ADSPath = "<%= iisobject.adspath%>" <br>


The output shows:

Name = "1"
Parent= "Iis://localhost/w3svc"
schemalocation = "Iis://localhost/schema/iiswebserver"
Class = "IIsWebServer"
Guid = ' {8b645280-7ba4-11cf-b03d-00aa006e0975} '
ADSPath = "IIS://LOCALHOST/W3SVC/1"


Note: If you want to run this program in an ASP page, make sure that the logged in user is granted administrator privileges.

In the last example of data retrieval can be used for any one object, including the Plan object. Every object whether he is a real
Object or a plan object must have these attributes: name, parent, plan, Class,guid, and ads path.

This information includes some navigation information for accessing data through a hierarchical structure. The Name property is the names of an object. A parent
Sex tells the location of the parent object. The SchemaLocation property refers to the location of the Plan object. The class attribute includes the type of the object.
It includes all attributes of an object and is set to schemalocation this position. A GUID property is one object's unique
Identification code. ADsPath is the location of the object you retrieved.


Note: The server named 1 represents the default Web site


Properties of the Default Web site
Now that we know the location of the object and the type of object, let's look at the properties of the default Web site. In order to achieve
For this purpose, we have to get the default Web site's schedule (schema), find the relevant attributes, and look at the default Web site's
The values of some properties.
Example 4


<%
strMachineName = "localhost"
Strobjectpath = "W3SVC/1" ' Find a Web server listed in W3SVC
strpath = "iis://" & strMachineName & "/" & Strobjectpath
Set IIsObject = GetObject (strpath)

' Find location of Web server ' definition
Set classdefinition = GetObject (Iisobject.schema)
%>
<table border=1>
<tr><th>default Web server Property</th><th>default Web server value</th></tr>
<tr><td>name</td><td><%= Iisobject.name%></td></tr>
<tr><td>parent</td><td><%= iisobject.parent%></td></tr>
<tr><td>schemalocation</td><td><%= Iisobject.schema%></td></tr>
<tr><td>class</td><td><%= Iisobject.class%></td></tr>
<tr><td>guid</td><td><%= Iisobject.guid%></td></tr>
<tr><td>adspath</td><td><%= Iisobject.adspath%></td></tr>
</table><br>
<table border=1>
<tr><th>class Property</th><th>class value</th></tr>
<tr><td>name</td><td><%= Classdefinition.name%></td></tr>
<tr><td>parent</td><td><%= classdefinition.parent%></td></tr>
<tr><td>schemalocation</td><td><%= Classdefinition.schema%></td></tr>
<tr><td>class</td><td><%= Classdefinition.class%></td></tr>
<tr><td>guid</td><td><%= Classdefinition.guid%></td></tr>
<tr><td>adspath</td><td><%= Classdefinition.adspath%></td></tr>
</table><br>
<%
On Error Resume Next

Asmusthaves = Classdefinition.mandatoryproperties
Asmayhaves = Classdefinition.optionalproperties

I=1
%>
<table border=1>
<tr><th>class must Have property</th>
<th> Default Web Site Current Value</th></tr>
<%
to each Thing in asmusthaves
Response.Write "<t R><td> ("& Cstr (i) &") "&_
Thing &" </td><td> "&_
Iisobject.get (Thing) & "</td></tr>"
i = i + 1
Next
%>
</table>
<br>
<table border=1
<tr><th>class may Have property</th>
<th>default the Web Site current value</th> </tr>
<%
I=1
for all Thing in asmayhaves
Response.Write "<tr><td> (" & CStr (i) & Amp ")" &_
Thing & "</td><td>" &_
Iisobject.get (Thing) & "</td></tr>"
I = i + 1
Next
%>
</table>


In order to get the properties of the default Web site, you need to know the name of the property, or iswebserver the list of attributes of this class. In the example
In 4, we get all the attributes and list their values. Each object can have mandatory and self-contained properties. The genus of coercion
Sex occurs when each object is established.


Note: If IIsWebServer only lists some of the optional properties, do not create a new Web site until you refer to the IIS documentation
The object of the point. ADSI's execution of IIS breaks this rule. Some of the required attributes are listed in the optional list of properties. If
When you set up a Web site without setting the necessary attributes, your metabase could be corrupted.


Note: ADSI will not stop at the wrong time and it will move on.
Create a virtual directory

In the example v we will create a virtual directory under the default Web site. We will first list all the properties of a virtual directory
And then create a virtual directory. To list all the attributes of a directory, I borrowed some of the code from example 3, except that the site
To the virtual directory, you must know the necessary attributes: What Web server do we want to build, its name, its physical path
Path, once you have established a virtual directory you will then set their properties one by one. Example five tells how to make use of these.
Example 5


<%
Scomputer = "localhost"
Sphydir = "C:\adsi"
Svirdir = "Adsitest"

' Get Default Web Site Object '
Set websvc = GetObject ("iis://" & Scomputer & "/W3SVC/1")

' Verify by printing out ServerComment
Response.Write "Comment =" & Websvc. ServerComment & "<br>"

' Get root ' Default Web Site
Set vRoot = Websvc. GetObject ("IIsWebVirtualDir", "Root")

' Get Class Definition ' virtual directory
Set classdefinition = GetObject (Vroot.schema)

' Get list of mandatory properties
Asmusthaves = Classdefinition.mandatoryproperties

' Get list of optional properties
Asmayhaves = Classdefinition.optionalproperties

I=1
%>
<table border=1>
<tr><th>class must Have property</th>
<th>root Virtual Directory Current value</th></tr>
<%
On Error Resume Next

For each Thing in asmusthaves
Response.Write "<tr><td> (" & Cstr (i) & ")" &_
Thing & "</td><td>" & Vroot.get (Thing) &_
"</td></tr>"
i = i + 1
Next
%>
</table>
<br>
<table border=1>
<tr><th>class May Have property</th>
<th>default Web Site Current value</th></tr>
<%
I=1
For each Thing in asmayhaves
Response.Write "<tr><td> (" & CStr (i) & ")" &_
Thing & "</td><td>" & Vroot.get (Thing) &_
"</td></tr>"
i = i + 1
Next
On Error Goto 0

' Create Virtual Directory
' Param 1 is class name
' Param 2 is the new object name
Set VDir = vroot.create ("IIsWebVirtualDir", Svirdir)

' Only setting two properties
Vdir.accessread = True
Vdir.path = Sphydir

' Write information back to Metabase
Vdir.setinfo
%>


In this example, in order to create an instance of an object, the parent object is used. The object class is used to create an instance of the image
Name. This object has no mandatory attributes, and some objects have mandatory properties. If you do not set these necessary attributes, when the
You make mistakes when you use Object.setinfo.
To verify that the virtual directory has been established, you can turn on IIS to see. Another way is to run the program again,
If the last one was established, you will get the wrong result this time. (recommended before, Sunwen)


Another feature of ADSI is the object. All property settings do not take effect until SetInfo is run. This makes your site
Will not be affected by some informal (if a person is requesting this site, an error will occur). It's more than a relational type.
Database is simpler. ADSI automatically completes all of the relevant data settings, so you don't have to use n-Times INSERT statements
(Xi Hee, foreigners and Sunwen as humorous!) )。 Updates are the same. You just have to do it once.

To add a default document

In this example, we will add the "index.htm" file to the list of default documents. In order to achieve this goal, I
To get this virtual directory from the data source, change the attributes associated with the default document, and then write the information back.
Example 6


<%
Scomputer = "localhost"
Sphydir = "C:\dina\adsi"
Svirdir = "Adsitest"

' Get Default Web Site Object '
Set websvc = GetObject ("iis://" & Scomputer & "/W3SVC/1")

' Verify by printing out ServerComment
Response.Write "Comment =" & Websvc. ServerComment & "<br>"

' Get root ' Default Web Site
Set vRoot = Websvc. GetObject ("IIsWebVirtualDir", "Root")

' Create Virtual Directory
Set VDir = vroot.create ("IIsWebVirtualDir", Svirdir)

' Get local copy of object '
Vdir.getinfo

Response.Write "Old Default Docs =" & Vdir.defaultdoc & "<BR>"

' Only setting two properties
Vdir.defaultdoc = Vdir.defaultdoc & ", index.htm"

' Write information back to Metabase
Vdir.setinfo

' Get Object Again
Vdir.getinfo

Response.Write "New Default Docs =" & Vdir.defaultdoc
%>


In this example, we use the Objec.getinfo statement to retrieve the information. It retrieves all the information at once.
Out. Then we use the SetInfo to reset the information back.

Delete a virtual directory

In this example, you will learn how to delete a virtual directory. When you create an object, you must have its parent object. This
A truth also applies to deletion. Example seven shows how to do it.
Example 7


<%
On Error Resume Next

Scomputer = "localhost"
Sphydir = "C:\dina\adsi"
Svirdir = "Adsitest"

' Get Default Web Site Object '
Set websvc = GetObject ("iis://" & Scomputer & "/W3SVC/1")

' Verify by printing out ServerComment
Response.Write "Comment =" & Websvc. ServerComment & "<br>"

' Get root ' Default Web Site
Set vRoot = Websvc. GetObject ("IIsWebVirtualDir", "Root")

' Delete Virtual Directory
Set VDir = Vroot.delete ("IIsWebVirtualDir", Svirdir)
%>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.