Listing printers, drivers and ports (or: accessing multiple it references in nested relevance)

(imported topic written by jcoon)

I have a relevance statement I’m trying to construct that returns the printers on a computer along with each printer’s driver and port address if any. I can’t find any way to reference multiple different ‘it’ objects like you usually would in nested for loops. Basically I want to produce output like this:

Front Office Printer || HP LaserJet 4050 Series PS || 192.168.1.150

Here’s the relevance I’ve crafted so far:

if (exists wmi) then
(
(string value of property “Name” of it) & " || " & (string value of property “DriverName” of it) & ((" || " & (string value of property “HostAddress” of select object (“Name, HostAddress from Win32_TCPIPPrinterPort WHERE Name=’” & (string value of property “PortName” of [it from Win32_Printer]) & “’”) of wmi)) | “”)
) of (select objects “PortName,Name,DriverName from Win32_Printer” of wmi)
else nothings

Here’s how I would have done it in vbscript:

Set objWMIService = GetObject(“winmgmts:\.\root\cimv2”)
Set colPrinters = objWMIService.ExecQuery(“Select * from Win32_Printer”)

   strInventory = ""
  
   For Each objPrinter in colPrinters
   Set colPorts = objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort WHERE Name='" & objPrinter.PortName & "'")
   strPrinter = objPrinter.Name & " || " & objPrinter.DriverName
   For Each objPort in colPorts
   strPrinter = objPrinter.Name & " || " & objPrinter.DriverName & " || " & objPort.HostAddress
   Next
   strInventory = strInventory & strPrinter & vbCrLf
   Next
  
   MsgBox strInventory

(imported comment written by NoahSalzman)

Something like this, perhaps?

if (exists wmi) then ((string value of property "Name" of it) & " || " & (string value of property "DriverName" of it) & ((" || " & (string value of property "HostAddress" of select object ("Name, HostAddress from Win32_TCPIPPrinterPort WHERE Name='" & (string value of property "PortName" of select objects "* from Win32_Printer" of wmi) & "'") of wmi)))) of (select objects "PortName,Name,DriverName from Win32_Printer" of wmi) else nothings

(imported comment written by jcoon)

That statement will work if there is exactly one printer; otherwise it throws a “Singular expression refers to non-unique object” error.

I could make a vbs script write the inventory data out to an xml document and read that; is there any way to ensure something like that happens automatically when the refresh starts? Or use the output of a shell command?

(imported comment written by NoahSalzman)

How about this:

q: ( item 0 of item 0 of it, item 1 of item 0 of it, item 0 of item 1 of it, item 1 of item 1 of it ) of ((string value of property "Name" of it , string value of property "DriverName" of it, string value of property "PortName" of it ) of select objects "PortName,DriverName,Name from Win32_Printer" of wmi , (string value of property "HostAddress" of it, string value of property "Name" of it ) of select objects "Name, HostAddress from Win32_TCPIPPrinterPort" of wmi) whose ( item 2 of item 0 of it = item 1 of item 1 of it )
A: Lexmark X422 (MS), Lexmark X422 (MS), 2ndFloor, 9.39.113.21
A: Lexmark C524 (MS), Lexmark C524 (MS), LexmarkColor, 9.39.113.22

(imported comment written by jcoon)

This one works; thanks!

It does skip the printers without IP ports but that’s fine for what I need.

(imported comment written by jgstew)

Related:

Printer Audit - Windows: http://bigfix.me/analysis/details/2994741

Win32_TCPIPPrinterPort class: http://msdn.microsoft.com/en-us/library/aa394492(v=vs.85).aspx

Win32_Printer class: http://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

I did something similar using the registry to pull locally installed printers, the driver, and the IP address for all listed printers…

(value “name” of it, value “printer driver” of it, value “port” of it) of keys whose (value “Port” of it as string contains “”) of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers” of registry as string

I created this as a managed property and then use web reports to look at the information… This is helpful when we rebuild a computer so we can reinstall printers for our users…