WMI Scripts

 

WMI - Query Printer

Introduction to Querying Printers with WMI

This WMI script offers a variety of solutions to printer problems on a Windows Network.  WMI provides and impressive range of eight classes of printer object so it is possible to query any aspect of a printer.  To let you into a secret, as I researched the Win32_Printer properties I found printer attributes that I did not even know existed.

Topics for Querying Printers

Scenario - Why do Want to Query a Printer?

Printers and Print Devices, give more than their fair share of administrative problems.  While the most likely solution is to add paper or toner; however, you may want to run a WMI script just in case there is a configuration setting that would help prevent the problem re-occurring.  As with most WMI scripts, the key is identifying the best Win32_Classes to query.  In the case of printer, there are eight Win32 objects to choose from, so if at first you don't succeed try a different class of object.

Example 1 - Query Win32_Printer

I am impressed by the sheer number of properties, from obvious items such as share name and printer driver to little known Horizontal Resolution.

Prerequisites for your Printer WMI Script

The only pre-requisite is that you have a printer attached to the machine where you run the script.  Be aware, there may be a delay while the script checks all possible printer ports.

Instructions for Listing Processes WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide which machine on your network to interrogate and then change line 9:
    strComputer = "." to the name of that machine.
  3. Save the file with a .vbs extension, for example: PrinterWMI.vbs 
  4. Double click PrinterWMI.vbs and check the number of printers and their properties.

Script to List the Processes Running on the Computer

 

 

' PrinterWMI.vbs
' Sample VBScript to interrogate Printer properties with WMI
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - December 2005
' --------------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer, intPrinters

strComputer ="."
intPrinters = 1

' --------------------------------------------
' Pure WMI Section
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")

Call Wait() ' Goto Sub Routine at the end

' On Error Resume Next
For Each objItem In colItems
WScript.Echo "Printers on " _
& objItem.name & ", Printer Number: " & intPrinters & VbCr & _
"====================================" & VbCr & _
"Availability: " & objItem.Availability & VbCr & _
"Description: " & objItem.Description & VbCr & _
"Printer: " & objItem.DeviceID & VbCr & _
"Driver Name: " & objItem.DriverName & VbCr & _
"Port Name: " & objItem.PortName & VbCr & _
"Printer State: " & objItem.PrinterState & VbCr & _
"Printer Status: " & objItem.PrinterStatus & VbCr & _
"PrintJobDataType: " & objItem.PrintJobDataType & VbCr & _
"Print Processor: " & objItem.PrintProcessor & VbCr & _
"Spool Enabled: " & objItem.SpoolEnabled & VbCr & _
"Separator File: " & objItem.SeparatorFile & VbCr & _
"Queued: " & objItem.Queued & VbCr & _
"Status: " & objItem.Status & VbCr & _
"StatusInfo: " & objItem.StatusInfo & VbCr & _
"Published: " & objItem.Published & VbCr & _
"Shared: " & objItem.Shared & VbCr & _
"ShareName: " & objItem.ShareName & VbCr & _
"Direct: " & objItem.Direct & VbCr & _
"Location: " & objItem.Location & VbCr & _
"Priority: " & objItem.Priority & VbCr & _
"Work Offline: " & objItem.WorkOffline & VbCr & _
"Horizontal Res: " & objItem.HorizontalResolution & VbCr & _
"Vertical Res: " & objItem.VerticalResolution & VbCr & _
""
intPrinters = intPrinters + 1
Next

sub Wait()
If strComputer = "." then
strComputer = "Local Host"
else strComputer = strComputer
end if

WScript.Echo "Wait 2 mins for " & strComputer _
& " to enumerate printers"

End Sub

WScript.Quit

' End of Sample Printer VBScript


'

WMI Tutorial - Learning Points

From a WMI perspective

1)  If you are new to WMI then you will soon appreciate that all WMI scripts begin by instructing winmgmts to access the root of the CIM library, here is the command:
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")

2) If you have already looked at WMI scripts, then you will know the importance of Win32_Service and objItem.name.

3) Set colProcess = objWMIService.ExecQuery _ is a standard WMI phrase to prepare for the WQL command:  Select * from Win32_Printer
".  The part we are particularly interested in is _Printer.  WMI has eight different types of printer object, we need to query the Printer component and not the PrinterController or PrinterDriver.

From a VBScript perspective

4) What makes scripting so powerful is the speed with which VBScript loops through an array of properties, in this instance the loop is controlled by: For Each....In... Next.

5) In this script I have added a sub routine called sub Wait().  In addition to introducing you to the syntax of the sub routine, I wanted to put the extra cosmetic detail at the end of the script.

6) It is also possible to output the WMI information not to the screen but to a file.  VBScript has all the tools you need to create a file and write a service on each line.  Writing to text files with FSO is covered in other VBScripts.

Summary of Querying Printer Properties

This page provides the knowledge you need to query printers with a WMI script.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


See Also


Introduction to WMIDownload my eBook:  Introduction to WMI - only  $6.25

30+ scripts to get you started with WMI.  Topics include memory, disk, process, and, File System Object.

In addition to the ebook, you get a PDF and version of Introduction to WMI.

 

 

 *


Google

WebComputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.