Link to home
Start Free TrialLog in
Avatar of IM&T SRFT
IM&T SRFT

asked on

Script to check printer status

I have a server that hosts all of the network printers. I want a script that displays the current status of each printer (online, offline, out of paper etc...) to assist the Service Desk in proactively diagnosing faults.

The script I have so far is printer.hta and is displayed below:

<SCRIPT LANGUAGE = "VBScript">
Sub window_onLoad
 GetInfo
 iTimerID = window.setInterval("GetInfo", 60000, "VBScript")
End Sub
Sub GetInfo
 For i = (objTable.Rows.Length - 1) to 0 Step -1
 myNewRow = Document.All.objTable.deleteRow(i)
 Next
 Set objRow = objTableBody.InsertRow()
 objRow.Style.fontWeight = "bold"
 Set objCell = objRow.InsertCell()
 objCell.InnerText = "Name"
 Set objCell = objRow.InsertCell()
 objCell.InnerText = "Location"
 Set objCell = objRow.InsertCell()
 objCell.InnerText = "Status"
 strComputer = "CMRIS"
 Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\cimv2")
 Set colPrinters = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Printer")
 For Each objPrinter in colPrinters
 Set objRow = objTableBody.InsertRow()
 Set objCell = objRow.InsertCell()
 objCell.InnerText = objPrinter.Name
 Set objCell = objRow.InsertCell()
 objCell.InnerText = objPrinter.Location
 Set objCell = objRow.InsertCell()
 Select Case objPrinter.PrinterStatus
 Case 1
 strPrinterStatus = "Other"
 Case 2
 strPrinterStatus = "Unknown"
 Case 3
 strPrinterStatus = "Idle"
 Case 4
 strPrinterStatus = "Printing"
 Case 5
 strPrinterStatus = "Warming up"
 End Select
 objCell.InnerText = strPrinterStatus
 Next
End Sub
</SCRIPT>
<TABLE ID = "objTable" border = "1" >
<TBODY ID = "objTableBody">
</TBODY>
</TABLE>

Open in new window


My problem is I can't seem to get it to display all the printer statuses. Every printer that is not online is showing up as Unknown or Other. I would like it to be a bit more detailed than this.

Also, is it possible to sort them based on the status rather than by printer name?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Sarika30
Sarika30
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IM&T SRFT
IM&T SRFT

ASKER

Where did you get those status codes from?
They seem to conflict with what's written here: http://msdn.microsoft.com/en-us/library/aa387974%28v=VS.85%29.aspx
I got it from here:
http://myitforum.com/cs2/blogs/dhite/archive/2008/08/17/vbs-script-to-retrieve-the-current-status-of-a-specified-printer-share.aspx

But i can see with your link that these might be wrong. You can try with the values given at the Microsoft website and then check if the results are ok. There is one more link, but it is pretty much similar to the link given given by you above:

http://msdn.microsoft.com/en-us/library/aa394363%28VS.85%29.aspx
Is there a way of sorting the list by Status rather than Printer name?
I dont think there is a way to sort them using any query. You will have to use some sorting technique like quick sort or bubble sort etc.