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.

695 lines
20 KiB

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. ' Abstract:
  6. ' prnjobs.vbs - job control script for WMI on Whistler
  7. ' used to pause, resume, cancel and list jobs
  8. '
  9. ' Usage:
  10. ' prnjobs [-zmxl?] [-s server] [-p printer] [-j jobid] [-u user name] [-w password]
  11. '
  12. ' Examples:
  13. ' prnjobs -z -j jobid -p printer
  14. ' prnjobs -l -p printer
  15. '
  16. '----------------------------------------------------------------------
  17. option explicit
  18. '
  19. ' Debugging trace flags, to enable debug output trace message
  20. ' change gDebugFlag to true.
  21. '
  22. const kDebugTrace = 1
  23. const kDebugError = 2
  24. dim gDebugFlag
  25. gDebugFlag = false
  26. '
  27. ' Operation action values.
  28. '
  29. const kActionUnknown = 0
  30. const kActionPause = 1
  31. const kActionResume = 2
  32. const kActionCancel = 3
  33. const kActionList = 4
  34. const kErrorSuccess = 0
  35. const KErrorFailure = 1
  36. const kNameSpace = "root\cimv2"
  37. '
  38. ' Generic strings
  39. '
  40. const L_Empty_Text = ""
  41. const L_Space_Text = " "
  42. const L_Error_Text = "Error"
  43. const L_Success_Text = "Success"
  44. const L_Failed_Text = "Failed"
  45. const L_Hex_Text = "0x"
  46. const L_Printer_Text = "Printer"
  47. const L_Operation_Text = "Operation"
  48. const L_Provider_Text = "Provider"
  49. const L_Description_Text = "Description"
  50. const L_Debug_Text = "Debug:"
  51. '
  52. ' General usage messages
  53. '
  54. const L_Help_Help_General01_Text = "Usage: prnjobs [-zmxl?] [-s server][-p printer][-j jobid][-u user name][-w password]"
  55. const L_Help_Help_General02_Text = "Arguments:"
  56. const L_Help_Help_General03_Text = "-j - job id"
  57. const L_Help_Help_General04_Text = "-l - list all jobs"
  58. const L_Help_Help_General05_Text = "-m - resume the job"
  59. const L_Help_Help_General06_Text = "-p - printer name"
  60. const L_Help_Help_General07_Text = "-s - server name"
  61. const L_Help_Help_General08_Text = "-u - user name"
  62. const L_Help_Help_General09_Text = "-w - password"
  63. const L_Help_Help_General10_Text = "-x - cancel the job"
  64. const L_Help_Help_General11_Text = "-z - pause the job"
  65. const L_Help_Help_General12_Text = "-? - display command usage"
  66. const L_Help_Help_General13_Text = "Examples:"
  67. const L_Help_Help_General14_Text = "prnjobs -z -p printer -j jobid"
  68. const L_Help_Help_General15_Text = "prnjobs -l -p printer"
  69. const L_Help_Help_General16_Text = "prnjobs -l"
  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 = "Win32 error code"
  85. '
  86. ' Miscellaneous messages
  87. '
  88. const L_Text_Msg_General01_Text = "Unable to enumarate print jobs"
  89. const L_Text_Msg_General02_Text = "Number of print jobs enumerated"
  90. const L_Text_Msg_General03_Text = "Unable to set print job"
  91. const L_Text_Msg_General04_Text = "Unable to get SWbemLocator object"
  92. const L_Text_Msg_General05_Text = "Unable to connect to WMI service"
  93. '
  94. ' Print job properties
  95. '
  96. const L_Text_Msg_Job01_Text = "Job id"
  97. const L_Text_Msg_Job02_Text = "Printer"
  98. const L_Text_Msg_Job03_Text = "Document"
  99. const L_Text_Msg_Job04_Text = "Data type"
  100. const L_Text_Msg_Job05_Text = "Driver name"
  101. const L_Text_Msg_Job06_Text = "Description"
  102. const L_Text_Msg_Job07_Text = "Elaspsed time"
  103. const L_Text_Msg_Job08_Text = "Machine name"
  104. const L_Text_Msg_Job09_Text = "Job status"
  105. const L_Text_Msg_Job10_Text = "Notify"
  106. const L_Text_Msg_Job11_Text = "Owner"
  107. const L_Text_Msg_Job12_Text = "Pages printed"
  108. const L_Text_Msg_Job13_Text = "Parameters"
  109. const L_Text_Msg_Job14_Text = "Size"
  110. const L_Text_Msg_Job15_Text = "Start time"
  111. const L_Text_Msg_Job16_Text = "Until time"
  112. const L_Text_Msg_Job17_Text = "Status"
  113. const L_Text_Msg_Job18_Text = "Status mask"
  114. const L_Text_Msg_Job19_Text = "Time submitted"
  115. const L_Text_Msg_Job20_Text = "Total pages"
  116. '
  117. ' Action strings
  118. '
  119. const L_Text_Action_General01_Text = "Pause"
  120. const L_Text_Action_General02_Text = "Resume"
  121. const L_Text_Action_General03_Text = "Cancel"
  122. '
  123. ' Debug messages
  124. '
  125. const L_Text_Dbg_Msg01_Text = "In function ListJobs"
  126. const L_Text_Dbg_Msg02_Text = "In function ExecJob"
  127. const L_Text_Dbg_Msg03_Text = "In function ParseCommandLine"
  128. main
  129. '
  130. ' Main execution starts here
  131. '
  132. sub main
  133. dim iAction
  134. dim iRetval
  135. dim strServer
  136. dim strPrinter
  137. dim strJob
  138. dim strUser
  139. dim strPassword
  140. '
  141. ' Abort if the host is not cscript
  142. '
  143. if not IsHostCscript() then
  144. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  145. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  146. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  147. wscript.quit
  148. end if
  149. iRetval = ParseCommandLine(iAction, strServer, strPrinter, strJob, strUser, strPassword)
  150. if iRetval = kErrorSuccess then
  151. select case iAction
  152. case kActionPause
  153. iRetval = ExecJob(strServer, strJob, strPrinter, strUser, strPassword, L_Text_Action_General01_Text)
  154. case kActionResume
  155. iRetval = ExecJob(strServer, strJob, strPrinter, strUser, strPassword, L_Text_Action_General02_Text)
  156. case kActionCancel
  157. iRetval = ExecJob(strServer, strJob, strPrinter, strUser, strPassword, L_Text_Action_General03_Text)
  158. case kActionList
  159. iRetval = ListJobs(strServer, strPrinter, strUser, strPassword)
  160. case else
  161. Usage(true)
  162. exit sub
  163. end select
  164. end if
  165. end sub
  166. '
  167. ' Enumerate all print jobs on a printer
  168. '
  169. function ListJobs(strServer, strPrinter, strUser, strPassword)
  170. on error resume next
  171. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  172. dim Jobs
  173. dim oJob
  174. dim oService
  175. dim iRetval
  176. dim strTemp
  177. dim iTotal
  178. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  179. set Jobs = oService.InstancesOf("Win32_PrintJob")
  180. else
  181. ListJobs = kErrorFailure
  182. exit function
  183. end if
  184. if Err.Number <> kErrorSuccess then
  185. wscript.echo L_Text_Msg_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  186. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  187. ListJobs = kErrorFailure
  188. exit function
  189. end if
  190. iTotal = 0
  191. for each oJob in Jobs
  192. '
  193. ' oJob.Name has the form "printer name, job id". We are isolating the printer name
  194. '
  195. strTemp = Mid(oJob.Name, 1, InStr(1, oJob.Name, ",", 1)-1 )
  196. '
  197. ' If no printer was specified, then enumerate all jobs
  198. '
  199. if strPrinter = null or strPrinter = "" or LCase(strTemp) = LCase(strPrinter) then
  200. iTotal = iTotal + 1
  201. wscript.echo L_Empty_Text
  202. wscript.echo L_Text_Msg_Job01_Text & L_Space_Text & oJob.JobId
  203. wscript.echo L_Text_Msg_Job02_Text & L_Space_Text & strTemp
  204. wscript.echo L_Text_Msg_Job03_Text & L_Space_Text & oJob.Document
  205. wscript.echo L_Text_Msg_Job04_Text & L_Space_Text & oJob.DataType
  206. wscript.echo L_Text_Msg_Job05_Text & L_Space_Text & oJob.DriverName
  207. wscript.echo L_Text_Msg_Job06_Text & L_Space_Text & oJob.Description
  208. wscript.echo L_Text_Msg_Job07_Text & L_Space_Text & Mid(CStr(oJob.ElapsedTime), 9, 2) & ":" _
  209. & Mid(CStr(oJob.ElapsedTime), 11, 2) & ":" _
  210. & Mid(CStr(oJob.ElapsedTime), 13, 2)
  211. wscript.echo L_Text_Msg_Job08_Text & L_Space_Text & oJob.HostPrintQueue
  212. wscript.echo L_Text_Msg_Job09_Text & L_Space_Text & oJob.JobStatus
  213. wscript.echo L_Text_Msg_Job10_Text & L_Space_Text & oJob.Notify
  214. wscript.echo L_Text_Msg_Job11_Text & L_Space_Text & oJob.Owner
  215. wscript.echo L_Text_Msg_Job12_Text & L_Space_Text & oJob.PagesPrinted
  216. wscript.echo L_Text_Msg_Job13_Text & L_Space_Text & oJob.Parameters
  217. wscript.echo L_Text_Msg_Job14_Text & L_Space_Text & oJob.Size
  218. if CStr(oJob.StartTime) <> "********000000.000000+000" and _
  219. CStr(oJob.UntilTime) <> "********000000.000000+000" then
  220. wscript.echo L_Text_Msg_Job15_Text & L_Space_Text & Mid(Mid(CStr(oJob.StartTime), 9, 4), 1, 2) & "h" _
  221. & Mid(Mid(CStr(oJob.StartTime), 9, 4), 3, 2)
  222. wscript.echo L_Text_Msg_Job16_Text & L_Space_Text & Mid(Mid(CStr(oJob.UntilTime), 9, 4), 1, 2) & "h" _
  223. & Mid(Mid(CStr(oJob.UntilTime), 9, 4), 3, 2)
  224. end if
  225. wscript.echo L_Text_Msg_Job17_Text & L_Space_Text & oJob.Status
  226. wscript.echo L_Text_Msg_Job18_Text & L_Space_Text & oJob.StatusMask
  227. wscript.echo L_Text_Msg_Job19_Text & L_Space_Text & Mid(CStr(oJob.TimeSubmitted), 5, 2) & "/" _
  228. & Mid(CStr(oJob.TimeSubmitted), 7, 2) & "/" _
  229. & Mid(CStr(oJob.TimeSubmitted), 1, 4) & " " _
  230. & Mid(CStr(oJob.TimeSubmitted), 9, 2) & ":" _
  231. & Mid(CStr(oJob.TimeSubmitted), 11, 2) & ":" _
  232. & Mid(CStr(oJob.TimeSubmitted), 13, 2)
  233. wscript.echo L_Text_Msg_Job20_Text & L_Space_Text & oJob.TotalPages
  234. Err.Clear
  235. end if
  236. next
  237. wscript.echo L_Empty_Text
  238. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & iTotal
  239. ListJobs = kErrorSuccess
  240. end function
  241. '
  242. ' Pause/Resume/Cancel jobs
  243. '
  244. function ExecJob(strServer, strJob, strPrinter, strUser, strPassword, strCommand)
  245. on error resume next
  246. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text
  247. dim oJob
  248. dim oService
  249. dim iRetval
  250. dim uResult
  251. dim strName
  252. '
  253. ' Build up the key. The key for print jobs is "printer-name, job-id"
  254. '
  255. strName = strPrinter & ", " & strJob
  256. iRetval = kErrorFailure
  257. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  258. set oJob = oService.Get("Win32_PrintJob.Name='" & strName & "'")
  259. else
  260. ExecJob = kErrorFailure
  261. exit function
  262. end if
  263. '
  264. ' Check if getting job instance succeeded
  265. '
  266. if Err.Number = kErrorSuccess then
  267. uResult = kErrorSuccess
  268. select case strCommand
  269. case L_Text_Action_General01_Text
  270. uResult = oJob.Pause()
  271. case L_Text_Action_General02_Text
  272. uResult = oJob.Resume()
  273. case L_Text_Action_General03_Text
  274. oJob.Delete_()
  275. case else
  276. Usage(true)
  277. end select
  278. if Err.Number = kErrorSuccess then
  279. if uResult = kErrorSuccess then
  280. wscript.echo L_Success_Text & L_Space_Text & strCommand & L_Space_Text _
  281. & L_Text_Msg_Job01_Text & L_Space_Text & strJob _
  282. & L_Space_Text & L_Printer_Text & L_Space_Text & strPrinter
  283. iRetval = kErrorSuccess
  284. else
  285. wscript.echo L_Failed_Text & L_Space_Text & strCommand & L_Space_Text _
  286. & L_Text_Error_General03_Text & L_Space_Text & uResult
  287. end if
  288. else
  289. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  290. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  291. '
  292. ' Try getting extended error information
  293. '
  294. call LastError()
  295. end if
  296. else
  297. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  298. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  299. '
  300. ' Try getting extended error information
  301. '
  302. call LastError()
  303. end if
  304. ExecJob = iRetval
  305. end function
  306. '
  307. ' Debug display helper function
  308. '
  309. sub DebugPrint(uFlags, strString)
  310. if gDebugFlag = true then
  311. if uFlags = kDebugTrace then
  312. wscript.echo L_Debug_Text & L_Space_Text & strString
  313. end if
  314. if uFlags = kDebugError then
  315. if Err <> 0 then
  316. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  317. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  318. & L_Space_Text & Err.Description
  319. end if
  320. end if
  321. end if
  322. end sub
  323. '
  324. ' Parse the command line into its components
  325. '
  326. function ParseCommandLine(iAction, strServer, strPrinter, strJob, strUser, strPassword)
  327. on error resume next
  328. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
  329. dim oArgs
  330. dim iIndex
  331. iAction = kActionUnknown
  332. iIndex = 0
  333. set oArgs = wscript.Arguments
  334. while iIndex < oArgs.Count
  335. select case oArgs(iIndex)
  336. case "-z"
  337. iAction = kActionPause
  338. case "-m"
  339. iAction = kActionResume
  340. case "-x"
  341. iAction = kActionCancel
  342. case "-l"
  343. iAction = kActionList
  344. case "-p"
  345. iIndex = iIndex + 1
  346. strPrinter = oArgs(iIndex)
  347. case "-s"
  348. iIndex = iIndex + 1
  349. strServer = RemoveBackslashes(oArgs(iIndex))
  350. case "-j"
  351. iIndex = iIndex + 1
  352. strJob = oArgs(iIndex)
  353. case "-u"
  354. iIndex = iIndex + 1
  355. strUser = oArgs(iIndex)
  356. case "-w"
  357. iIndex = iIndex + 1
  358. strPassword = oArgs(iIndex)
  359. case "-?"
  360. Usage(true)
  361. exit function
  362. case else
  363. Usage(true)
  364. exit function
  365. end select
  366. iIndex = iIndex + 1
  367. wend
  368. if Err.Number = kErrorSuccess then
  369. ParseCommandLine = kErrorSuccess
  370. else
  371. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  372. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  373. ParseCommandLine = kErrorFailure
  374. end if
  375. end function
  376. '
  377. ' Display command usage.
  378. '
  379. sub Usage(bExit)
  380. wscript.echo L_Help_Help_General01_Text
  381. wscript.echo L_Empty_Text
  382. wscript.echo L_Help_Help_General02_Text
  383. wscript.echo L_Help_Help_General03_Text
  384. wscript.echo L_Help_Help_General04_Text
  385. wscript.echo L_Help_Help_General05_Text
  386. wscript.echo L_Help_Help_General06_Text
  387. wscript.echo L_Help_Help_General07_Text
  388. wscript.echo L_Help_Help_General08_Text
  389. wscript.echo L_Help_Help_General09_Text
  390. wscript.echo L_Help_Help_General10_Text
  391. wscript.echo L_Help_Help_General11_Text
  392. wscript.echo L_Help_Help_General12_Text
  393. wscript.echo L_Empty_Text
  394. wscript.echo L_Help_Help_General13_Text
  395. wscript.echo L_Help_Help_General14_Text
  396. wscript.echo L_Help_Help_General15_Text
  397. wscript.echo L_Help_Help_General16_Text
  398. if bExit then
  399. wscript.quit(1)
  400. end if
  401. end sub
  402. '
  403. ' Determines which program is being used to run this script.
  404. ' Returns true if the script host is cscript.exe
  405. '
  406. function IsHostCscript()
  407. on error resume next
  408. dim strFullName
  409. dim strCommand
  410. dim i, j
  411. dim bReturn
  412. bReturn = false
  413. strFullName = WScript.FullName
  414. i = InStr(1, strFullName, ".exe", 1)
  415. if i <> 0 then
  416. j = InStrRev(strFullName, "\", i, 1)
  417. if j <> 0 then
  418. strCommand = Mid(strFullName, j+1, i-j-1)
  419. if LCase(strCommand) = "cscript" then
  420. bReturn = true
  421. end if
  422. end if
  423. end if
  424. if Err <> 0 then
  425. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  426. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  427. end if
  428. IsHostCscript = bReturn
  429. end function
  430. '
  431. ' Retrieves extended information about the last error that occured
  432. ' during a WBEM operation. The methods that set an SWbemLastError
  433. ' object are GetObject, PutInstance, DeleteInstance
  434. '
  435. sub LastError()
  436. on error resume next
  437. dim oError
  438. set oError = CreateObject("WbemScripting.SWbemLastError")
  439. if Err = kErrorSuccess then
  440. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  441. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  442. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  443. wscript.echo L_Text_Error_General03_Text & L_Space_Text & oError.StatusCode
  444. end if
  445. end sub
  446. '
  447. ' Connects to the WMI service on a server. oService is returned as a service
  448. ' object (SWbemServices)
  449. '
  450. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  451. on error resume next
  452. dim oLocator
  453. dim bResult
  454. oService = null
  455. bResult = false
  456. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  457. if Err = kErrorSuccess then
  458. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  459. if Err = kErrorSuccess then
  460. bResult = true
  461. oService.Security_.impersonationlevel = 3
  462. '
  463. ' Required to perform administrative tasks on the spooler service
  464. '
  465. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  466. Err.Clear
  467. else
  468. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & L_Error_Text _
  469. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  470. & Err.Description
  471. end if
  472. else
  473. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & L_Error_Text _
  474. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  475. & Err.Description
  476. end if
  477. WmiConnect = bResult
  478. end function
  479. '
  480. ' Remove leading "\\" from server name
  481. '
  482. function RemoveBackslashes(strServer)
  483. dim strRet
  484. strRet = strServer
  485. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  486. strRet = Mid(strServer, 3)
  487. end if
  488. RemoveBackslashes = strRet
  489. end function