Leaked source code of windows server 2003
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.

1425 lines
43 KiB

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. ' Abstract:
  6. ' prnmngr.vbs - printer script for WMI on Whistler
  7. ' used to add, delete, and list printers and connections
  8. ' also for getting and setting the default printer
  9. '
  10. ' Usage:
  11. ' prnmngr [-adxgtl?][co] [-s server][-p printer][-m driver model][-r port]
  12. ' [-u user name][-w password]
  13. '
  14. ' Examples:
  15. ' prnmngr -a -p "printer" -m "driver" -r "lpt1:"
  16. ' prnmngr -d -p "printer" -s server
  17. ' prnmngr -ac -p "\\server\printer"
  18. ' prnmngr -d -p "\\server\printer"
  19. ' prnmngr -x -s server
  20. ' prnmngr -l -s server
  21. ' prnmngr -g
  22. ' prnmngr -t -p "printer"
  23. '
  24. '----------------------------------------------------------------------
  25. option explicit
  26. '
  27. ' Debugging trace flags, to enable debug output trace message
  28. ' change gDebugFlag to true.
  29. '
  30. const kDebugTrace = 1
  31. const kDebugError = 2
  32. dim gDebugFlag
  33. gDebugFlag = false
  34. '
  35. ' Operation action values.
  36. '
  37. const kActionUnknown = 0
  38. const kActionAdd = 1
  39. const kActionAddConn = 2
  40. const kActionDel = 3
  41. const kActionDelAll = 4
  42. const kActionDelAllCon = 5
  43. const kActionDelAllLocal = 6
  44. const kActionList = 7
  45. const kActionGetDefaultPrinter = 8
  46. const kActionSetDefaultPrinter = 9
  47. const kErrorSuccess = 0
  48. const KErrorFailure = 1
  49. const kFlagCreateOnly = 2
  50. const kNameSpace = "root\cimv2"
  51. '
  52. ' Generic strings
  53. '
  54. const L_Empty_Text = ""
  55. const L_Space_Text = " "
  56. const L_Error_Text = "Error"
  57. const L_Success_Text = "Success"
  58. const L_Failed_Text = "Failed"
  59. const L_Hex_Text = "0x"
  60. const L_Printer_Text = "Printer"
  61. const L_Operation_Text = "Operation"
  62. const L_Provider_Text = "Provider"
  63. const L_Description_Text = "Description"
  64. const L_Debug_Text = "Debug:"
  65. const L_Connection_Text = "connection"
  66. '
  67. ' General usage messages
  68. '
  69. const L_Help_Help_General01_Text = "Usage: prnmngr [-adxgtl?][c] [-s server][-p printer][-m driver model]"
  70. const L_Help_Help_General02_Text = " [-r port][-u user name][-w password]"
  71. const L_Help_Help_General03_Text = "Arguments:"
  72. const L_Help_Help_General04_Text = "-a - add local printer"
  73. const L_Help_Help_General05_Text = "-ac - add printer connection"
  74. const L_Help_Help_General06_Text = "-d - delete printer"
  75. const L_Help_Help_General07_Text = "-g - get the default printer"
  76. const L_Help_Help_General08_Text = "-l - list printers"
  77. const L_Help_Help_General09_Text = "-m - driver model"
  78. const L_Help_Help_General10_Text = "-p - printer name"
  79. const L_Help_Help_General11_Text = "-r - port name"
  80. const L_Help_Help_General12_Text = "-s - server name"
  81. const L_Help_Help_General13_Text = "-t - set the default printer"
  82. const L_Help_Help_General14_Text = "-u - user name"
  83. const L_Help_Help_General15_Text = "-w - password"
  84. const L_Help_Help_General16_Text = "-x - delete all printers"
  85. const L_Help_Help_General17_Text = "-xc - delete all printer connections"
  86. const L_Help_Help_General18_Text = "-xo - delete all local printers"
  87. const L_Help_Help_General19_Text = "-? - display command usage"
  88. const L_Help_Help_General20_Text = "Examples:"
  89. const L_Help_Help_General21_Text = "prnmngr -a -p ""printer"" -m ""driver"" -r ""lpt1:"""
  90. const L_Help_Help_General22_Text = "prnmngr -d -p ""printer"" -s server"
  91. const L_Help_Help_General23_Text = "prnmngr -ac -p ""\\server\printer"""
  92. const L_Help_Help_General24_Text = "prnmngr -d -p ""\\server\printer"""
  93. const L_Help_Help_General25_Text = "prnmngr -x -s server"
  94. const L_Help_Help_General26_Text = "prnmngr -xo"
  95. const L_Help_Help_General27_Text = "prnmngr -l -s server"
  96. const L_Help_Help_General28_Text = "prnmngr -g"
  97. const L_Help_Help_General29_Text = "prnmngr -t -p ""\\server\printer"""
  98. '
  99. ' Messages to be displayed if the scripting host is not cscript
  100. '
  101. const L_Help_Help_Host01_Text = "Please run this script using CScript."
  102. const L_Help_Help_Host02_Text = "This can be achieved by"
  103. const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
  104. const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
  105. const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
  106. const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
  107. '
  108. ' General error messages
  109. '
  110. const L_Text_Error_General01_Text = "The scripting host could not be determined."
  111. const L_Text_Error_General02_Text = "Unable to parse command line."
  112. const L_Text_Error_General03_Text = "Win32 error code"
  113. '
  114. ' Miscellaneous messages
  115. '
  116. const L_Text_Msg_General01_Text = "Added printer"
  117. const L_Text_Msg_General02_Text = "Unable to add printer"
  118. const L_Text_Msg_General03_Text = "Added printer connection"
  119. const L_Text_Msg_General04_Text = "Unable to add printer connection"
  120. const L_Text_Msg_General05_Text = "Deleted printer"
  121. const L_Text_Msg_General06_Text = "Unable to delete printer"
  122. const L_Text_Msg_General07_Text = "Attempting to delete printer"
  123. const L_Text_Msg_General08_Text = "Unable to delete printers"
  124. const L_Text_Msg_General09_Text = "Number of local printers and connections enumerated"
  125. const L_Text_Msg_General10_Text = "Number of local printers and connections deleted"
  126. const L_Text_Msg_General11_Text = "Unable to enumerate printers"
  127. const L_Text_Msg_General12_Text = "The default printer is"
  128. const L_Text_Msg_General13_Text = "Unable to get the default printer"
  129. const L_Text_Msg_General14_Text = "Unable to set the default printer"
  130. const L_Text_Msg_General15_Text = "The default printer is now"
  131. const L_Text_Msg_General16_Text = "Number of printer connections enumerated"
  132. const L_Text_Msg_General17_Text = "Number of printer connections deleted"
  133. const L_Text_Msg_General18_Text = "Number of local printers enumerated"
  134. const L_Text_Msg_General19_Text = "Number of local printers deleted"
  135. '
  136. ' Printer properties
  137. '
  138. const L_Text_Msg_Printer01_Text = "Server name"
  139. const L_Text_Msg_Printer02_Text = "Printer name"
  140. const L_Text_Msg_Printer03_Text = "Share name"
  141. const L_Text_Msg_Printer04_Text = "Driver name"
  142. const L_Text_Msg_Printer05_Text = "Port name"
  143. const L_Text_Msg_Printer06_Text = "Comment"
  144. const L_Text_Msg_Printer07_Text = "Location"
  145. const L_Text_Msg_Printer08_Text = "Separator file"
  146. const L_Text_Msg_Printer09_Text = "Print processor"
  147. const L_Text_Msg_Printer10_Text = "Data type"
  148. const L_Text_Msg_Printer11_Text = "Parameters"
  149. const L_Text_Msg_Printer12_Text = "Attributes"
  150. const L_Text_Msg_Printer13_Text = "Priority"
  151. const L_Text_Msg_Printer14_Text = "Default priority"
  152. const L_Text_Msg_Printer15_Text = "Start time"
  153. const L_Text_Msg_Printer16_Text = "Until time"
  154. const L_Text_Msg_Printer17_Text = "Job count"
  155. const L_Text_Msg_Printer18_Text = "Average pages per minute"
  156. const L_Text_Msg_Printer19_Text = "Printer status"
  157. const L_Text_Msg_Printer20_Text = "Extended printer status"
  158. const L_Text_Msg_Printer21_Text = "Detected error state"
  159. const L_Text_Msg_Printer22_Text = "Extended detected error state"
  160. '
  161. ' Printer status
  162. '
  163. const L_Text_Msg_Status01_Text = "Other"
  164. const L_Text_Msg_Status02_Text = "Unknown"
  165. const L_Text_Msg_Status03_Text = "Idle"
  166. const L_Text_Msg_Status04_Text = "Printing"
  167. const L_Text_Msg_Status05_Text = "Warmup"
  168. const L_Text_Msg_Status06_Text = "Stopped printing"
  169. const L_Text_Msg_Status07_Text = "Offline"
  170. const L_Text_Msg_Status08_Text = "Paused"
  171. const L_Text_Msg_Status09_Text = "Error"
  172. const L_Text_Msg_Status10_Text = "Busy"
  173. const L_Text_Msg_Status11_Text = "Not available"
  174. const L_Text_Msg_Status12_Text = "Waiting"
  175. const L_Text_Msg_Status13_Text = "Processing"
  176. const L_Text_Msg_Status14_Text = "Initializing"
  177. const L_Text_Msg_Status15_Text = "Power save"
  178. const L_Text_Msg_Status16_Text = "Pending deletion"
  179. const L_Text_Msg_Status17_Text = "I/O active"
  180. const L_Text_Msg_Status18_Text = "Manual feed"
  181. const L_Text_Msg_Status19_Text = "No error"
  182. const L_Text_Msg_Status20_Text = "Low paper"
  183. const L_Text_Msg_Status21_Text = "No paper"
  184. const L_Text_Msg_Status22_Text = "Low toner"
  185. const L_Text_Msg_Status23_Text = "No toner"
  186. const L_Text_Msg_Status24_Text = "Door open"
  187. const L_Text_Msg_Status25_Text = "Jammed"
  188. const L_Text_Msg_Status26_Text = "Service requested"
  189. const L_Text_Msg_Status27_Text = "Output bin full"
  190. const L_Text_Msg_Status28_Text = "Paper problem"
  191. const L_Text_Msg_Status29_Text = "Cannot print page"
  192. const L_Text_Msg_Status30_Text = "User intervention required"
  193. const L_Text_Msg_Status31_Text = "Out of memory"
  194. const L_Text_Msg_Status32_Text = "Server unknown"
  195. '
  196. ' Debug messages
  197. '
  198. const L_Text_Dbg_Msg01_Text = "In function AddPrinter"
  199. const L_Text_Dbg_Msg02_Text = "In function AddPrinterConnection"
  200. const L_Text_Dbg_Msg03_Text = "In function DelPrinter"
  201. const L_Text_Dbg_Msg04_Text = "In function DelAllPrinters"
  202. const L_Text_Dbg_Msg05_Text = "In function ListPrinters"
  203. const L_Text_Dbg_Msg06_Text = "In function GetDefaultPrinter"
  204. const L_Text_Dbg_Msg07_Text = "In function SetDefaultPrinter"
  205. const L_Text_Dbg_Msg08_Text = "In function ParseCommandLine"
  206. main
  207. '
  208. ' Main execution starts here
  209. '
  210. sub main
  211. dim iAction
  212. dim iRetval
  213. dim strServer
  214. dim strPrinter
  215. dim strDriver
  216. dim strPort
  217. dim strUser
  218. dim strPassword
  219. '
  220. ' Abort if the host is not cscript
  221. '
  222. if not IsHostCscript() then
  223. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  224. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  225. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  226. wscript.quit
  227. end if
  228. '
  229. ' Get command line parameters
  230. '
  231. iRetval = ParseCommandLine(iAction, strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  232. if iRetval = kErrorSuccess then
  233. select case iAction
  234. case kActionAdd
  235. iRetval = AddPrinter(strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  236. case kActionAddConn
  237. iRetval = AddPrinterConnection(strPrinter, strUser, strPassword)
  238. case kActionDel
  239. iRetval = DelPrinter(strServer, strPrinter, strUser, strPassword)
  240. case kActionDelAll
  241. iRetval = DelAllPrinters(kActionDelAll, strServer, strUser, strPassword)
  242. case kActionDelAllCon
  243. iRetval = DelAllPrinters(kActionDelAllCon, strServer, strUser, strPassword)
  244. case kActionDelAllLocal
  245. iRetval = DelAllPrinters(kActionDelAllLocal, strServer, strUser, strPassword)
  246. case kActionList
  247. iRetval = ListPrinters(strServer, strUser, strPassword)
  248. case kActionGetDefaultPrinter
  249. iRetval = GetDefaultPrinter(strUser, strPassword)
  250. case kActionSetDefaultPrinter
  251. iRetval = SetDefaultPrinter(strPrinter, strUser, strPassword)
  252. case kActionUnknown
  253. Usage(true)
  254. exit sub
  255. case else
  256. Usage(true)
  257. exit sub
  258. end select
  259. end if
  260. end sub
  261. '
  262. ' Add a printer with minimum settings. Use prncnfg.vbs to
  263. ' set the complete configuration of a printer
  264. '
  265. function AddPrinter(strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  266. on error resume next
  267. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  268. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  269. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & strPrinter
  270. DebugPrint kDebugTrace, L_Text_Msg_Printer04_Text & L_Space_Text & strDriver
  271. DebugPrint kDebugTrace, L_Text_Msg_Printer05_Text & L_Space_Text & strPort
  272. dim oPrinter
  273. dim oService
  274. dim iRetval
  275. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  276. set oPrinter = oService.Get("Win32_Printer").SpawnInstance_
  277. else
  278. AddPrinter = kErrorFailure
  279. exit function
  280. end if
  281. oPrinter.DriverName = strDriver
  282. oPrinter.PortName = strPort
  283. oPrinter.DeviceID = strPrinter
  284. oPrinter.Put_(kFlagCreateOnly)
  285. if Err.Number = kErrorSuccess then
  286. wscript.echo L_Text_Msg_General01_Text & L_Space_Text & strPrinter
  287. iRetval = kErrorSuccess
  288. else
  289. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & strPrinter & L_Space_Text & L_Error_Text _
  290. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  291. '
  292. ' Try getting extended error information
  293. '
  294. call LastError()
  295. iRetval = kErrorFailure
  296. end if
  297. AddPrinter = iRetval
  298. end function
  299. '
  300. ' Add a printer connection
  301. '
  302. function AddPrinterConnection(strPrinter, strUser, strPassword)
  303. on error resume next
  304. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text
  305. dim oPrinter
  306. dim oService
  307. dim iRetval
  308. dim uResult
  309. '
  310. ' Initialize return value
  311. '
  312. iRetval = kErrorFailure
  313. '
  314. ' We connect to the local server
  315. '
  316. if WmiConnect("", kNameSpace, strUser, strPassword, oService) then
  317. set oPrinter = oService.Get("Win32_Printer")
  318. else
  319. AddPrinterConnection = kErrorFailure
  320. exit function
  321. end if
  322. '
  323. ' Check if Get was successful
  324. '
  325. if Err.Number = kErrorSuccess then
  326. '
  327. ' The Err object indicates whether the WMI provider reached the execution
  328. ' of the function that adds a printer connection. The uResult is the Win32
  329. ' error code returned by the static method that adds a printer connection
  330. '
  331. uResult = oPrinter.AddPrinterConnection(strPrinter)
  332. if Err.Number = kErrorSuccess then
  333. if uResult = kErrorSuccess then
  334. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & strPrinter
  335. iRetval = kErrorSuccess
  336. else
  337. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & L_Text_Error_General03_Text _
  338. & L_Space_text & uResult
  339. end if
  340. else
  341. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  342. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  343. & Err.Description
  344. end if
  345. else
  346. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  347. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  348. & Err.Description
  349. end if
  350. AddPrinterConnection = iRetval
  351. end function
  352. '
  353. ' Delete a printer or a printer connection
  354. '
  355. function DelPrinter(strServer, strPrinter, strUser, strPassword)
  356. on error resume next
  357. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
  358. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  359. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & strPrinter
  360. dim oService
  361. dim oPrinter
  362. dim iRetval
  363. iRetval = kErrorFailure
  364. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  365. set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")
  366. else
  367. DelPrinter = kErrorFailure
  368. exit function
  369. end if
  370. '
  371. ' Check if Get was successful
  372. '
  373. if Err.Number = kErrorSuccess then
  374. oPrinter.Delete_
  375. if Err.Number = kErrorSuccess then
  376. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & strPrinter
  377. iRetval = kErrorSuccess
  378. else
  379. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & strPrinter & L_Space_Text _
  380. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  381. & L_Space_Text & Err.Description
  382. '
  383. ' Try getting extended error information
  384. '
  385. call LastError()
  386. end if
  387. else
  388. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & strPrinter & L_Space_Text _
  389. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  390. & L_Space_Text & Err.Description
  391. '
  392. ' Try getting extended error information
  393. '
  394. call LastError()
  395. end if
  396. DelPrinter = iRetval
  397. end function
  398. '
  399. ' Delete all local printers and connections on a machine
  400. '
  401. function DelAllPrinters(kAction, strServer, strUser, strPassword)
  402. on error resume next
  403. DebugPrint kDebugTrace, L_Text_Dbg_Msg04_Text
  404. dim Printers
  405. dim oPrinter
  406. dim oService
  407. dim iResult
  408. dim iTotal
  409. dim iTotalDeleted
  410. dim strPrinterName
  411. dim bDelete
  412. dim bConnection
  413. dim strTemp
  414. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  415. set Printers = oService.InstancesOf("Win32_Printer")
  416. else
  417. DelAllPrinters = kErrorFailure
  418. exit function
  419. end if
  420. if Err.Number <> kErrorSuccess then
  421. wscript.echo L_Text_Msg_General11_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  422. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  423. DelAllPrinters = kErrorFailure
  424. exit function
  425. end if
  426. iTotal = 0
  427. iTotalDeleted = 0
  428. for each oPrinter in Printers
  429. strPrinterName = oPrinter.DeviceID
  430. bConnection = oPrinter.Network
  431. if kAction = kActionDelAll then
  432. bDelete = 1
  433. iTotal = iTotal + 1
  434. elseif kAction = kActionDelAllCon and bConnection then
  435. bDelete = 1
  436. iTotal = iTotal + 1
  437. elseif kAction = kActionDelAllLocal and not bConnection then
  438. bDelete = 1
  439. iTotal = iTotal + 1
  440. else
  441. bDelete = 0
  442. end if
  443. if bDelete = 1 then
  444. if bConnection then
  445. strTemp = L_Space_Text & L_Connection_Text & L_Space_Text
  446. else
  447. strTemp = L_Space_Text
  448. end if
  449. '
  450. ' Delete printer instance
  451. '
  452. oPrinter.Delete_
  453. if Err.Number = kErrorSuccess then
  454. wscript.echo L_Text_Msg_General05_Text & strTemp & oPrinter.DeviceID
  455. iTotalDeleted = iTotalDeleted + 1
  456. else
  457. wscript.echo L_Text_Msg_General06_Text & strTemp & strPrinterName _
  458. & L_Space_Text & L_Error_Text & L_Space_Text & L_Hex_Text _
  459. & hex(Err.Number) & L_Space_Text & Err.Description
  460. '
  461. ' Try getting extended error information
  462. '
  463. call LastError()
  464. '
  465. ' Continue deleting the rest of the printers despite this error
  466. '
  467. Err.Clear
  468. end if
  469. end if
  470. next
  471. wscript.echo L_Empty_Text
  472. if kAction = kActionDelAll then
  473. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & iTotal
  474. wscript.echo L_Text_Msg_General10_Text & L_Space_Text & iTotalDeleted
  475. elseif kAction = kActionDelAllCon then
  476. wscript.echo L_Text_Msg_General16_Text & L_Space_Text & iTotal
  477. wscript.echo L_Text_Msg_General17_Text & L_Space_Text & iTotalDeleted
  478. elseif kAction = kActionDelAllLocal then
  479. wscript.echo L_Text_Msg_General18_Text & L_Space_Text & iTotal
  480. wscript.echo L_Text_Msg_General19_Text & L_Space_Text & iTotalDeleted
  481. else
  482. end if
  483. DelAllPrinters = kErrorSuccess
  484. end function
  485. '
  486. ' List the printers
  487. '
  488. function ListPrinters(strServer, strUser, strPassword)
  489. on error resume next
  490. DebugPrint kDebugTrace, L_Text_Dbg_Msg05_Text
  491. dim Printers
  492. dim oService
  493. dim oPrinter
  494. dim iTotal
  495. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  496. set Printers = oService.InstancesOf("Win32_Printer")
  497. else
  498. ListPrinters = kErrorFailure
  499. exit function
  500. end if
  501. if Err.Number <> kErrorSuccess then
  502. wscript.echo L_Text_Msg_General11_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  503. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  504. ListPrinters = kErrorFailure
  505. exit function
  506. end if
  507. iTotal = 0
  508. for each oPrinter in Printers
  509. iTotal = iTotal + 1
  510. wscript.echo L_Empty_Text
  511. wscript.echo L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  512. wscript.echo L_Text_Msg_Printer02_Text & L_Space_Text & oPrinter.DeviceID
  513. wscript.echo L_Text_Msg_Printer03_Text & L_Space_Text & oPrinter.ShareName
  514. wscript.echo L_Text_Msg_Printer04_Text & L_Space_Text & oPrinter.DriverName
  515. wscript.echo L_Text_Msg_Printer05_Text & L_Space_Text & oPrinter.PortName
  516. wscript.echo L_Text_Msg_Printer06_Text & L_Space_Text & oPrinter.Comment
  517. wscript.echo L_Text_Msg_Printer07_Text & L_Space_Text & oPrinter.Location
  518. wscript.echo L_Text_Msg_Printer08_Text & L_Space_Text & oPrinter.SepFile
  519. wscript.echo L_Text_Msg_Printer09_Text & L_Space_Text & oPrinter.PrintProcessor
  520. wscript.echo L_Text_Msg_Printer10_Text & L_Space_Text & oPrinter.PrintJobDataType
  521. wscript.echo L_Text_Msg_Printer11_Text & L_Space_Text & oPrinter.Parameters
  522. wscript.echo L_Text_Msg_Printer12_Text & L_Space_Text & CSTR(oPrinter.Attributes)
  523. wscript.echo L_Text_Msg_Printer13_Text & L_Space_Text & CSTR(oPrinter.Priority)
  524. wscript.echo L_Text_Msg_Printer14_Text & L_Space_Text & CStr(oPrinter.DefaultPriority)
  525. if CStr(oPrinter.StartTime) <> "" and CStr(oPrinter.UntilTime) <> "" then
  526. wscript.echo L_Text_Msg_Printer15_Text & L_Space_Text & Mid(Mid(CStr(oPrinter.StartTime), 9, 4), 1, 2) & "h" & Mid(Mid(CStr(oPrinter.StartTime), 9, 4), 3, 2)
  527. wscript.echo L_Text_Msg_Printer16_Text & L_Space_Text & Mid(Mid(CStr(oPrinter.UntilTime), 9, 4), 1, 2) & "h" & Mid(Mid(CStr(oPrinter.UntilTime), 9, 4), 3, 2)
  528. end if
  529. wscript.echo L_Text_Msg_Printer17_Text & L_Space_Text & CStr(oPrinter.Jobs)
  530. wscript.echo L_Text_Msg_Printer18_Text & L_Space_Text & CStr(oPrinter.AveragePagesPerMinute)
  531. wscript.echo L_Text_Msg_Printer19_Text & L_Space_Text & PrnStatusToString(oPrinter.PrinterStatus)
  532. wscript.echo L_Text_Msg_Printer20_Text & L_Space_Text & ExtPrnStatusToString(oPrinter.ExtendedPrinterStatus)
  533. wscript.echo L_Text_Msg_Printer21_Text & L_Space_Text & DetectedErrorStateToString(oPrinter.DetectedErrorState)
  534. wscript.echo L_Text_Msg_Printer22_Text & L_Space_Text & ExtDetectedErrorStateToString(oPrinter.ExtendedDetectedErrorState)
  535. Err.Clear
  536. next
  537. wscript.echo L_Empty_Text
  538. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & iTotal
  539. ListPrinters = kErrorSuccess
  540. end function
  541. '
  542. ' Get the default printer
  543. '
  544. function GetDefaultPrinter(strUser, strPassword)
  545. on error resume next
  546. DebugPrint kDebugTrace, L_Text_Dbg_Msg06_Text
  547. dim oService
  548. dim oPrinter
  549. dim iRetval
  550. dim oEnum
  551. iRetval = kErrorFailure
  552. '
  553. ' We connect to the local server
  554. '
  555. if WmiConnect("", kNameSpace, strUser, strPassword, oService) then
  556. set oEnum = oService.ExecQuery("select DeviceID from Win32_Printer where default=true")
  557. else
  558. SetDefaultPrinter = kErrorFailure
  559. exit function
  560. end if
  561. if Err.Number = kErrorSuccess then
  562. for each oPrinter in oEnum
  563. wscript.echo L_Text_Msg_General12_Text & L_Space_Text & oPrinter.DeviceID
  564. next
  565. iRetval = kErrorSuccess
  566. else
  567. wscript.echo L_Text_Msg_General13_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  568. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  569. end if
  570. GetDefaultPrinter = iRetval
  571. end function
  572. '
  573. ' Set the default printer
  574. '
  575. function SetDefaultPrinter(strPrinter, strUser, strPassword)
  576. 'on error resume next
  577. DebugPrint kDebugTrace, L_Text_Dbg_Msg07_Text
  578. dim oService
  579. dim oPrinter
  580. dim iRetval
  581. dim uResult
  582. iRetval = kErrorFailure
  583. '
  584. ' We connect to the local server
  585. '
  586. if WmiConnect("", kNameSpace, strUser, strPassword, oService) then
  587. set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")
  588. else
  589. SetDefaultPrinter = kErrorFailure
  590. exit function
  591. end if
  592. '
  593. ' Check if Get was successful
  594. '
  595. if Err.Number = kErrorSuccess then
  596. '
  597. ' The Err object indicates whether the WMI provider reached the execution
  598. ' of the function that sets the default printer. The uResult is the Win32
  599. ' error code of the spooler function that sets the default printer
  600. '
  601. uResult = oPrinter.SetDefaultPrinter
  602. if Err.Number = kErrorSuccess then
  603. if uResult = kErrorSuccess then
  604. wscript.echo L_Text_Msg_General15_Text & L_Space_Text & strPrinter
  605. iRetval = kErrorSuccess
  606. else
  607. wscript.echo L_Text_Msg_General14_Text & L_Space_Text _
  608. & L_Text_Error_General03_Text& L_Space_Text & uResult
  609. end if
  610. else
  611. wscript.echo L_Text_Msg_General14_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  612. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  613. end if
  614. else
  615. wscript.echo L_Text_Msg_General14_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  616. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  617. '
  618. ' Try getting extended error information
  619. '
  620. call LastError()
  621. end if
  622. SetDefaultPrinter = iRetval
  623. end function
  624. '
  625. ' Converts the printer status to a string
  626. '
  627. function PrnStatusToString(Status)
  628. dim str
  629. str = L_Empty_Text
  630. select case Status
  631. case 1
  632. str = str + L_Text_Msg_Status01_Text + L_Space_Text
  633. case 2
  634. str = str + L_Text_Msg_Status02_Text + L_Space_Text
  635. case 3
  636. str = str + L_Text_Msg_Status03_Text + L_Space_Text
  637. case 4
  638. str = str + L_Text_Msg_Status04_Text + L_Space_Text
  639. case 5
  640. str = str + L_Text_Msg_Status05_Text + L_Space_Text
  641. case 6
  642. str = str + L_Text_Msg_Status06_Text + L_Space_Text
  643. case 7
  644. str = str + L_Text_Msg_Status07_Text + L_Space_Text
  645. end select
  646. PrnStatusToString = str
  647. end function
  648. '
  649. ' Converts the extended printer status to a string
  650. '
  651. function ExtPrnStatusToString(Status)
  652. dim str
  653. str = L_Empty_Text
  654. select case Status
  655. case 1
  656. str = str + L_Text_Msg_Status01_Text + L_Space_Text
  657. case 2
  658. str = str + L_Text_Msg_Status02_Text + L_Space_Text
  659. case 3
  660. str = str + L_Text_Msg_Status03_Text + L_Space_Text
  661. case 4
  662. str = str + L_Text_Msg_Status04_Text + L_Space_Text
  663. case 5
  664. str = str + L_Text_Msg_Status05_Text + L_Space_Text
  665. case 6
  666. str = str + L_Text_Msg_Status06_Text + L_Space_Text
  667. case 7
  668. str = str + L_Text_Msg_Status07_Text + L_Space_Text
  669. case 8
  670. str = str + L_Text_Msg_Status08_Text + L_Space_Text
  671. case 9
  672. str = str + L_Text_Msg_Status09_Text + L_Space_Text
  673. case 10
  674. str = str + L_Text_Msg_Status10_Text + L_Space_Text
  675. case 11
  676. str = str + L_Text_Msg_Status11_Text + L_Space_Text
  677. case 12
  678. str = str + L_Text_Msg_Status12_Text + L_Space_Text
  679. case 13
  680. str = str + L_Text_Msg_Status13_Text + L_Space_Text
  681. case 14
  682. str = str + L_Text_Msg_Status14_Text + L_Space_Text
  683. case 15
  684. str = str + L_Text_Msg_Status15_Text + L_Space_Text
  685. case 16
  686. str = str + L_Text_Msg_Status16_Text + L_Space_Text
  687. case 17
  688. str = str + L_Text_Msg_Status17_Text + L_Space_Text
  689. case 18
  690. str = str + L_Text_Msg_Status18_Text + L_Space_Text
  691. end select
  692. ExtPrnStatusToString = str
  693. end function
  694. '
  695. ' Converts the detected error state to a string
  696. '
  697. function DetectedErrorStateToString(Status)
  698. dim str
  699. str = L_Empty_Text
  700. select case Status
  701. case 0
  702. str = str + L_Text_Msg_Status02_Text + L_Space_Text
  703. case 1
  704. str = str + L_Text_Msg_Status01_Text + L_Space_Text
  705. case 2
  706. str = str + L_Text_Msg_Status01_Text + L_Space_Text
  707. case 3
  708. str = str + L_Text_Msg_Status20_Text + L_Space_Text
  709. case 4
  710. str = str + L_Text_Msg_Status21_Text + L_Space_Text
  711. case 5
  712. str = str + L_Text_Msg_Status22_Text + L_Space_Text
  713. case 6
  714. str = str + L_Text_Msg_Status23_Text + L_Space_Text
  715. case 7
  716. str = str + L_Text_Msg_Status24_Text + L_Space_Text
  717. case 8
  718. str = str + L_Text_Msg_Status25_Text + L_Space_Text
  719. case 9
  720. str = str + L_Text_Msg_Status07_Text + L_Space_Text
  721. case 10
  722. str = str + L_Text_Msg_Status26_Text + L_Space_Text
  723. case 11
  724. str = str + L_Text_Msg_Status27_Text + L_Space_Text
  725. end select
  726. DetectedErrorStateToString = str
  727. end function
  728. '
  729. ' Converts the extended detected error state to a string
  730. '
  731. function ExtDetectedErrorStateToString(Status)
  732. dim str
  733. str = L_Empty_Text
  734. select case Status
  735. case 0
  736. str = str + L_Text_Msg_Status02_Text + L_Space_Text
  737. case 1
  738. str = str + L_Text_Msg_Status01_Text + L_Space_Text
  739. case 2
  740. str = str + L_Text_Msg_Status01_Text + L_Space_Text
  741. case 3
  742. str = str + L_Text_Msg_Status20_Text + L_Space_Text
  743. case 4
  744. str = str + L_Text_Msg_Status21_Text + L_Space_Text
  745. case 5
  746. str = str + L_Text_Msg_Status22_Text + L_Space_Text
  747. case 6
  748. str = str + L_Text_Msg_Status23_Text + L_Space_Text
  749. case 7
  750. str = str + L_Text_Msg_Status24_Text + L_Space_Text
  751. case 8
  752. str = str + L_Text_Msg_Status25_Text + L_Space_Text
  753. case 9
  754. str = str + L_Text_Msg_Status07_Text + L_Space_Text
  755. case 10
  756. str = str + L_Text_Msg_Status26_Text + L_Space_Text
  757. case 11
  758. str = str + L_Text_Msg_Status27_Text + L_Space_Text
  759. case 12
  760. str = str + L_Text_Msg_Status28_Text + L_Space_Text
  761. case 13
  762. str = str + L_Text_Msg_Status29_Text + L_Space_Text
  763. case 14
  764. str = str + L_Text_Msg_Status30_Text + L_Space_Text
  765. case 15
  766. str = str + L_Text_Msg_Status31_Text + L_Space_Text
  767. case 16
  768. str = str + L_Text_Msg_Status32_Text + L_Space_Text
  769. end select
  770. ExtDetectedErrorStateToString = str
  771. end function
  772. '
  773. ' Debug display helper function
  774. '
  775. sub DebugPrint(uFlags, strString)
  776. if gDebugFlag = true then
  777. if uFlags = kDebugTrace then
  778. wscript.echo L_Debug_Text & L_Space_Text & strString
  779. end if
  780. if uFlags = kDebugError then
  781. if Err <> 0 then
  782. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  783. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  784. & L_Space_Text & Err.Description
  785. end if
  786. end if
  787. end if
  788. end sub
  789. '
  790. ' Parse the command line into its components
  791. '
  792. function ParseCommandLine(iAction, strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  793. on error resume next
  794. DebugPrint kDebugTrace, L_Text_Dbg_Msg08_Text
  795. dim oArgs
  796. dim iIndex
  797. iAction = kActionUnknown
  798. iIndex = 0
  799. set oArgs = wscript.Arguments
  800. while iIndex < oArgs.Count
  801. select case oArgs(iIndex)
  802. case "-a"
  803. iAction = kActionAdd
  804. case "-ac"
  805. iAction = kActionAddConn
  806. case "-d"
  807. iAction = kActionDel
  808. case "-x"
  809. iAction = kActionDelAll
  810. case "-xc"
  811. iAction = kActionDelAllCon
  812. case "-xo"
  813. iAction = kActionDelAllLocal
  814. case "-l"
  815. iAction = kActionList
  816. case "-g"
  817. iAction = kActionGetDefaultPrinter
  818. case "-t"
  819. iAction = kActionSetDefaultPrinter
  820. case "-s"
  821. iIndex = iIndex + 1
  822. strServer = RemoveBackslashes(oArgs(iIndex))
  823. case "-p"
  824. iIndex = iIndex + 1
  825. strPrinter = oArgs(iIndex)
  826. case "-m"
  827. iIndex = iIndex + 1
  828. strDriver = oArgs(iIndex)
  829. case "-u"
  830. iIndex = iIndex + 1
  831. strUser = oArgs(iIndex)
  832. case "-w"
  833. iIndex = iIndex + 1
  834. strPassword = oArgs(iIndex)
  835. case "-r"
  836. iIndex = iIndex + 1
  837. strPort = oArgs(iIndex)
  838. case "-?"
  839. Usage(true)
  840. exit function
  841. case else
  842. Usage(true)
  843. exit function
  844. end select
  845. iIndex = iIndex + 1
  846. wend
  847. if Err = kErrorSuccess then
  848. ParseCommandLine = kErrorSuccess
  849. else
  850. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  851. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  852. ParseCommandLine = kErrorFailure
  853. end if
  854. end function
  855. '
  856. ' Display command usage.
  857. '
  858. sub Usage(bExit)
  859. wscript.echo L_Help_Help_General01_Text
  860. wscript.echo L_Help_Help_General02_Text
  861. wscript.echo L_Help_Help_General03_Text
  862. wscript.echo L_Help_Help_General04_Text
  863. wscript.echo L_Help_Help_General05_Text
  864. wscript.echo L_Help_Help_General06_Text
  865. wscript.echo L_Help_Help_General07_Text
  866. wscript.echo L_Help_Help_General08_Text
  867. wscript.echo L_Help_Help_General09_Text
  868. wscript.echo L_Help_Help_General10_Text
  869. wscript.echo L_Help_Help_General11_Text
  870. wscript.echo L_Help_Help_General12_Text
  871. wscript.echo L_Help_Help_General13_Text
  872. wscript.echo L_Help_Help_General14_Text
  873. wscript.echo L_Help_Help_General15_Text
  874. wscript.echo L_Help_Help_General16_Text
  875. wscript.echo L_Help_Help_General17_Text
  876. wscript.echo L_Help_Help_General18_Text
  877. wscript.echo L_Help_Help_General19_Text
  878. wscript.echo L_Empty_Text
  879. wscript.echo L_Help_Help_General20_Text
  880. wscript.echo L_Help_Help_General21_Text
  881. wscript.echo L_Help_Help_General22_Text
  882. wscript.echo L_Help_Help_General23_Text
  883. wscript.echo L_Help_Help_General24_Text
  884. wscript.echo L_Help_Help_General25_Text
  885. wscript.echo L_Help_Help_General26_Text
  886. wscript.echo L_Help_Help_General27_Text
  887. wscript.echo L_Help_Help_General28_Text
  888. wscript.echo L_Help_Help_General29_Text
  889. if bExit then
  890. wscript.quit(1)
  891. end if
  892. end sub
  893. '
  894. ' Determines which program is being used to run this script.
  895. ' Returns true if the script host is cscript.exe
  896. '
  897. function IsHostCscript()
  898. on error resume next
  899. dim strFullName
  900. dim strCommand
  901. dim i, j
  902. dim bReturn
  903. bReturn = false
  904. strFullName = WScript.FullName
  905. i = InStr(1, strFullName, ".exe", 1)
  906. if i <> 0 then
  907. j = InStrRev(strFullName, "\", i, 1)
  908. if j <> 0 then
  909. strCommand = Mid(strFullName, j+1, i-j-1)
  910. if LCase(strCommand) = "cscript" then
  911. bReturn = true
  912. end if
  913. end if
  914. end if
  915. if Err <> 0 then
  916. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  917. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  918. end if
  919. IsHostCscript = bReturn
  920. end function
  921. '
  922. ' Retrieves extended information about the last error that occurred
  923. ' during a WBEM operation. The methods that set an SWbemLastError
  924. ' object are GetObject, PutInstance, DeleteInstance
  925. '
  926. sub LastError()
  927. on error resume next
  928. dim oError
  929. set oError = CreateObject("WbemScripting.SWbemLastError")
  930. if Err = kErrorSuccess then
  931. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  932. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  933. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  934. wscript.echo L_Text_Error_General03_Text & L_Space_Text & oError.StatusCode
  935. end if
  936. end sub
  937. '
  938. ' Connects to the WMI service on a server. oService is returned as a service
  939. ' object (SWbemServices)
  940. '
  941. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  942. on error resume next
  943. dim oLocator
  944. dim bResult
  945. oService = null
  946. bResult = false
  947. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  948. if Err = kErrorSuccess then
  949. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  950. if Err = kErrorSuccess then
  951. bResult = true
  952. oService.Security_.impersonationlevel = 3
  953. '
  954. ' Required to perform administrative tasks on the spooler service
  955. '
  956. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  957. Err.Clear
  958. else
  959. wscript.echo L_Text_Msg_General11_Text & L_Space_Text & L_Error_Text _
  960. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  961. & Err.Description
  962. end if
  963. else
  964. wscript.echo L_Text_Msg_General10_Text & L_Space_Text & L_Error_Text _
  965. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  966. & Err.Description
  967. end if
  968. WmiConnect = bResult
  969. end function
  970. '
  971. ' Remove leading "\\" from server name
  972. '
  973. function RemoveBackslashes(strServer)
  974. dim strRet
  975. strRet = strServer
  976. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  977. strRet = Mid(strServer, 3)
  978. end if
  979. RemoveBackslashes = strRet
  980. end function