A Sample ASP Script
As an example, I'll show you a simple ASP script that accepts the name of a computer, and then uses Windows Management Instrumentation (WMI) to display some key information about that computer. You could expand this easily into a real-time inventory check page, usable by help desk technicians, perhaps.
Before this script will work, however, you need to make some changes to the computer running it. I'm going to list the changes needed; I'll describe the need for these changes in Chapter 23.
Open the Registry Editor. Browse to HKEY_LOCAL_MACHINE\Software\Microsoft\WBEM\Scripting. This key should already exist. Create a new DWORD value named "Enable for ASP" and set the value to 1. If the value already exists, make sure it's set to 1. If you're putting this Web page in the Default Web Site, use Internet Services Manager to open the properties of that Web site. If you're using another Web site, open that site's properties. On the Directory Security tab, disable anonymous access and ensure that Windows Integrated Authentication is selected.
When you attempt to access the Web page (or any other page in that Web site), you may be prompted to log on. Make sure you're doing so with a user account that's an administrator, or the script may not run properly. ASP and WMI security are complex; I'll cover them in more detail in Chapter 23 to explain what's going on.
Displaying Information in a Web Page
The script in Listing 21.1 uses WMI to display information about a selected computer.
Listing 21.1. Display.asp. Uses WMI to display information about a computer.
<%
If Request("COMPUTERNAME") <> "" Then
Set oSystem = GetObject("winmgmts:{impersonationLevel=" & _
"impersonate}!//" & Request("COMPUTERNAME") & _
"/root/cimv2:Win32_ComputerSystem=" & Chr(34) & _
Request("COMPUTERNAME") & Chr(34))
Response.Write "System information for " & _
Request("COMPUTERNAME") & "<HR>"
Response.Write "Name: " & oSystem.Caption & "<BR>"
Response.Write "Owner: " & oSystem.PrimaryOwnerName & "<BR>"
Response.Write "Domain: " & oSystem.Domain & "<BR>"
Response.Write "Type: " & oSystem.SystemType & "<BR>"
Response.Write "<HR>"
Response.End
End If
%>
<HTML>
<BODY>
<%
Response.Write("It is now " & Now)
%>
<FORM ACTION="display.asp" METHOD="POST">
Computer name: <INPUT TYPE="TEXT" NAME="COMPUTERNAME"><BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
Other than the security changes I outlined previously, you shouldn't need to make any modifications to use this Web page. Keep in mind that, because it's a Web page, it should be usable by an administrator with HTTP access to the computer hosting the page.
Displaying Information in a Web Page-Explained
Notice that this script begins with ASP code. The first line of code checks to see if Request("COMPUTERNAME") is empty. If it is, the script hasn't run before, and none of the WMI stuff is executed. Instead, the script skips directly to the HTML code.
<%
If Request("COMPUTERNAME") <> "" Then
...
End If
%>
<HTML>
<BODY>
The script then uses VBScript to display the current date and time.
<%
Response.Write("It is now " & Now)
%>
Finally, the script uses standard HTML to create a short input form. The form consists of a text box and a Submit button. Notice that the form's ACTION is set to submit the form to this very same page, and that the name of the text box is COMPUTERNAME. That will become important in a bit.
<FORM ACTION="display.asp" METHOD="POST">
Computer name: <INPUT TYPE="TEXT" NAME="COMPUTERNAME"><BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
Now, the Web user can type a computer name and press Submit. When he does that, his browser bundles the form information (the contents of the text box) and sends it to Display.asp, which happens to be the same page. Neither the Web browser nor ASP cares that the same Web page is both displaying the form and handling it; I could have split this into two separate pages, in fact. However, keeping the form and its handling code in one page is easier for maintenance, so I did it this way.
When the code is resubmitted, ASP takes a fresh look at the page. This time, Request("COMPUTERNAME") isn't empty-it contains the name of a computer that the user types. The If…Then construct evaluates to True.
<%
If Request("COMPUTERNAME") <> "" Then
ASP evaluates the next line of code, which is a call to WMI. This call connects to the computer designated by the user and queries its Win32_ComputerSystem class. Actually, the query is Win32_ComputerSystem = 'computername', where computername is whatever the user typed. This retrieves the property WMI class from the remote computer.
Set oSystem = GetObject("winmgmts:{impersonationLevel=" & _
"impersonate}!//" & Request("COMPUTERNAME") & _
"/root/cimv2:Win32_ComputerSystem=" & Chr(34) & _
Request("COMPUTERNAME") & Chr(34))
It's downhill from there. With oSystem set to the WMI class, ASP can output the information requested.
Response.Write "System information for " & _
Request("COMPUTERNAME") & "<HR>"
Response.Write "Name: " & oSystem.Caption & "<BR>"
Response.Write "Owner: " & oSystem.PrimaryOwnerName & "<BR>"
Response.Write "Domain: " & oSystem.Domain & "<BR>"
Response.Write "Type: " & oSystem.SystemType & "<BR>"
Response.Write "<HR>"
Response.End
Did you notice Response.End? That prevents ASP from dropping through to the original HTML form. You could easily remove Response.End. The result would be a script that displays the system information, and then redisplays the original HTML form. That would be handy for immediately typing in another computer name and getting its information.
This simple example can be easily expanded to collect other information about the remote computer. In fact, this isn't unlike the WMI scripts you saw in Chapters 17 through 20; the only differences are that ASP has to be configured to work with WMI's security (and vice versa) and that you use Response.Write to output information instead of MsgBox or WScript.Echo.
|