Previous Page
Next Page

Monitoring Print Jobs Step-by-Step Exercises

In this section, you will practice monitoring print jobs by using the Win32_PrintJob WMI class.

1.
Open \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs in Microsoft Notepad or your favorite script editor.

2.
On the first line, type Option Explicit.

3.
On the next line, type On Error Resume Next and then comment it out, so you can see the errors while working with the script.

4.
Save your script as YourNameMonitorPrintJobs.vbs.

5.
Declare the following variables: strComputer, wmiNS, wmiQuery, objWMIService, colItems, and objItem. Your Header information section will look like the following:

Option Explicit
'On Error Resume Next
Dim strComputer
Dim wmiNS
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

6.
Assign the value "." to the variable strComputer, as seen below

strComputer = <;$QD>.<;$QD>

7.
Assign the value "\root\cimv2" to the variable wmiNS, as seen below:

wmiNS = "\root\cimv2"

8.
Assign the string "Select * from Win32_PrintJob" to the wmiQuery variable, as seen below:

wmiQuery = "Select * from win32_PrintJob"

9.
Use objWMIService to hold the SWbemServices object that is returned by the GetObject command when we connect to the root\cimv2 namespace on the local machine. Use the winmgmts moniker to make the connection. Specify the target computer as strComputer. Your code for this will look like the following:

Set objWMIService = GetObject("winmgmts:\\" _
  & strComputer & wmiNS)

10.
Set the variable colItems equal to the object that is returned from the ExecQuery method of objWMIService when it executes the query contained in the variable wmiQuery. Your code will look like the following:

Set colItems = objWMIService.ExecQuery(wmiQuery)

11.
Use the ColItems.Count property to ensure print jobs are in the collection. Implement an If...Then...Else construction to handle this. If there are no print jobs, echo a message to that effect. If there are print jobs, move into a For...Each loop. Your code for this part looks like the following:

If colItems.Count = 0 Then
WScript.Echo("There are no print jobs at this time")
else

12.
Use a For Each...Next construction to iterate through the print jobs contained in the colItems collection. Use the variable objItem to hold each job as you walk through the collection. Echo out the JobId, JobStatus, Owner, and TotalPages properties. Your code for this looks like the following:

For Each objitem In colItems
  WScript.Echo("Print job: " & objItem.JobId)
  WScript.Echo("job status: " & objItem.JobStatus)
  WScript.Echo("Owner: " & objItem.Owner)
  WScript.Echo("Remaining pages: " & objItem.TotalPages)
Next

13.
Close out the If...Then...Else construction by using End If.

14.
Save your work and run the script under CScript. If it does not work as expected, compare it with \My Documents\Microsoft Press\VBScriptSBS\ch18\StepByStep\MonitorPrintJobs.vbs.


Previous Page
Next Page