Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printing.vbs

(view source code of printing.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrStatus(18)
  4. Dim blnActFlush, blnActionSet, blnActPause, blnActResume, blnList
  5. Dim blnOptionSet, blnPrinterSet, blnQuiet, blnUseAllPrn, blnUseDefault
  6. Dim intPrinters, intPrintJobs
  7. Dim colItems1, colItems2, objItem1, objItem2, objWMIService
  8. Dim strAction, strArg, strMsg, strQuery1, strQuery2, strPrinter, strStatus
  9.  
  10. ' Initial values
  11. arrStatus(0)  = "-- WMI Error --"
  12. arrStatus(1)  = "Other"
  13. arrStatus(2)  = "Unknown"
  14. arrStatus(3)  = "Idle"
  15. arrStatus(4)  = "Printing"
  16. arrStatus(5)  = "Warmup"
  17. arrStatus(6)  = "Stopped Printing"
  18. arrStatus(7)  = "Offline"
  19. arrStatus(8)  = "Paused"
  20. arrStatus(9)  = "Error"
  21. arrStatus(10) = "Busy"
  22. arrStatus(11) = "NotA vailable"
  23. arrStatus(12) = "Waiting"
  24. arrStatus(13) = "Processing"
  25. arrStatus(14) = "Initialization"
  26. arrStatus(15) = "Power Save"
  27. arrStatus(16) = "Pending Deletion"
  28. arrStatus(17) = "I/O Active"
  29. arrStatus(18) = "Manual Feed"
  30. blnActFlush   = False
  31. blnActionSet  = False
  32. blnActPause   = False
  33. blnActResume  = False
  34. blnList       = False
  35. blnOptionSet  = False
  36. blnPrinterSet = False
  37. blnQuiet      = False
  38. blnUseAllPrn  = False
  39. blnUseDefault = False
  40. intPrinters   = 0
  41. strMsg        = ""
  42. strPrinter    = ""
  43.  
  44. ' Parse command line
  45. With WScript.Arguments
  46. 	If .Named.Count   = 0 Then Syntax ""
  47. 	If .Named.Count   > 3 Then Syntax "Invalid or duplicate command line switches"
  48. 	If .Unnamed.Count > 1 Then Syntax "Multiple printer arguments"
  49. 	If .Unnamed.Count = 1 Then
  50. 		strPrinter    = .Unnamed(0)
  51. 		blnPrinterSet = True
  52. 	End If
  53. 	For Each strArg In .Named
  54. 		Select Case UCase( strArg )
  55. 			Case "A", "ALL"
  56. 				If blnPrinterSet Then Syntax "Multiple printer arguments"
  57. 				blnUseAllPrn  = True
  58. 				blnPrinterSet = True
  59. 			Case "D", "DEFAULT"
  60. 				If blnPrinterSet Then Syntax "Multiple printer arguments"
  61. 				blnUseDefault = True
  62. 				blnPrinterSet = True
  63. 			Case "F", "FLUSH"
  64. 				If blnActionSet Then Syntax "Multiple action arguments"
  65. 				strAction     = "CancelAllJobs"
  66. 				blnActFlush   = True
  67. 				blnActionSet  = True
  68. 			Case "L", "LIST"
  69. 				If blnActionSet Or blnOptionSet Or blnPrinterSet Then Syntax "/LIST switch cannot be combined with other command line arguments"
  70. 				blnList       = True
  71. 				blnActionSet  = True
  72. 				blnOptionSet  = True
  73. 				blnUseAllPrn  = True
  74. 				blnPrinterSet = True
  75. 			Case "P", "PAUSE"
  76. 				If blnActionSet Then Syntax "Multiple action arguments"
  77. 				strAction    = "Pause"
  78. 				blnActPause  = True
  79. 				blnActionSet = True
  80. 			Case "Q", "QUIET"
  81. 				If blnOptionSet Then Syntax "Multiple option arguments"
  82. 				blnQuiet     = True
  83. 				blnOptionSet = True
  84. 			Case "R", "RESUME"
  85. 				If blnActionSet Then Syntax "Multiple action arguments"
  86. 				strAction    = "Resume"
  87. 				blnActResume = True
  88. 				blnActionSet = True
  89. 			Case "V", "VERBOSE"
  90. 				If blnOptionSet Then Syntax "Multiple option arguments"
  91. 				blnOptionSet = True
  92. 			Case Else
  93. 				Syntax "Invalid command line switch: /" & UCase( strArg )
  94. 		End Select
  95. 	Next
  96. End With
  97. If Not blnActionSet  Then Syntax "Specify action (/Pause, /Resume or /Flush)"
  98. If Not blnPrinterSet Then Syntax "Specify printer(s) by name or by using /ALL or /DEFAULT switch"
  99.  
  100. ' Prepare query for specified printer(s)
  101. strQuery1 = "SELECT * FROM Win32_Printer"
  102. If blnUseDefault Then
  103. 	strQuery1 = strQuery1 & " WHERE Default='TRUE'"
  104. ElseIf Not blnUseAllPrn Then
  105. 	strQuery1 = strQuery1 & " WHERE DeviceID='" & strPrinter & "'"
  106. End If
  107.  
  108. ' Query selected printer(s)
  109. On Error Resume Next
  110. Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
  111. Set colItems1     = objWMIService.ExecQuery( strQuery1, "WQL", 48 )
  112. If Err Then Syntax "No matching printer found"
  113. On Error Goto 0
  114.  
  115. ' Iterate through selected printers
  116. For Each objItem1 In colItems1
  117. 	intPrinters = intPrinters + 1
  118. 	strPrinter = Trim( objItem1.DeviceID )
  119. 	strStatus  = arrStatus( objItem1.ExtendedPrinterStatus )
  120. 	strQuery2  = "SELECT * FROM Win32_PrintJob WHERE Name LIKE '" & strPrinter & ", %'"
  121. 	Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
  122. 	intPrintJobs = 0
  123. 	For Each objItem2 In colItems2
  124. 		intPrintJobs = intPrintJobs + 1
  125. 	Next
  126.  
  127. 	' List
  128. 	strMsg = strmsg & vbCrLf _
  129. 	       & "Printer    : " & strPrinter   & vbCrLf _
  130. 	       & "Status     : " & strStatus    & vbCrLf _
  131. 	       & "Print Jobs : " & intPrintJobs & vbCrLf
  132.  
  133. 	' Pause/resume
  134. 	If ( blnActPause And ( strStatus <> "Paused" ) ) Or ( blnActResume And ( strStatus = "Paused" ) ) Then
  135. 		strMsg    = strMsg & strAction & " printing . . ." & vbCrLf
  136. 		strQuery2 = "SELECT * FROM Win32_Printer WHERE DeviceID='" & strPrinter & "'"
  137. 		Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
  138. 		For Each objItem2 In colItems2
  139. 			If strAction = "Pause" Then
  140. 				objItem2.Pause
  141. 			Else
  142. 				objItem2.Resume
  143. 			End If
  144. 		Next
  145. 		' Check result
  146. 		strQuery2 = "SELECT * FROM Win32_Printer WHERE DeviceID='" & strPrinter & "'"
  147. 		Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
  148. 		For Each objItem2 In colItems2
  149. 			strStatus = arrStatus( objItem2.ExtendedPrinterStatus )
  150. 		Next
  151. 		strMsg = strMsg & "Status     : " & strStatus & vbCrLf
  152. 	End If
  153.  
  154. 	' Flush
  155. 	If ( blnActFlush And ( intPrintJobs > 0 ) ) Then
  156. 		strMsg    = strMsg & "Flush all print jobs from queue . . ." & vbCrLf
  157. 		strQuery2 = "SELECT * FROM Win32_Printer WHERE DeviceID='"   & strPrinter & "'"
  158. 		Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
  159. 		For Each objItem2 In colItems2
  160. 			objItem2.CancelAllJobs
  161. 		Next
  162. 		' Check result
  163. 		Do Until intPrintJobs = 0
  164. 			WScript.Sleep 1000
  165. 			strQuery2 = "SELECT * FROM Win32_PrintJob WHERE Name LIKE '" & strPrinter & ", %'"
  166. 			Set colItems2 = objWMIService.ExecQuery( strQuery2, "WQL", 48 )
  167. 			intPrintJobs = 0
  168. 			For Each objItem2 In colItems2
  169. 				intPrintJobs = intPrintJobs + 1
  170. 			Next
  171. 		Loop
  172. 		strMsg = strMsg & "Print Jobs : " & intPrintJobs & vbCrLf
  173. 	End If
  174. Next
  175. If intPrinters = 0 Then Syntax "No matching printer found"
  176.  
  177. If Not blnQuiet Then WScript.Echo strMsg
  178.  
  179. Set colItems1     = Nothing
  180. Set colItems2     = Nothing
  181. Set objWMIService = Nothing
  182.  
  183.  
  184. Sub Syntax( myErrMsg )
  185. 	Dim strMsg
  186. 	If Trim( myErrMsg ) <> "" Then strMsg = vbCrLf & "ERROR: " & Trim( myErrMsg ) & vbCrLf
  187. 	strMsg = strMsg _
  188. 	       & vbCrLf _
  189. 	       & "Printing.vbs, Version 2.20" _
  190. 	       & vbCrLf _
  191. 	       & "Pause or resume printing, or flush all queued printjobs on the specified" _
  192. 	       & vbCrLf _
  193. 	       & "printer(s), or list all printers, their status and number of printjobs" _
  194. 	       & vbCrLf & vbCrLf _
  195. 	       & "Usage:  CSCRIPT   //NoLogo   PRINTING.VBS   printer   action   [ option ]" _
  196. 	       & vbCrLf & vbCrLf _
  197. 	       & "   or:  CSCRIPT   //NoLogo   PRINTING.VBS   /List" _
  198. 	       & vbCrLf & vbCrLf _
  199. 	       & "Where:  ""printer""  is either /All, /Default or a printer name" _
  200. 	       & vbCrLf _
  201. 	       & "        ""action""   is either /Pause, /Resume or /Flush" _
  202. 	       & vbCrLf _
  203. 	       & "        ""option""   is either /Quiet or /Verbose (default)" _
  204. 	       & vbCrLf & vbCrLf _
  205. 	       & "Notes:  Use doublequotes if the printer name contains spaces." _
  206. 	       & vbCrLf _
  207. 	       & "        Do not specify a printer when /List switch is used." _
  208. 	       & vbCrLf _
  209. 	       & "        Switches may be abbreviated, e.g. /D instead of /Default." _
  210. 	       & vbCrLf _
  211. 	       & "        This script may work on Windows 2000 except /Default switch." _
  212. 	       & vbCrLf & vbCrLf _
  213. 	       & "Written by Rob van der Woude" _
  214. 	       & vbCrLf _
  215. 	       & "http://www.robvanderwoude.com"
  216. 	WScript.Echo strMsg
  217. 	WScript.Quit 1
  218. End Sub
  219.  

page last modified: 2024-04-16; loaded in 0.0288 seconds