Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

559 lines
15 KiB

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. ' Abstract:
  6. ' prnqctl.vbs - printer control script for WMI on Whistler
  7. ' used to pause, resume and purge a printer
  8. ' also used to print a test page on a printer
  9. '
  10. ' Usage:
  11. ' prnqctl [-umex?] [-s server] [-p printer] [-u user name] [-w password]
  12. '
  13. ' Examples:
  14. ' prnqctl -m -s server -p printer
  15. ' prnqctl -x -s server -p printer
  16. ' prnqctl -e -b printer
  17. '----------------------------------------------------------------------
  18. option explicit
  19. '
  20. ' Debugging trace flags, to enable debug output trace message
  21. ' change gDebugFlag to true.
  22. '
  23. const kDebugTrace = 1
  24. const kDebugError = 2
  25. dim gDebugFlag
  26. gDebugFlag = false
  27. '
  28. ' Operation action values.
  29. '
  30. const kActionUnknown = 0
  31. const kActionPause = 1
  32. const kActionResume = 2
  33. const kActionPurge = 3
  34. const kActionTestPage = 4
  35. const kErrorSuccess = 0
  36. const KErrorFailure = 1
  37. const kNameSpace = "root\cimv2"
  38. '
  39. ' Generic strings
  40. '
  41. const L_Empty_Text = ""
  42. const L_Space_Text = " "
  43. const L_Error_Text = "Error"
  44. const L_Success_Text = "Success"
  45. const L_Failed_Text = "Failed"
  46. const L_Hex_Text = "0x"
  47. const L_Printer_Text = "Printer"
  48. const L_Operation_Text = "Operation"
  49. const L_Provider_Text = "Provider"
  50. const L_Description_Text = "Description"
  51. const L_Debug_Text = "Debug:"
  52. '
  53. ' General usage messages
  54. '
  55. const L_Help_Help_General01_Text = "Usage: prnqctl [-zmex?] [-s server][-p printer][-u user name][-w password]"
  56. const L_Help_Help_General02_Text = "Arguments:"
  57. const L_Help_Help_General03_Text = "-e - print test page"
  58. const L_Help_Help_General04_Text = "-m - resume the printer"
  59. const L_Help_Help_General05_Text = "-p - printer name"
  60. const L_Help_Help_General06_Text = "-s - server name"
  61. const L_Help_Help_General07_Text = "-u - user name"
  62. const L_Help_Help_General08_Text = "-w - password"
  63. const L_Help_Help_General09_Text = "-x - purge the printer (cancel all jobs)"
  64. const L_Help_Help_General10_Text = "-z - pause the printer"
  65. const L_Help_Help_General11_Text = "-? - display command usage"
  66. const L_Help_Help_General12_Text = "Examples:"
  67. const L_Help_Help_General13_Text = "prnqctl -e -s server -p printer"
  68. const L_Help_Help_General14_Text = "prnqctl -m -p printer"
  69. const L_Help_Help_General15_Text = "prnqctl -x -p printer"
  70. '
  71. ' Messages to be displayed if the scripting host is not cscript
  72. '
  73. const L_Help_Help_Host01_Text = "Please run this script using CScript."
  74. const L_Help_Help_Host02_Text = "This can be achieved by"
  75. const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
  76. const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
  77. const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
  78. const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
  79. '
  80. ' General error messages
  81. '
  82. const L_Text_Error_General01_Text = "The scripting host could not be determined."
  83. const L_Text_Error_General02_Text = "Unable to parse command line."
  84. const L_Text_Error_General03_Text = "Unable to get printer instance."
  85. const L_Text_Error_General04_Text = "Win32 error code"
  86. const L_Text_Error_General05_Text = "Unable to get SWbemLocator object"
  87. const L_Text_Error_General06_Text = "Unable to connect to WMI service"
  88. '
  89. ' Action strings
  90. '
  91. const L_Text_Action_General01_Text = "Pause"
  92. const L_Text_Action_General02_Text = "Resume"
  93. const L_Text_Action_General03_Text = "Purge"
  94. const L_Text_Action_General04_Text = "Print Test Page"
  95. '
  96. ' Debug messages
  97. '
  98. const L_Text_Dbg_Msg01_Text = "In function ExecPrinter"
  99. const L_Text_Dbg_Msg02_Text = "Server name"
  100. const L_Text_Dbg_Msg03_Text = "Printer name"
  101. const L_Text_Dbg_Msg04_Text = "In function ParseCommandLine"
  102. main
  103. '
  104. ' Main execution starts here
  105. '
  106. sub main
  107. dim iAction
  108. dim iRetval
  109. dim strServer
  110. dim strPrinter
  111. dim strUser
  112. dim strPassword
  113. '
  114. ' Abort if the host is not cscript
  115. '
  116. if not IsHostCscript() then
  117. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  118. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  119. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  120. wscript.quit
  121. end if
  122. '
  123. ' Get command line parameters
  124. '
  125. iRetval = ParseCommandLine(iAction, strServer, strPrinter, strUser, strPassword)
  126. if iRetval = kErrorSuccess then
  127. select case iAction
  128. case kActionPause
  129. iRetval = ExecPrinter(strServer, strPrinter, strUser, strPassword, L_Text_Action_General01_Text)
  130. case kActionResume
  131. iRetval = ExecPrinter(strServer, strPrinter, strUser, strPassword, L_Text_Action_General02_Text)
  132. case kActionPurge
  133. iRetval = ExecPrinter(strServer, strPrinter, strUser, strPassword, L_Text_Action_General03_Text)
  134. case kActionTestPage
  135. iRetval = ExecPrinter(strServer, strPrinter, strUser, strPassword, L_Text_Action_General04_Text)
  136. case kActionUnknown
  137. Usage(true)
  138. exit sub
  139. case else
  140. Usage(true)
  141. exit sub
  142. end select
  143. end if
  144. end sub
  145. '
  146. ' Pause/Resume/Purge printer and print test page
  147. '
  148. function ExecPrinter(strServer, strPrinter, strUser, strPassword, strCommand)
  149. on error resume next
  150. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  151. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text & L_Space_Text & strServer
  152. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text & L_Space_Text & strPrinter
  153. dim oPrinter
  154. dim oService
  155. dim iRetval
  156. dim uResult
  157. iRetval = kErrorFailure
  158. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  159. set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")
  160. else
  161. ExecPrinter = kErrorFailure
  162. exit function
  163. end if
  164. '
  165. ' Check if getting a printer instance succeeded
  166. '
  167. if Err.Number = kErrorSuccess then
  168. select case strCommand
  169. case L_Text_Action_General01_Text
  170. uResult = oPrinter.Pause()
  171. case L_Text_Action_General02_Text
  172. uResult = oPrinter.Resume()
  173. case L_Text_Action_General03_Text
  174. uResult = oPrinter.CancelAllJobs()
  175. case L_Text_Action_General04_Text
  176. uResult = oPrinter.PrintTestPage()
  177. case else
  178. Usage(true)
  179. end select
  180. '
  181. ' Err set by WMI
  182. '
  183. if Err.Number = kErrorSuccess then
  184. '
  185. ' uResult set by printer methods
  186. '
  187. if uResult = kErrorSuccess then
  188. wscript.echo L_Success_Text & L_Space_Text & strCommand & L_Space_Text _
  189. & L_Printer_Text & L_Space_Text & strPrinter
  190. iRetval = kErrorSuccess
  191. else
  192. wscript.echo L_Failed_Text & L_Space_Text & strCommand & L_Space_Text _
  193. & L_Text_Error_General04_Text & L_Space_Text & uResult
  194. end if
  195. else
  196. wscript.echo L_Failed_Text & L_Space_Text & strCommand & L_Space_Text & L_Error_Text _
  197. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  198. end if
  199. else
  200. wscript.echo L_Text_Error_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  201. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  202. '
  203. ' Try getting extended error information
  204. '
  205. call LastError()
  206. end if
  207. ExecPrinter = iRetval
  208. end function
  209. '
  210. ' Debug display helper function
  211. '
  212. sub DebugPrint(uFlags, strString)
  213. if gDebugFlag = true then
  214. if uFlags = kDebugTrace then
  215. wscript.echo L_Debug_Text & L_Space_Text & strString
  216. end if
  217. if uFlags = kDebugError then
  218. if Err <> 0 then
  219. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  220. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  221. & L_Space_Text & Err.Description
  222. end if
  223. end if
  224. end if
  225. end sub
  226. '
  227. ' Parse the command line into its components
  228. '
  229. function ParseCommandLine(iAction, strServer, strPrinter, strUser, strPassword)
  230. on error resume next
  231. DebugPrint kDebugTrace, L_Text_Dbg_Msg04_Text
  232. dim oArgs
  233. dim iIndex
  234. iAction = kActionUnknown
  235. iIndex = 0
  236. set oArgs = wscript.Arguments
  237. while iIndex < oArgs.Count
  238. select case oArgs(iIndex)
  239. case "-z"
  240. iAction = kActionPause
  241. case "-m"
  242. iAction = kActionResume
  243. case "-x"
  244. iAction = kActionPurge
  245. case "-e"
  246. iAction = kActionTestPage
  247. case "-p"
  248. iIndex = iIndex + 1
  249. strPrinter = oArgs(iIndex)
  250. case "-s"
  251. iIndex = iIndex + 1
  252. strServer = RemoveBackslashes(oArgs(iIndex))
  253. case "-y"
  254. iIndex = iIndex + 1
  255. strUser = oArgs(iIndex)
  256. case "-w"
  257. iIndex = iIndex + 1
  258. strPassword = oArgs(iIndex)
  259. case "-?"
  260. Usage(true)
  261. exit function
  262. case else
  263. Usage(true)
  264. exit function
  265. end select
  266. iIndex = iIndex + 1
  267. wend
  268. if Err.Number = kErrorSuccess then
  269. ParseCommandLine = kErrorSuccess
  270. else
  271. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  272. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  273. ParseCommandLine = kErrorFailure
  274. end if
  275. end function
  276. '
  277. ' Display command usage.
  278. '
  279. sub Usage(bExit)
  280. wscript.echo L_Help_Help_General01_Text
  281. wscript.echo L_Empty_Text
  282. wscript.echo L_Help_Help_General02_Text
  283. wscript.echo L_Help_Help_General03_Text
  284. wscript.echo L_Help_Help_General04_Text
  285. wscript.echo L_Help_Help_General05_Text
  286. wscript.echo L_Help_Help_General06_Text
  287. wscript.echo L_Help_Help_General07_Text
  288. wscript.echo L_Help_Help_General08_Text
  289. wscript.echo L_Help_Help_General09_Text
  290. wscript.echo L_Help_Help_General10_Text
  291. wscript.echo L_Help_Help_General11_Text
  292. wscript.echo L_Empty_Text
  293. wscript.echo L_Help_Help_General12_Text
  294. wscript.echo L_Help_Help_General13_Text
  295. wscript.echo L_Help_Help_General14_Text
  296. wscript.echo L_Help_Help_General15_Text
  297. if bExit then
  298. wscript.quit(1)
  299. end if
  300. end sub
  301. '
  302. ' Determines which program is being used to run this script.
  303. ' Returns true if the script host is cscript.exe
  304. '
  305. function IsHostCscript()
  306. on error resume next
  307. dim strFullName
  308. dim strCommand
  309. dim i, j
  310. dim bReturn
  311. bReturn = false
  312. strFullName = WScript.FullName
  313. i = InStr(1, strFullName, ".exe", 1)
  314. if i <> 0 then
  315. j = InStrRev(strFullName, "\", i, 1)
  316. if j <> 0 then
  317. strCommand = Mid(strFullName, j+1, i-j-1)
  318. if LCase(strCommand) = "cscript" then
  319. bReturn = true
  320. end if
  321. end if
  322. end if
  323. if Err <> 0 then
  324. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  325. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  326. end if
  327. IsHostCscript = bReturn
  328. end function
  329. '
  330. ' Retrieves extended information about the last error that occured
  331. ' during a WBEM operation. The methods that set an SWbemLastError
  332. ' object are GetObject, PutInstance, DeleteInstance
  333. '
  334. sub LastError()
  335. on error resume next
  336. dim oError
  337. set oError = CreateObject("WbemScripting.SWbemLastError")
  338. if Err = kErrorSuccess then
  339. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  340. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  341. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  342. wscript.echo L_Text_Error_General04_Text & L_Space_Text & oError.StatusCode
  343. end if
  344. end sub
  345. '
  346. ' Connects to the WMI service on a server. oService is returned as a service
  347. ' object (SWbemServices)
  348. '
  349. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  350. on error resume next
  351. dim oLocator
  352. dim bResult
  353. oService = null
  354. bResult = false
  355. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  356. if Err = kErrorSuccess then
  357. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  358. if Err = kErrorSuccess then
  359. bResult = true
  360. oService.Security_.impersonationlevel = 3
  361. '
  362. ' Required to perform administrative tasks on the spooler service
  363. '
  364. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  365. Err.Clear
  366. else
  367. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & L_Error_Text _
  368. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  369. & Err.Description
  370. end if
  371. else
  372. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & L_Error_Text _
  373. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  374. & Err.Description
  375. end if
  376. WmiConnect = bResult
  377. end function
  378. '
  379. ' Remove leading "\\" from server name
  380. '
  381. function RemoveBackslashes(strServer)
  382. dim strRet
  383. strRet = strServer
  384. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  385. strRet = Mid(strServer, 3)
  386. end if
  387. RemoveBackslashes = strRet
  388. end function