WMI Printer – Query

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 WMI Printer

 ♦

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 – WMI 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.

VBScript to List WMI Printer Properties

‘ PrinterWMI.vbs
‘ Sample WMI Printer VBScript to interrogate properties
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.3 – December 2010
‘ ———————————————–‘
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

Footnote: 
Harry Stein kindly wrote in pointing out that you get a better formatting result with VbCrLf, rather than plain VbCr. Windows 8 printer troubleshooting.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

WMI Printer – 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. See also SolarWinds SNMP enabler.

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.

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

PowerShell Printer Example : Properties for Win32_Printer

# PowerShell cmdlet to investigate win32_printer properties
# Author: Guy Thomas
$Class = "Win32_printer"
$GMProp = Get-WmiObject -class $Class | gm -memberType property `
| where {$_.name -notmatch "__*"}
$GMProp
$GMProp.count

Learning Points

Note 1:  Use of -notMatch.  "__" needs the star, hence "__*".

Note 2:  By assigning the output to a variable, we can count the number of matching properties.

Note 3: The tiny backtick (`) is useful for explaining to PowerShell that the same command continues on the next line.

Note 4: Here is a more longwinded alternative to using the .count property:
| where {if ($_.name -notmatch "__*") {$i++}}

See more PowerShell printer examples.

Summary of Querying Printer Properties

Printers and Print Devices, give more than their fair share of administrative problems.  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.

If you like this page then please share it with your friends

 


See more VBScript WMI examples:

WMI Tutorial   • Win32_Process   • WMI Memory   • WMI Basics   • Free Download of WMI Monitor

WMI VBS   • VBScript Services   • WMI Disks   • WMI Physical Disks

WMI Home   • WMI Win32   • WMI Printer   • VBScript Echo   • WMI VBScript