Virtual Control
IIS gives you the power to create and manage virtual directories—for
jobs big (and little).
- By Chris Brooke
- February 01, 2001
Last month I went to the obstetrician with my wife for her sonogram.
Not only did they record the whole thing on VHS so we can bore people
with it on home-movie night, but they printed out four “key” pictures,
two of which indicate the gender (a boy!). My wife was able to look at
the pictures for a full three seconds before I took custody of them. Upon
returning to work, I promptly scanned them; posted them on my Web site;
and e-mailed everyone in the family, inviting them to take a look. I plan
on shooting digital video during the delivery, although I’m not sure my
wife will so readily allow me to post that on the Web!
While using an area of my Web site to display pictures of my unborn son
(son!) may not seem like a highly complex use of IIS’s talents, I did
manage to set up everything using ADSI scripts for IIS administration.
It seemed like a timely opportunity, as I’m discussing that very topic
this month. Before I jump into creating and managing virtual directories,
let’s see how you did on your homework. As I recall, we needed to stop
and restart the IIS service.
' StopNGo.vbs
Dim objServer, colService
Set objServer=GetObject("WinNT://domain/iiscomputer,computer")
Set colService=objServer.GetObject("service","W3SVC")
colService.Stop
colService.Start
Set colService=Nothing
Set objServer=Nothing
Wscript.Quit
I know what you’re thinking. “How did he know what the name of the service
was?” Well, I looked it up.
' ShowServices.vbs
Dim objComputer, clsService
Set objComputer=GetObject(
"WinNT://domain/computer,computer")
objComputer.Filter=Array("service")
For Each clsService in objComputer
Wscript.Echo clsService.Name
Next
Since we’ve been working with IIS, we’re assuming that the service was
already started. However, it might not be a bad idea to check for this
first.
clsService.Status
This returns a long integer value that corresponds to a particular status:
1 - Stopped
2 - Start Pending
3 - Stop Pending
4 - Running
5 - Continue Pending
6 - Pause Pending
7 - Paused
8 - Error
You might also want to make sure that the objService.StartType is either
2 or 3 (Automatic or Manual), not 1 (Disabled). You can’t start a disabled
service, no matter how many times you run the script. Now, let’s move
on to manipulating the virtual directories of the site.
I Can Do Virtually Anything
Virtual directories are the areas of your Web site to the right of the
slash (http://www.MyWebSite.com/MyVirtualDirectory). They’re called “virtual”
directories because they don’t necessarily have to reside on the Web server.
They can, in fact, be anywhere, as long as the server can get to them
and has the proper access permissions to read them. This only pertains
to the first “slash.” All subsequent directories aren’t virtual. They’re
folders underneath the virtual directory folder, wherever it may be. I’ll
use as an example the following:
http://www.MyWebServer.com/MyVirtualDirectory/
SubDirectory/SubDirectory/WebPage.asp
Let’s start by creating a virtual directory in the site root called “sales.”
' NewVirDir.vbs
Dim objServer, clsVirDir
Set objServer=GetObject("IIS://MyWebServer/W3SVC/1/ROOT")
Set clsVirDir=objServer.Create("IIsWebVirtualDir", "sales")
clsVirDir.SetInfo
clsVirDir.Path="C:\Inetpub\wwwroot\sales"
clsVirDir.SetInfo
You’ll notice that I have two “SetInfo” statements. That’s because I
have to create the virtual directory before I can point it to a path.
Once I set the path, I must execute another “SetInfo” to save the information.
Another step in this process is to ensure that there’s a “sales” directory
under “wwwroot.” This is easy enough using Windows Explorer, but let’s
go ahead and script it, remembering to check to see if the folder already
exists before I create it — just to be on the safe side.
' NewVirDir2.vbs
Dim objServer, clsVirDir, objFSO, objFolder, bExists
Set objServer=GetObject("IIS://MyWebServer/W3SVC/1/ROOT")
Set clsVirDir=objServer.Create("IIsWebVirtualDir", "sales")
clsVirDir.SetInfo
Set objFSO=CreateObject("Scripting.FileSystemObject")
bExists=objFSO.FolderExists("c:\Inetpub\wwwroot\sales")
If bExists=False Then objFolder=objFSO.CreateFolder("c:\Inetpub\wwwroot\sales")
Else
objFolder=objFSO.GetFolder("c:\Inetpub\wwwroot\sales")
End If
clsVirDir.Path=objFolder.Path
clsVirDir.SetInfo
F-T-P, as easy as 1-2-3
A key to professional Web sites is the ability to download files, and
IIS includes an FTP service for just this task. Creating a virtual directory
for the FTP service is similar to the www service, with just a few exceptions.
' NewFTPDir.vbs
Dim objServer, clsVirDir, objFSO, objFolder, bExists
Set objServer=GetObject(
"IIS://MyFTPServer/MSFTPSVC/1/ROOT")
Set clsVirDir=objServer.Create("IIsFTPVirtualDir", "pub")
clsVirDir.SetInfo
Set objFSO=CreateObject("Scripting.FileSystemObject")
bExists=objFSO.FolderExists("c:\Inetpub\ftproot\pub")
If bExists=False Then objFolder=objFSO.CreateFolder("c:\Inetpub\ftproot\pub")
Else
objFolder=objFSO.GetFolder("c:\Inetpub\ftproot\pub")
End If
clsVirDir.Path=objFolder.Path
clsVirDir.SetInfo
Cut Em Off!
In your homework, you learned that in order to stop and restart the Web
service you had to use the WinNT provider to access NT Services. However,
IIS gives you the ability to stop and restart a particular Web site using
the IIS provider. As you may recall, last month I discussed virtual servers,
each with its own IP address and index number.
Let’s assume you’re a Web-hosting company. You host several small sites
on one Web server to save money. They don’t get a lot of traffic and your
server is fairly robust, so you can accommodate five sites without placing
undue load on the server. Let’s further assume that your accounting department
has told you to cut off one of the sites for delinquent payment. This
is likely a temporary measure, as the client will no doubt pay eventually,
so you don’t want to delete the entire site only to have to create it
again in a week or two. Here’s how you can use ADSI to do this:
' CutEmOff.vbs
Dim objSite
Set objSite=GetObject("IIS://MyWebServer/W3SVC/4")
objSite.Stop
And, just like that, it’s cut off! Note how I needed to use the site
index number I discussed last month. This is the “/4” in the “GetObject”
statement above. I’ll hope you wrote this information down when you created
the customer’s site. If not, you’ll have to run the script we wrote last
month to enumerate the sites based on their friendly names. Be sure to
write it down this time.
Next month, I’ll finish my romp through the world of ADSI by looking
at the LDAP provider. As such, I’m not assigning you any homework. I have
homework, though. My wife and I have to come up with a name for our son
(our son!). I think we’re leaning toward Samuel Garrett. Of course, if
we go with Samuel Michael his initials will be SMB. He’ll already be on
his way to becoming a full-fledged NT geek!