(view source code of printing.vbs as plain text)
Option Explicit
Dim arrStatus(18)
Dim blnActFlush, blnActionSet, blnActPause, blnActResume, blnList
Dim blnOptionSet, blnPrinterSet, blnQuiet, blnUseAllPrn, blnUseDefault
Dim intPrinters, intPrintJobs
Dim colItems1, colItems2, objItem1, objItem2, objWMIService
Dim strAction, strArg, strMsg, strQuery1, strQuery2, strPrinter, strStatus
' Initial values
arrStatus(0) = "-- WMI Error --"
arrStatus(1) = "Other"
arrStatus(2) = "Unknown"
arrStatus(3) = "Idle"
arrStatus(4) = "Printing"
arrStatus(5) = "Warmup"
arrStatus(6) = "Stopped Printing"
arrStatus(7) = "Offline"
arrStatus(8) = "Paused"
arrStatus(9) = "Error"
arrStatus(10) = "Busy"
arrStatus(11) = "NotA vailable"
arrStatus(12) = "Waiting"
arrStatus(13) = "Processing"
arrStatus(14) = "Initialization"
arrStatus(15) = "Power Save"
arrStatus(16) = "Pending Deletion"
arrStatus(17) = "I/O Active"
arrStatus(18) = "Manual Feed"
blnActFlush = False
blnActionSet = False
blnActPause = False
blnActResume = False
blnList = False
blnOptionSet = False
blnPrinterSet = False
blnQuiet = False
blnUseAllPrn = False
blnUseDefault = False
intPrinters = 0
strMsg = ""
strPrinter = ""
' Parse command line
With WScript.Arguments
If .Named.Count = 0 Then Syntax ""
If .Named.Count > 3 Then Syntax "Invalid or duplicate command line switches"
If .Unnamed.Count > 1 Then Syntax "Multiple printer arguments"
If .Unnamed.Count = 1 Then
strPrinter = .Unnamed(0)
blnPrinterSet = True
End If
For Each strArg In .Named
Select Case UCase( strArg )
Case "A", "ALL"
If blnPrinterSet Then Syntax "Multiple printer arguments"
blnUseAllPrn = True
blnPrinterSet = True
Case "D", "DEFAULT"
If blnPrinterSet Then Syntax "Multiple printer arguments"
blnUseDefault = True
blnPrinterSet = True
Case "F", "FLUSH"
If blnActionSet Then Syntax "Multiple action arguments"
strAction = "CancelAllJobs"
blnActFlush = True
blnActionSet = True
Case "L", "LIST"
If blnActionSet Or blnOptionSet Or blnPrinterSet Then Syntax "/LIST switch cannot be combined with other command line arguments"
blnList = True
blnActionSet = True
blnOptionSet = True
blnUseAllPrn = True
blnPrinterSet = True
Case "P", "PAUSE"
If blnActionSet Then Syntax "Multiple action arguments"
strAction = "Pause"
blnActPause = True
blnActionSet = True
Case "Q", "QUIET"
If blnOptionSet Then Syntax "Multiple option arguments"
blnQuiet = True
blnOptionSet = True
Case "R", "RESUME"
If blnActionSet Then Syntax "Multiple action arguments"
strAction = "Resume"
blnActResume = True
blnActionSet = True
Case "V", "VERBOSE"
If blnOptionSet Then Syntax "Multiple option arguments"
blnOptionSet = True
Case Else
Syntax "Invalid command line switch: /" & UCase( strArg )
End Select
Next
End With
If Not blnActionSet Then Syntax "Specify action (/Pause, /Resume or /Flush)"
If Not blnPrinterSet Then Syntax "Specify printer(s) by name or by using /ALL or /DEFAULT switch"
' Prepare query for specified printer(s)
strQuery1 = "SELECT * FROM Win32_Printer"
If blnUseDefault Then
strQuery1 = strQuery1 & " WHERE Default='TRUE'"
ElseIf Not blnUseAllPrn Then
strQuery1 = strQuery1 & " WHERE DeviceID='" & strPrinter & "'"
End If
' Query selected printer(s)
On Error Resume Next
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
Set colItems1 = objWMIService.ExecQuery( strQuery1, "WQL", 48 )
If Err Then Syntax "No matching printer found"
On Error Goto 0
' Iterate through selected printers
For Each objItem1 In colItems1
intPrinters = intPrinters + 1
strPrinter = Trim( objItem1.DeviceID )
strStatus = arrStatus( objItem1.ExtendedPrinterStatus )
strQuery2 = "SELECT * FROM Win32_PrintJob WHERE Name LIKE '" & strPrinter & ", %'"
Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
intPrintJobs = 0
For Each objItem2 In colItems2
intPrintJobs = intPrintJobs + 1
Next
' List
strMsg = strmsg & vbCrLf _
& "Printer : " & strPrinter & vbCrLf _
& "Status : " & strStatus & vbCrLf _
& "Print Jobs : " & intPrintJobs & vbCrLf
' Pause/resume
If ( blnActPause And ( strStatus <> "Paused" ) ) Or ( blnActResume And ( strStatus = "Paused" ) ) Then
strMsg = strMsg & strAction & " printing . . ." & vbCrLf
strQuery2 = "SELECT * FROM Win32_Printer WHERE DeviceID='" & strPrinter & "'"
Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
For Each objItem2 In colItems2
If strAction = "Pause" Then
objItem2.Pause
Else
objItem2.Resume
End If
Next
' Check result
strQuery2 = "SELECT * FROM Win32_Printer WHERE DeviceID='" & strPrinter & "'"
Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
For Each objItem2 In colItems2
strStatus = arrStatus( objItem2.ExtendedPrinterStatus )
Next
strMsg = strMsg & "Status : " & strStatus & vbCrLf
End If
' Flush
If ( blnActFlush And ( intPrintJobs > 0 ) ) Then
strMsg = strMsg & "Flush all print jobs from queue . . ." & vbCrLf
strQuery2 = "SELECT * FROM Win32_Printer WHERE DeviceID='" & strPrinter & "'"
Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
For Each objItem2 In colItems2
objItem2.CancelAllJobs
Next
' Check result
Do Until intPrintJobs = 0
WScript.Sleep 1000
strQuery2 = "SELECT * FROM Win32_PrintJob WHERE Name LIKE '" & strPrinter & ", %'"
Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
intPrintJobs = 0
For Each objItem2 In colItems2
intPrintJobs = intPrintJobs + 1
Next
Loop
strMsg = strMsg & "Print Jobs : " & intPrintJobs & vbCrLf
End If
Next
If intPrinters = 0 Then Syntax "No matching printer found"
If Not blnQuiet Then WScript.Echo strMsg
Set colItems1 = Nothing
Set colItems2 = Nothing
Set objWMIService = Nothing
Sub Syntax( myErrMsg )
Dim strMsg
If Trim( myErrMsg ) <> "" Then strMsg = vbCrLf & "ERROR: " & Trim( myErrMsg ) & vbCrLf
strMsg = strMsg _
& vbCrLf _
& "Printing.vbs, Version 2.20" _
& vbCrLf _
& "Pause or resume printing, or flush all queued printjobs on the specified" _
& vbCrLf _
& "printer(s), or list all printers, their status and number of printjobs" _
& vbCrLf & vbCrLf _
& "Usage: CSCRIPT //NoLogo PRINTING.VBS printer action [ option ]" _
& vbCrLf & vbCrLf _
& " or: CSCRIPT //NoLogo PRINTING.VBS /List" _
& vbCrLf & vbCrLf _
& "Where: ""printer"" is either /All, /Default or a printer name" _
& vbCrLf _
& " ""action"" is either /Pause, /Resume or /Flush" _
& vbCrLf _
& " ""option"" is either /Quiet or /Verbose (default)" _
& vbCrLf & vbCrLf _
& "Notes: Use doublequotes if the printer name contains spaces." _
& vbCrLf _
& " Do not specify a printer when /List switch is used." _
& vbCrLf _
& " Switches may be abbreviated, e.g. /D instead of /Default." _
& vbCrLf _
& " This script may work on Windows 2000 except /Default switch." _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" _
& vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0092 seconds