This script is designed to be placed on an IIS web server so that the stats can be made available to_
selected users on your Intranet. But it works just as well from XP's built in IIS.
1. Create directory in the root of your web server called "pc" (lower case)
2. Create another directory within /pc called "template"
3. Save the following code into a file called "1.asp" and place in "pc" directory
<% dim strTitle : strTitle = "All Print" %>
<!--#include virtual="pc/template/header.inc"-->
<!-- Paste html Page Content Between These Tags "Below" -->
<%
codeword = "printer101"
IF len(Request.Form("form_codeword"))=0 and Request.Cookies("name") <> "PrinterCounts" THEN
%>
<h2>Login</h2>
<BR>
<CENTER><FONT SIZE=5><B>Login Password</B></FONT></CENTER>
<FORM METHOD="POST" ACTION="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<CENTER><INPUT TYPE="PASSWORD" NAME="form_codeword" SIZE=15></CENTER>
<CENTER><INPUT TYPE="SUBMIT" VALUE="Enter"></CENTER>
</FORM>
<%
ELSE
IF Request.Form("form_codeword") = codeword or Request.Cookies("name") = "PrinterCounts" THEN
%>
<!-- ****** YOUR SECRET PAGE START ****** -->
<%
Dim intDays : intDays = Request.querystring("intDays")
if intDays = "" then intDays = 1
Response.buffer = True
Response.Cookies("name") = "PrinterCounts"
Response.Cookies("name").Expires = Date + 7
%>
<b>| <a href="1.asp?intDays=<%=intDays%>">Home</a> | <a href="4.asp?intDays=<%=intDays%>">Printers</a> | <a href="5.asp?intDays=<%=intDays%>">Users</a> |</b>
<br><br>
<h2>Home</h2><br>
<b>Note: </b>
<ul>
<li> These statistics are only "printing" counts on the main network printers. i.e. they do not include local bubble jet printers.
<li> Monthly totals of all prints including photocopies are collected separately using a different system.
<li> These printing counts do not include photocopies or faxes.
<li> These printing counts do not specify if the print was B&W/Colour or the size of the paper.
<li> These printing counts do not specify if the print was single or double sided.
</ul>
<!-- ****** YOUR SECRET PAGE END ****** -->
<%
ELSE
%>
<BR>
<CENTER><FONT SIZE=5><B>Fail</B></FONT></CENTER>
<CENTER><A HREF="<%=Request.ServerVariables("SCRIPT_NAME")%>">Try Again</A> after you have been given the Password by ITB</CENTER>
<%
END IF
END IF
%>
<!-- Paste html Page Content Between These Tags "Above" -->
<!--#include virtual="pc/template/footer.inc"-->
4. Save the following code into a file called "2.asp" and place in "pc" directory
<% dim strTitle : strTitle = "All Print" %>
<!--#include virtual="pc/template/header.inc"-->
<!-- Paste html Page Content Between These Tags "Below" -->
<%
MyPrinter = Request.querystring("MyPrinter")
if Request.Cookies("name") <> "PrinterCounts" then Response.Redirect "1.asp"
intDays = Request.querystring("intDays")
if intDays=1 then
strDays = "Yesterday | <a href='2.asp?MyPrinter="&MyPrinter&"&intDays=7'>Last Week</a> | <a href='2.asp?MyPrinter="&MyPrinter&"&intDays=28'>Last Month</a>"
end if
if intDays=7 then
strDays = "<a href='2.asp?MyPrinter="&MyPrinter&"&intDays=1'>Yesterday</a> | Last Week | <a href='2.asp?MyPrinter="&MyPrinter&"&intDays=28'>Last Month</a>"
end if
if intDays=28 then
strDays = "<a href='2.asp?MyPrinter="&MyPrinter&"&intDays=1'>Yesterday</a> | <a href='2.asp?MyPrinter="&MyPrinter&"&intDays=7'>Last Week</a> | Last Month"
end if
%>
<b>| <a href="1.asp?intDays=<%=intDays%>">Home</a> | <a href="4.asp?intDays=<%=intDays%>">Printers</a> | <a href="5.asp?intDays=<%=intDays%>">Users</a> | <%=strDays%> | <a href="javascript: history.go(-1)">Back</a> |</b>
<br><br>
<h2>User Printing Totals On - <font color=red><%Response.Write (MyPrinter)%></font></h2><br>
<%response.write "Printing between dates "&date()-intDays&" and "&date()-1&" ("&intDays&"days)"%>
<TABLE border="1">
<TR>
<TD align="right"><B>Users</B></TD>
<TD><B>Total</B></TD>
<TD><B>Details</B></TD>
</TR>
<%
MyDate = date()-intDays
if len(month(MyDate)) =1 then
x= "0"&month(MyDate)
else
x=month(MyDate)
end if
if len(day(MyDate)) =1 then
y= "0"&day(MyDate)
else
y=day(MyDate)
end if
MyDate = year(MyDate)&x&y
'on error resume next
GrandTotal = 0
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("PrinterCounts.mdb")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT First([PrnCounts].[DomainUser]) AS FirstOfDomainUser, Sum([PrnCounts].[NumberPages]) AS SumOfNumberPages "&_
"FROM PrnCounts "&_
"WHERE (((PrnCounts.TimeStampSrv) > '"&MyDate&"')) "&_
"GROUP BY [PrnCounts].[DomainUser], [PrnCounts].[Printer] "&_
"HAVING (((PrnCounts.Printer)='"&MyPrinter&"')) "&_
"ORDER BY Sum(PrnCounts.NumberPages) DESC;"
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<TR>
<TD align="right"><a href="3.asp?MyUser=<%=replace(RS("FirstOfDomainUser"), "\", "-")&"&intDays="&intDays%>"><font face=courier><%=RS("FirstOfDomainUser")%></font></a></TD>
<TD><font face=courier><%=RS("SumOfNumberPages")%> </font></TD>
<TD><a href="6.asp?MyUser=<%=replace(RS("FirstOfDomainUser"), "\", "-")&"&intDays="&intDays&"&MyPrinter="&MyPrinter&""%>"><font face=courier>Click</font></a></TD>
</TR>
<%
GrandTotal = GrandTotal + RS("SumOfNumberPages")
RS.MoveNext
WEND
RS.Close
set RS = nothing
MyConn.close
set MyConn = nothing
'on error goto 0
if GrandTotal = "" then GrandTotal=0
%>
<TR>
<TD align="right"><b>Grand Total</b> </a></TD>
<TD><b><%Response.Write(GrandTotal)%></b> </TD>
<TD><B> </B></TD>
</TR>
</TABLE>
<!-- Paste html Page Content Between These Tags "Above" -->
<!--#include virtual="pc/template/footer.inc"-->
5. Save the following code into a file called "3.asp" and place in "pc" directory
<% dim strTitle : strTitle = "All Print" %>
<!--#include virtual="pc/template/header.inc"-->
<!-- Paste html Page Content Between These Tags "Below" -->
<%
MyUser = replace(Request.querystring("MyUser"), "-", "\")
if Request.Cookies("name") <> "PrinterCounts" then Response.Redirect "1.asp"
intDays = Request.querystring("intDays")
if intDays=1 then
strDays = "Yesterday | <a href='3.asp?Myuser="&MyUser&"&intDays=7'>Last Week</a> | <a href='3.asp?Myuser="&MyUser&"&intDays=28'>Last Month</a>"
end if
if intDays=7 then
strDays = "<a href='3.asp?Myuser="&MyUser&"&intDays=1'>Yesterday</a> | Last Week | <a href='3.asp?Myuser="&MyUser&"&intDays=28'>Last Month</a>"
end if
if intDays=28 then
strDays = "<a href='3.asp?Myuser="&MyUser&"&intDays=1'>Yesterday</a> | <a href='3.asp?Myuser="&MyUser&"&intDays=7'>Last Week</a> | Last Month"
end if
%>
<b>| <a href="1.asp?intDays=<%=intDays%>">Home</a> | <a href="4.asp?intDays=<%=intDays%>">Printers</a> | <a href="5.asp?intDays=<%=intDays%>">Users</a> | <%=strDays%> | <a href="javascript: history.go(-1)">Back</a> |</b>
<br><br>
<h2>Printing Totals For User - <font color=red><%Response.Write(MyUser)%></font></h2><br>
<%response.write "Printing between dates "&date()-intDays&" and "&date()-1&" ("&intDays&"days)"%>
<TABLE border="1">
<TR>
<TD align="Right"><B>Printers</B></TD>
<TD><B>Total</B></TD>
<TD><B>Details</B></TD>
</TR>
<%
MyDate = date()-intDays
if len(month(MyDate)) =1 then
x= "0"&month(MyDate)
else
x=month(MyDate)
end if
if len(day(MyDate)) =1 then
y= "0"&day(MyDate)
else
y=day(MyDate)
end if
MyDate = year(MyDate)&x&y
'on error resume next
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("PrinterCounts.mdb")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT First(PrnCounts.Printer) AS FirstOfPrinter, Sum(PrnCounts.NumberPages) AS SumOfNumberPages "&_
"FROM PrnCounts "&_
"WHERE (((PrnCounts.TimeStampSrv) > '"&MyDate&"')) "&_
"GROUP BY PrnCounts.Printer, PrnCounts.DomainUser "&_
"HAVING (((PrnCounts.DomainUser)='"&MyUser&"')) "&_
"ORDER BY Sum(PrnCounts.NumberPages) DESC;"
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<TR>
<TD align="right"><font face=courier><a href="2.asp?MyPrinter=<%=RS("FirstOfPrinter")&"&intDays="&intDays%>"><%=RS("FirstOfPrinter")%></font></a></TD>
<TD><font face=courier><%=RS("SumOfNumberPages")%> </font></TD>
<TD><a href="6.asp?MyPrinter=<%=replace(RS("FirstOfPrinter"), "\", "-")&"&intDays="&intDays&"&MyUser="&MyUser&""%>"><font face=courier>Click</font></a></TD>
</TR>
<%
GrandTotal = GrandTotal + RS("SumOfNumberPages")
RS.MoveNext
WEND
RS.Close
set RS = nothing
MyConn.close
set MyConn = nothing
'on error goto 0
if GrandTotal = "" then GrandTotal=0
%>
<TR>
<TD align="right"><b>Grand Total</b> </a></TD>
<TD><b><%Response.Write(GrandTotal)%></b> </TD>
<TD><B> </B></TD>
</TR>
</TABLE>
<!-- Paste html Page Content Between These Tags "Above" -->
<!--#include virtual="pc/template/footer.inc"-->
6. Save the following code into a file called "4.asp" and place in "pc" directory
<% dim strTitle : strTitle = "All Print" %>
<!--#include virtual="pc/template/header.inc"-->
<!-- Paste html Page Content Between These Tags "Below" -->
<%
if Request.Cookies("name") <> "PrinterCounts" then Response.Redirect "1.asp"
intDays = Request.querystring("intDays")
strOrder = "Sum(PrnCounts.NumberPages) DESC"
ColSort = Request.querystring("ColSort")
SortOne="4.asp?intDays="&intDays&"&ColSort=OneAZ"
SortTwo="4.asp?intDays="&intDays&"&ColSort=TwoAZ"
If ColSort = "TwoZA" then
strOrder = "Sum(PrnCounts.NumberPages) DESC"
SortTwo="4.asp?intDays="&intDays&"&ColSort=TwoAZ"
end if
if ColSort = "TwoAZ" then
strOrder = "Sum(PrnCounts.NumberPages)"
SortTwo="4.asp?intDays="&intDays&"&ColSort=TwoZA"
end if
if ColSort = "OneZA" then
strOrder = "(PrnCounts.Printer) DESC"
SortOne="4.asp?intDays="&intDays&"&ColSort=OneAZ"
end if
if ColSort = "OneAZ" then
strOrder = "(PrnCounts.Printer)"
SortOne="4.asp?intDays="&intDays&"&ColSort=OneZA"
end if
if intDays=1 then
strDays = "Yesterday | <a href='4.asp?ColSort="&ColSort&"&intDays=7'>Last Week</a> | <a href='4.asp?ColSort="&ColSort&"&intDays=28'>Last Month</a>"
end if
if intDays=7 then
strDays = "<a href='4.asp?ColSort="&ColSort&"&intDays=1'>Yesterday</a> | Last Week | <a href='4.asp?ColSort="&ColSort&"&intDays=28'>Last Month</a>"
end if
if intDays=28 then
strDays = "<a href='4.asp?ColSort="&ColSort&"&intDays=1'>Yesterday</a> | <a href='4.asp?ColSort="&ColSort&"&intDays=7'>Last Week</a> | Last Month"
end if
%>
<b>| <a href="1.asp?intDays=<%=intDays%>">Home</a> | Printers | <a href="5.asp?intDays=<%=intDays%>">Users</a> | <%=strDays%> | <a href="javascript: history.go(-1)">Back</a> |</b>
<br><br>
<h2>Total Jobs Each Printer</h2><br>
<%response.write "Printing between dates "&date()-intDays&" and "&date()-1&" ("&intDays&"days)"%>
<TABLE border="1">
<TR>
<TD align="right"><B><a href="<%=SortOne%>">Printer</a></B></TD>
<TD><B><a href="<%=SortTwo%>">Total</a></B></TD>
</TR>
<%
MyDate = date()-intDays
if len(month(MyDate)) =1 then
x= "0"&month(MyDate)
else
x=month(MyDate)
end if
if len(day(MyDate)) =1 then
y= "0"&day(MyDate)
else
y=day(MyDate)
end if
MyDate = year(MyDate)&x&y
'on error resume next
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("PrinterCounts.mdb")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT First(PrnCounts.Printer) AS FirstOfPrinter, Sum(PrnCounts.NumberPages) AS SumOfNumberPages "&_
"FROM PrnCounts "&_
"WHERE (((PrnCounts.TimeStampSrv) > '"&MyDate&"')) "&_
"GROUP BY PrnCounts.Printer "&_
"HAVING (((Count(PrnCounts.Printer))=1 Or (Count(PrnCounts.Printer))>1)) "&_
"ORDER BY "&strOrder&";"
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<TR>
<TD align="right"><a href="2.asp?MyPrinter=<%=RS("FirstOfPrinter")&"&intDays="&intDays%>"><font face=courier><%=RS("FirstOfPrinter")%><font></a></TD>
<TD><font face=courier><%=RS("SumOfNumberPages")%> </font></TD>
</TR>
<%
GrandTotal = GrandTotal + RS("SumOfNumberPages")
RS.MoveNext
WEND
RS.Close
set RS = nothing
MyConn.close
set MyConn = nothing
'on error goto 0
if GrandTotal = "" then GrandTotal=0
%>
<TR>
<TD align="right"><b>Grand Total</b> </a></TD>
<TD><b><%Response.Write(GrandTotal)%></b> </TD>
</TR>
</TABLE>
<!-- Paste html Page Content Between These Tags "Above" -->
<!--#include virtual="pc/template/footer.inc"-->
7. Save the following code into a file called "5.asp" and place in "pc" directory
<% dim strTitle : strTitle = "All Print" %>
<!--#include virtual="pc/template/header.inc"-->
<!-- Paste html Page Content Between These Tags "Below" -->
<%
if Request.Cookies("name") <> "PrinterCounts" then Response.Redirect "1.asp"
intDays = Request.querystring("intDays")
strOrder = "Sum(PrnCounts.NumberPages) DESC"
ColSort = Request.querystring("ColSort")
SortOne="5.asp?intDays="&intDays&"&ColSort=OneAZ"
SortTwo="5.asp?intDays="&intDays&"&ColSort=TwoAZ"
If ColSort = "TwoZA" then
strOrder = "Sum(PrnCounts.NumberPages) DESC"
SortTwo="5.asp?intDays="&intDays&"&ColSort=TwoAZ"
end if
if ColSort = "TwoAZ" then
strOrder = "Sum(PrnCounts.NumberPages)"
SortTwo="5.asp?intDays="&intDays&"&ColSort=TwoZA"
end if
if ColSort = "OneZA" then
strOrder = "(PrnCounts.DomainUser) DESC"
SortOne="5.asp?intDays="&intDays&"&ColSort=OneAZ"
end if
if ColSort = "OneAZ" then
strOrder = "(PrnCounts.DomainUser)"
SortOne="5.asp?intDays="&intDays&"&ColSort=OneZA"
end if
if intDays=1 then
strDays = "Yesterday | <a href='5.asp?ColSort="&ColSort&"&intDays=7'>Last Week</a> | <a href='5.asp?ColSort="&ColSort&"&intDays=28'>Last Month</a>"
end if
if intDays=7 then
strDays = "<a href='5.asp?ColSort="&ColSort&"&intDays=1'>Yesterday</a> | Last Week | <a href='5.asp?ColSort="&ColSort&"&intDays=28'>Last Month</a>"
end if
if intDays=28 then
strDays = "<a href='5.asp?ColSort="&ColSort&"&intDays=1'>Yesterday</a> | <a href='5.asp?ColSort="&ColSort&"&intDays=7'>Last Week</a> | Last Month"
end if
%>
<b>| <a href="1.asp?intDays=<%=intDays%>">Home</a> | <a href="4.asp?intDays=<%=intDays%>">Printers</a> | Users | <%=strDays%> | <a href="javascript: history.go(-1)">Back</a> |</b>
<br><br>
<h2>Total Jobs Each User</h2><br>
<%response.write "Printing between dates "&date()-intDays&" and "&date()-1&" ("&intDays&"days)"%>
<TABLE border="1">
<TR>
<TD align="right"><B><a href="<%=SortOne%>">User</a></B></TD>
<TD><B><a href="<%=SortTwo%>">Total</a></B></TD>
</TR>
<%
MyDate = date()-intDays
if len(month(MyDate)) =1 then
x= "0"&month(MyDate)
else
x=month(MyDate)
end if
if len(day(MyDate)) =1 then
y= "0"&day(MyDate)
else
y=day(MyDate)
end if
MyDate = year(MyDate)&x&y
'on error resume next
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("PrinterCounts.mdb")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT First(PrnCounts.DomainUser) AS FirstOfDomainUser, Sum(PrnCounts.NumberPages) AS SumOfNumberPages "&_
"FROM PrnCounts "&_
"WHERE (((PrnCounts.TimeStampSrv) > '"&MyDate&"')) "&_
"GROUP BY PrnCounts.DomainUser "&_
"HAVING (((Count(PrnCounts.DomainUser))>1 Or (Count(PrnCounts.DomainUser))=1)) "&_
"ORDER BY "&strOrder&";"
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<TR>
<TD align="right"><a href="3.asp?MyUser=<%=replace(RS("FirstOfDomainUser"), "/", "-")&"&intDays="&intDays%>"><font face=courier><%=RS("FirstOfDomainUser")%><font></a></TD>
<TD><font face=courier><%=RS("SumOfNumberPages")%> </font></TD>
</TR>
<%
GrandTotal = GrandTotal + RS("SumOfNumberPages")
RS.MoveNext
WEND
RS.Close
set RS = nothing
MyConn.close
set MyConn = nothing
if GrandTotal = "" then GrandTotal=0
'on error goto 0
%>
<TR>
<TD align="right"><b>Grand Total</b> </a></TD>
<TD><b><%Response.Write(GrandTotal)%></b> </TD>
</TR>
</TABLE>
<!-- Paste html Page Content Between These Tags "Above" -->
<!--#include virtual="pc/template/footer.inc"-->
8. Save the following code into a file called "6.asp" and place in "pc" directory
<% dim strTitle : strTitle = "All Print" %>
<!--#include virtual="pc/template/header.inc"-->
<!-- Paste html Page Content Between These Tags "Below" -->
<%
if Request.Cookies("name") <> "PrinterCounts" then Response.Redirect "1.asp"
MyUser = replace(Request.querystring("MyUser"), "-", "\")
MyPrinter = Request.querystring("MyPrinter")
intDays = Request.querystring("intDays")
strOrder = "Sum(PrnCounts.NumberPages) DESC"
ColSort = Request.querystring("ColSort")
SortOne="6.asp?intDays="&intDays&"&ColSort=OneAZ&Myuser="&MyUser&"&MyPrinter="&MyPrinter
SortTwo="6.asp?intDays="&intDays&"&ColSort=TwoAZ&Myuser="&MyUser&"&MyPrinter="&MyPrinter
If ColSort = "TwoZA" then
strOrder = "SUM(PrnCounts.NumberPages) DESC"
SortTwo="6.asp?intDays="&intDays&"&ColSort=TwoAZ&Myuser="&MyUser&"&MyPrinter="&MyPrinter
end if
if ColSort = "TwoAZ" then
strOrder = "SUM(PrnCounts.NumberPages)"
SortTwo="6.asp?intDays="&intDays&"&ColSort=TwoZA&Myuser="&MyUser&"&MyPrinter="&MyPrinter
end if
if ColSort = "OneZA" then
strOrder = "(PrnCounts.TimeStampSrv) DESC"
SortOne="6.asp?intDays="&intDays&"&ColSort=OneAZ&Myuser="&MyUser&"&MyPrinter="&MyPrinter
end if
if ColSort = "OneAZ" then
strOrder = "(PrnCounts.TimeStampSrv)"
SortOne="6.asp?intDays="&intDays&"&ColSort=OneZA&Myuser="&MyUser&"&MyPrinter="&MyPrinter
end if
if intDays=1 then
strDays = "Yesterday | <a href='6.asp?Myuser="&MyUser&"&MyPrinter="&MyPrinter&"&ColSort="&ColSort&"&intDays=7'>Last Week</a> | <a href='6.asp?Myuser="&MyUser&"&MyPrinter="&MyPrinter&"&ColSort="&ColSort&"&intDays=28'>Last Month</a>"
end if
if intDays=7 then
strDays = "<a href='6.asp?Myuser="&MyUser&"&MyPrinter="&MyPrinter&"&ColSort="&ColSort&"&intDays=1'>Yesterday</a> | Last Week | <a href='6.asp?Myuser="&MyUser&"&MyPrinter="&MyPrinter&"&ColSort="&ColSort&"&intDays=28'>Last Month</a>"
end if
if intDays=28 then
strDays = "<a href='6.asp?Myuser="&MyUser&"&MyPrinter="&MyPrinter&"&ColSort="&ColSort&"&intDays=1'>Yesterday</a> | <a href='6.asp?Myuser="&MyUser&"&MyPrinter="&MyPrinter&"&ColSort="&ColSort&"&intDays=7'>Last Week</a> | Last Month"
end if
%>
<b>| <a href="1.asp?intDays=<%=intDays%>">Home</a> | <a href="4.asp?intDays=<%=intDays%>">Printers</a> | <a href="5.asp?intDays=<%=intDays%>">Users</a> | <%=strDays%> | <a href="javascript: history.go(-1)">Back</a> |</b>
<br><br>
<h2>Print Jobs by User - <font color=red><%Response.Write(MyUser)%></font> on Printer - <font color=red><%Response.Write(MyPrinter)%></font></h2><br>
<%response.write "Printing between dates "&date()-intDays&" and "&date()-1&" ("&intDays&"days)"%>
<TABLE border="1">
<TR>
<TD align="right"><B><a href="<%=SortOne%>">Time</a></B></TD>
<TD><B><a href="<%=SortTwo%>">Pages</a></B></TD>
<TD><B>Document</B></TD>
</TR>
<%
GrandTotal = 0
MyDate = date()-intDays
if len(month(MyDate)) =1 then
x= "0"&month(MyDate)
else
x=month(MyDate)
end if
if len(day(MyDate)) =1 then
y= "0"&day(MyDate)
else
y=day(MyDate)
end if
MyDate = year(MyDate)&x&y
'on error resume next
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("PrinterCounts.mdb")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT PrnCounts.TimeStampSrv, PrnCounts.DomainUser, PrnCounts.NumberPages, PrnCounts.Printer, PrnCounts.DocumentName "&_
"FROM PrnCounts "&_
"GROUP BY PrnCounts.TimeStampSrv, PrnCounts.DomainUser, PrnCounts.NumberPages, PrnCounts.Printer, PrnCounts.DocumentName, PrnCounts.TimeStampSrv "&_
"HAVING (((PrnCounts.TimeStampSrv)>'"&MyDate&"') AND ((PrnCounts.DomainUser)='"&MyUser&"') AND ((PrnCounts.Printer)='"&MyPrinter&"')) "&_
"ORDER BY "&strOrder&";"
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<TR>
<TD width=200 align="right"><font face=courier><%=ParseTime(RS("TimeStampSrv"))%> </font></a></TD>
<TD><font face=courier><%=RS("NumberPages")%> </font></TD>
<TD><font face=courier><%=RS("DocumentName")%> </font></TD>
</TR>
<%
GrandTotal = abs(GrandTotal) + abs(RS("NumberPages"))
RS.MoveNext
WEND
RS.Close
set RS = nothing
MyConn.close
set MyConn = nothing
'on error goto 0
%>
<TR>
<TD width=200 align="right"><b>Total</b> </a></TD>
<TD><b><%Response.Write(GrandTotal)%></b> </TD>
<TD> </TD>
</TR>
</TABLE>
<%
Function ParseTime(xxx)
' Convert TimeStamp to Real (Australian) Time Day/Month/Year
ParseTime = (Mid(xxx, 7, 2) &"/"& Mid(xxx, 5, 2) &"/"& Left(xxx, 4) &" "& Mid (xxx, 9, 2) &":"& Mid(xxx, 11, 2) &":"& Mid(xxx,13, 2))
end Function
%>
<!-- Paste html Page Content Between These Tags "Above" -->
<!--#include virtual="pc/template/footer.inc"-->
9. Save the following code into a file called "default.asp" and place in "pc" directory
<%Response.Redirect "1.asp"%>
10. Save the following code into a file called "footer.inc" and place in "pc/template" directory
<!--begin footer-->
<br><br><br>
<hr>
<TABLE width="600">
<TR>
<TD><span class="text_footer"><i><b>ITB </b> "emPowering the Business"</i></span></TD>
<TD align="right"><a href="1.asp?intDays=<%=intDays%>"><i><span class="link_footer">Home</a> <a href="javascript: history.go(-1)">Back</a></i> </TD>
</TR>
</TABLE>
</span>
</BODY>
</HTML>
11. Save the following code into a file called "header.inc" and place in "pc/template" directory
<HTML>
<HEAD>
<TITLE><% Response.Write strTitle %></TITLE>
<link rel="stylesheet" href="sup.css">
</HEAD>
<BODY BGCOLOR="#ffffff" >
<span class="text_SUP">
<H1>emPower Printer Counts</H1>
<br>
<!--end header-->
12. Save the following code into a file called "sup.css" and place in "pc" directory
P {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
color: #333333;
}
UL {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
line-height: 15px;
color: #333333;
}
OL {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
line-height: 15px;
color: #333333;
}
BODY {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
color: #333333;
background-image: url(images/sup-wallpaper.jpg);
background-repeat: no-repeat;
background-position: 0px 0px;
}
:link {
Color : blue ;
Text-Decoration : none
}
:active {
Color : blue ;
Text-Decoration : none
}
:visited {
Color : blue ;
Text-Decoration : none
}
A:hover {
Color : red ;
text-decoration : underline
}
.text_SUP{
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
color: #000000;
}
.text_little_date{
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 11px;
color: #00737B;
}
.text_footer{
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
color: #00737B;
}
.link_footer{
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size : 14px;
}
H1 {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
color: #00737B;
text-decoration: bold;
font-size : 20px;
}
H2 {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
color: #00737B;
font-size : 16px;
text-decoration: bold;
}
H3 {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
color: #00737B;
font-size : 14px;
text-decoration: bold;
}
H4 {
font-family : Tahoma, Verdana, Arial, Helvetica, sans-serif;
color: #00737B;
font-size : 12px;
text-decoration: bold;
}
13. Save the following code into a file called "PrinterCounts.vbs" and place in "pc" directory
' Script by TomRiddle 2008
' PrinterCounts.vbs
' Scan the event logs of all your print servers and output to a database file which can be queried to obtain stats.
' Schedule this script to run every morning (early)
' Database file is created by this script. You do not need MS Access installed, but you can view the contents with MS Access.
' Tested in a large environment where database ran suprising fast even with 100,000+ records.
' Part two of this script is an ASP web page which can be run from any XP pc or IIS web server.
' Stats provided are:
' Total Jobs All Printers, Total Jobs All Users, Printer Printing Totals For Specific User, User Printing Totals For Specific Printer and_
' Document names printed on specific printer by specific user
' Each one of these stats can be drilled down to from another stat page and are filtered for prints Yesterday, Last Week or Last Month.
Option Explicit
'Declare Variables
Dim objWMI, objItem
Dim colLoggedEvents
Dim strBase, strAttributes, strQuery
Dim k
Dim objDate, objFSO
'Script Setup
'Dim SUTCTime : SUTCTime = StdToUTCTime(inputbox("Enter Start Date", "Printer Counts", date))
'Dim FUTCTime : FUTCTime = StdToUTCTime(inputbox("Enter Finish Date", "Printer Counts", date-1))
Dim strDB : strDB = "PrinterCounts.mdb"
Dim SUTCTime : SUTCTime = StdToUTCTime(date)
Dim FUTCTime : FUTCTime = StdToUTCTime(cdate(DBReadFirst(strDB)))
Dim intNumberID : intNumberID = "10" ' Event ID Number for Print Jobs
Dim strLogType : strLogType = "System"
Dim PrintServers : PrintServers = "PrnSrv1,PrnSrv2" 'comma separated - Enter all of your network print servers.
'Dim Timer : Timer = now
DBCreate strDB
DBDelOld StdToUTCTime(date-28), strDB
'Feed EACH print server name to event log reader
Dim PSArray : PSArray = split(PrintServers, ",")
For k = 0 To Ubound(PSArray)
EvLFilter PSArray(k), strLogType, intNumberID, SUTCTime, FUTCTime, strDB
Next
'msgbox "Done "&vbcrlf&"Script Time "&now - Timer
'Clean up.
set objFSO = Nothing
WScript.Quit
'-----------------------------------------------------------
Function DBDelOld(OldTime, strDB)
'Delete database records (older than 28days)
Const adOpenStatic = 3
Const adLockOptimistic = 3
Dim objConnection, objRecordSet
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = " &strDB
objRecordSet.Open "DELETE * FROM PrnCounts WHERE " & _
"TimeStampSrv < '"&OldTime&"'", _
objConnection, adOpenStatic, adLockOptimistic
objConnection.Close
End Function
'-----------------------------------------------------------
Function DBReadFirst(strDB)
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Dim objConnection, objRecordSet
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = " &strDB
objRecordSet.Open "SELECT First(PrnCounts.TimeStampSrv) AS FirstOfTimeStampSrv "&_
"FROM PrnCounts "&_
"GROUP BY PrnCounts.TimeStampSrv "&_
"ORDER BY First(PrnCounts.TimeStampSrv) DESC;", _
objConnection, adOpenStatic, adLockOptimistic
objRecordSet.MoveFirst
DBReadFirst = objRecordset.Fields.Item("FirstOfTimeStampSrv")
objRecordSet.Close
objConnection.Close
DBReadFirst=left(DBReadFirst, 8)
DBReadFirst= mid(DBReadFirst, 7,2)&"/"&mid(DBReadFirst, 5,2)&"/"&mid(DBReadFirst, 1,4)
End Function
'-----------------------------------------------------------
Function DBCreate(strDB)
Dim objConnection
'Check if database exists
Set objFSO = CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists(ScriptPath&strDB) Then
'Create PrinterCountsDB Database
Set objConnection = CreateObject("ADOX.Catalog")
objConnection.Create _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = "&strDB
'Create PrinterCountsDB table
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider= Microsoft.Jet.OLEDB.4.0; " & _
"Data Source= "&strDB
'TimeStampServer DomainUser NumberPages Printer DocumentName
objConnection.Execute "CREATE TABLE PrnCounts(" & _
"TimeStampSrv TEXT(50) ," & _
"DomainUser TEXT(50) ," & _
"NumberPages TEXT(5) ," & _
"Printer TEXT(50) ," & _
"DocumentName TEXT(255))"
objConnection.Close
End if
End Function
'-----------------------------------------------------------
Function DatabaseUpdate(TSS,DomainUser,NumberPages,Printer,DocumentName,strDB)
Dim objConnection, objRecordSet
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
'Check if PrintJob has been already added to database
objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = "&strDB
objRecordSet.Open "SELECT PrnCounts.TimeStampSrv FROM PrnCounts WHERE (((PrnCounts.TimeStampSrv)="&Chr(34)&TSS&Chr(34)&"))", _
objConnection, adOpenStatic, adLockOptimistic
On Error Resume NEXT
objRecordSet.MoveFirst
If objRecordSet.Fields.Item("TimeStampSrv")="" Then
On Error Goto 0
objRecordSet.Close
'Add PrintJob to database
objRecordSet.Open "SELECT * FROM PrnCounts" , _
objConnection, adOpenStatic, adLockOptimistic
objRecordSet.AddNew
objRecordSet("TimeStampSrv") = TSS
objRecordSet("DomainUser") = DomainUser
objRecordSet("NumberPages") = NumberPages
objRecordSet("Printer") = Printer
objRecordSet("DocumentName") = left(DocumentName, 254)
objRecordSet.Update
End IF
objRecordSet.Close
objConnection.Close
End Function
'-----------------------------------------------------------
sub EvLFilter(PS, strLogType, intNumberID, SUTCTime, FUTCTime, strDB)
'Loop Through Filtered Event Logs, writing to output log file.
'msgbox "SELECT * FROM Win32_NTLogEvent WHERE LogFile ='" & strLogType & "' AND EventCode = '" & intNumberID & "' AND TimeGenerated <= '" & SUTCTime & "' AND TimeGenerated >= '" & FUTCTime & "'"
Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & PS & "\root\cimv2")
Set colLoggedEvents = objWMI.ExecQuery ("SELECT * FROM Win32_NTLogEvent WHERE LogFile ='" & strLogType &_
"' AND EventCode = '" & intNumberID & "' AND TimeGenerated <= '" & SUTCTime & "' AND TimeGenerated >= '" & FUTCTime & "'")
For Each objItem in colLoggedEvents
' TimeStamp-Server Domain/User Number Pages Printer Document Name Database Name
DatabaseUpdate ParseTime(objItem.TimeGenerated)&PS , objItem.User , ParsePages(objItem.Message) , ParsePrinter(objItem.Message) , ParseDocument(objItem.Message) , strDB
Next
end sub
'-----------------------------------------------------------
Function ParsePrinter(xxx)
' Extract Printer name from event log
Dim y, z
xxx=replace(xxx, vbtab, "")
z=instr(xxx, "was printed on") : y=len(xxx)
xxx=right(xxx, y-z-13)
z=instr(xxx, "via")
ParsePrinter=left(xxx, z-1)
end Function
'-----------------------------------------------------------
Function ParseUser(xxx)
' Extract user name from event log
Dim y, z
xxx=replace(xxx, vbtab, "")
z=instr(xxx, "User Name:") : y=len(xxx)
xxx=right(xxx, y-z-9)
z=instr(xxx, vbcrlf)
ParseUser=left(xxx, z-1)
end Function
'-----------------------------------------------------------
Function ParseDocument(xxx)
' Extract Document name from event log
Dim y, z
xxx=replace(xxx, vbtab, "")
z=instr(xxx, ",") : y=len(xxx)
xxx=right(xxx, y-z-1)
z=instr(xxx, "owned")
ParseDocument=left(xxx, z-1)
end Function
'-----------------------------------------------------------
Function ParsePages(xxx)
' Extract Number of Pages from event log
Dim y, z
xxx=replace(xxx, vbtab, "")
z=instr(xxx, "pages printed:") : y=len(xxx)
ParsePages=trim(right(xxx, y-z-13))
ParsePages=replace(ParsePages, vbcrlf, "")
if instr(ParsePages, "%") then parsepages = "1" '% is due to server logging error - give page count of least one
end Function
'-----------------------------------------------------------
Function ParseTime(xxx)
' Convert UTC time to what I want to input into my database. I will add "print server" to end and make this the DB key.
' i.e. 20080401083055PRNSRV1
ParseTime = Left(xxx, 15)
end Function
'-----------------------------------------------------------
Function StdToUTCTime(inputDT)
' Convert Standard time to UTC format
Dim objTime : Set objTime = CreateObject("WbemScripting.SWbemDateTime")
Dim UTCDateTime: UTCDateTime = objtime.SetVarDate(inputDT)
StdToUTCTime = objTime
End Function
'-----------------------------------------------------------
Function ScriptPath()
ScriptPath = Left(WScript.ScriptFullName, _
Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
End Function
'-----------------------------------------------------------
13. PrinterCounts.vbs needs to be run from a scheduled task (early in the morning each day) to populate the database.
I have a database with ~100,000 entries (print jobs recorded in the last 28days) While it took several hours to_
completely build the database initially, it only takes milliseconds to retrieve the stats. (amazing performance)
[image]local://17407/629A7A935729404783B2B7A6406100DA.jpg[/image]
<message edited by TomRiddle on Friday, April 11, 2008 11:05 PM>