Want to list all printers connected to your print server?

Printer OverviewVBScript to the rescue! VBScript ain’t dead. PowerShell is pushed by Microsoft to replace it. But still, even MS uses VBScript. You know the Windows and Office activation scripts? Or the Office 2013 removal fixit tool (when Office 365 causes havoc)? It’s VBScript. Easy to use, stable and available on all Windows versions since ’98 and here to stay. So don’t let anyone tell you not to use it. It’s practical and easier to understand than PowerShell. Looking for a great VBScript editor and debugger to help you make the best scripts? Try VbsEdit.

Enough about that… here is the solution to view all shared network printers on a specific print server:

 

Option Explicit
Dim wmi, cprinters, cports
Dim computer, out, o
Dim dports

If WScript.Arguments.Count > 0 Then
	computer = WScript.Arguments(0)
Else
	computer = "."
End If
Set dports = CreateObject("Scripting.Dictionary")
Set wmi = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
Set cports = wmi.ExecQuery("Select * from Win32_TCPIPPrinterPort")
For Each o In cports
	dports.Add o.Name, o
Next
out = "Name" & vbTab & "Location" & vbTab & "Comment" & vbTab & "Status" & vbTab & "Error" & vbTab & "IP"
WScript.Echo out
Set cprinters = wmi.ExecQuery("Select * from Win32_Printer Where Shared=1")
For Each o In cprinters
	out = o.Name & vbTab & o.Location & vbTab & o.Comment & vbTab & o.PrinterStatus & vbTab & o.DetectedErrorState
	If Not IsEmpty(dports(o.PortName)) Then
		out = out & vbTab & dports(o.PortName).HostAddress
	End If
	WScript.Echo out
Next

Copy & paste this text into your editor (please ban Windows Notepad, instead install Notepad++) and save the file as printers.vbs . Open a command prompt (cmd.exe) and run this command:

cscript //nologo printers.vbs [printserver] > printers.txt

The result is a tab delimited file that you can open in Excel. It will list the name, location, comment, status, error and ip address of all shared printers connected to the print server. For the meaning of the values in the status and error columns take a look at PrinterStatus and DetectedErrorState on http://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx .

First Post… more to follow!

Advertisements

About Stoomkracht

Scripter, programmer, Windows administrator, network/wifi engineer, thinker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: