Adding Inline Script
Now it's time to start pulling everything together. In the previous chapter, I mentioned how it can save time and maintenance effort to keep a form and the handling page in the same file. I showed you an example of how to use an If…Then construct to tell if the form is being submitted or not.
In this page, I want to only display the HTML page if the form isn't being submitted. If it is being submitted, I want to take the submitted information and display the appropriate event log entries.
ASP allows you to include conditional static HTML in a Web page. It's actually harder to describe than to do it, so here's an example.
<%
'Was the form submitted?
If Request.Form("SUBMIT") = "" Then
%>
<FORM ACTION="event.asp" METHOD="POST">
<Table cellpadding=2 cellspacing=2 border=0>
<TR>
<TD>
<INPUT type="text" name="ComputerName"
value="<% Response.Write CompName%>">
</TD>
<TD>Computer:</TD>
</TR>
<TR>
<TD>
<SELECT name="LogName">
<OPTION value="application">Application</OPTION>
<OPTION value="system">System</OPTION>
<OPTION value="security">Security</OPTION>
</SELECT>
</TD>
<TD>Log</TD>
</TR>
<TR>
<TD><INPUT type="text" name="Source"></TD>
<TD>Event Source</TD>
</TR>
<TR>
<TD>
<SELECT name="Type">
<OPTION value="">All</option>
<OPTION value="information">Information</OPTION>
<OPTION value="warning">Warning</OPTION>
<OPTION value="error">Error</OPTION>
</SELECT>
</TD>
<TD>Type</TD>
</TR>
<TR>
<TD><input type="text" name="EventCode"></TD>
<TD>Event Code</TD>
</TR>
<TR>
<TD><input type="text" name="UserName"</TD>
<TD>User Name</TD>
</TR>
<TR>
<TD><input type="password" name="Password"></TD>
<TD>Password</TD>
</TR>
<TR>
<TD COLSPAN=2 Align=center>
<INPUT type="submit" NAME="Submit" VALUE="Submit">
</TD>
</TR>
</TABLE>
</FORM>
<%
'Here's the end of the original If...Then
'This is executed if the form was submitted
Else
Can you spot the key components? At the very start of the page, the ASP code tags indicate that VBScript is being used. The first statement is an If…Then construct that checks the value of the "SUBMIT" input field. If you look at the form HTML, "SUBMIT" is the name of the Submit button, and it will have a value of "SUBMIT" if it's clicked. If it's empty, the form wasn't submitted and it needs to be displayed.
<%
'Was the form submitted?
If Request.Form("SUBMIT") = "" Then
%>
<FORM ACTION="event.asp" METHOD="POST">
<Table cellpadding=2 cellspacing=2 border=0>
<TR>
<TD>
<INPUT type="text" name="ComputerName"
value="<% Response.Write CompName%>">
</TD>
...
Notice that the If…Then doesn't have an Else or End If right away. Instead, the ASP code ends with the %> tag, and the static HTML begins. This causes ASP only to display the static HTML if the logical condition in If…Then is true. There is an Else statement, all the way at the end of the static HTML.
</TR>
</TABLE>
</FORM>
<%
'Here's the end of the original If...Then
'This is executed if the form was submitted
Else
If the logical condition of the If…Then block is False, VBScript and ASP skips the static HTML and heads straight for whatever comes after the Else.
Speaking of that, here's the code that queries the event log and displays the information. Again, I'll walk through this line-by-line later, so for now, just see how far along you can follow.
'declare variables
Dim oServices, oResultset, oRecord
Dim sComputerName, sLogFile, sQuery
Dim dtDate, dtTime
'get the network object
set oNet =CreateObject("WScript.Network")
'get a WMI locator
set oLocator = CreateObject("WbemScripting.SWbemLocator")
'get the local computer name
sComputerName = oNet.ComputerName
'build the WMI query
sQuery = "SELECT * FROM Win32_NTLogEvent WHERE Logfile="
'computer name specified?
If(Request("ComputerName") <> "") Then
sComputerName = Request("ComputerName")
End If
'log filename specified?
If(Request("LogName")<> "") Then
sLogFile = Request("LogName")
End If
'append computer name and log file
'to WMI query
sQuery = sQuery & """" & sLogFile & """"
'add source, type, and code to query
If(Request("Source")<> "") Then
sQuery = sQuery & " AND SourceName=" & """" & _
Request("Source") & """"
End If
If(Request("Type") <>"") Then
sQuery = sQuery & " AND Type=" & """" & _
Request("Type") & """"
End If
If(Request("EventCode") <>"") Then
sQuery = sQuery & " AND EventCode=" & _
"""" & Request("EventCode") & """"
End If
'username is blank?
If Request.form("UserName") <> "" Then
'no - connect to local machine
Set oServices = oLocator.ConnectServer(sComputerName, _
"root\default", Request.form("UserName"), _
Request.Form("Password"))
Else
'yes = connect to local computer
Set oServices = oLocator.ConnectServer(sComputerName )
End If
'execute query
Set oResultset = oServices.ExecQuery(sQuery)
'any results?
If(oResultset.Count = 0) Then
'no
Response.Write "<b>Query returned 0 records.</b>"
Else
'yes - display results
'build table header
Response.Write "<TABLE cellspacing=0 cellpadding=3 border=1>"
'build first table row
Response.Write "<TR>"
Response.Write "<TH>Record</TH>"
Response.Write "<TH>Type</TH>"
Response.Write "<TH>Date</TH>"
Response.Write "<TH>Time</TH>"
Response.Write "<TH>Source</TH>"
Response.Write "<TH>Category</TH>"
Response.Write "<TH>Cat Strg</TH>"
Response.Write "<TH>Event</TH>"
Response.Write "<TH>Usr</TH>"
Response.Write "<TH>Computer</TH>"
Response.Write "<TH>Msg</TH>"
Response.Write "</TR>"
'go through each event entry
For Each oRecord In oResultset
'Format the date and time of the entry
dtDate = CDateWMI(oRecord.TimeGenerated)
dtTime = CTimeWMI(oRecord.TimeGenerated)
'write row tag
Response.Write "<TR>"
'write cell tag & record information
Response.Write "<TD>" & oRecord.RecordNumber &" </TD>" & _
"<TD>" & oRecord.Type & "</TD>" & _
"<TD>" & dtDate & "</TD>" & _
"<TD>" & dtTime & "</TD>" & _
"<TD>" & oRecord.SourceName & "</TD>" & _
"<TD>" & oRecord.Category & "</TD>" & _
"<TD>" & oRecord.CategoryString & "</TD>" & _
"<TD>" & oRecord.EventCode & "</TD>" & _
"<TD>" & oRecord.User & "</TD>" & _
"<TD>" & oRecord.ComputerName & "</TD>" & _
"<TD>" & oRecord.Message & "</TD></TR>"
Next
'close the table
Response.Write "</TABLE> </FONT>"
End If
There are a couple of interesting blocks of code here. Remember that prototype HTML I cooked up for the table header? Here, it's being dynamically written out by using Response.Write statements.
'build first table row
Response.Write "<TR>"
Response.Write "<TH>Record</TH>"
Response.Write "<TH>Type</TH>"
Response.Write "<TH>Date</TH>"
Response.Write "<TH>Time</TH>"
Response.Write "<TH>Source</TH>"
Response.Write "<TH>Category</TH>"
Response.Write "<TH>Cat Strg</TH>"
Response.Write "<TH>Event</TH>"
Response.Write "<TH>Usr</TH>"
Response.Write "<TH>Computer</TH>"
Response.Write "<TH>Msg</TH>"
Response.Write "</TR>"
A similar chunk of code writes out the data rows, using <TD> tags and the properties of the oRecord object, which is used to represent a single event log entry.
'write cell tag & record information
Response.Write "<TD>" & oRecord.RecordNumber &" </TD>" & _
"<TD>" & oRecord.Type & "</TD>" & _
"<TD>" & dtDate & "</TD>" & _
"<TD>" & dtTime & "</TD>" & _
"<TD>" & oRecord.SourceName & "</TD>" & _
"<TD>" & oRecord.Category & "</TD>" & _
"<TD>" & oRecord.CategoryString & "</TD>" & _
"<TD>" & oRecord.EventCode & "</TD>" & _
"<TD>" & oRecord.User & "</TD>" & _
"<TD>" & oRecord.ComputerName & "</TD>" & _
"<TD>" & oRecord.Message & "</TD></TR>"
I've now got my entire script spread throughout various files. It's time to bring them all together.
|