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.

1074 lines
31 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?][c] [-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 kActionList = 5
  43. const kActionGetDefaultPrinter = 6
  44. const kActionSetDefaultPrinter = 7
  45. const kErrorSuccess = 0
  46. const KErrorFailure = 1
  47. const kFlagCreateOnly = 2
  48. const kNameSpace = "root\cimv2"
  49. '
  50. ' Generic strings
  51. '
  52. const L_Empty_Text = ""
  53. const L_Space_Text = " "
  54. const L_Error_Text = "Error"
  55. const L_Success_Text = "Success"
  56. const L_Failed_Text = "Failed"
  57. const L_Hex_Text = "0x"
  58. const L_Printer_Text = "Printer"
  59. const L_Operation_Text = "Operation"
  60. const L_Provider_Text = "Provider"
  61. const L_Description_Text = "Description"
  62. const L_Debug_Text = "Debug:"
  63. '
  64. ' General usage messages
  65. '
  66. const L_Help_Help_General01_Text = "Usage: prnmngr [-adxgtl?][c] [-s server][-p printer][-m driver model]"
  67. const L_Help_Help_General02_Text = " [-r port][-u user name][-w password]"
  68. const L_Help_Help_General03_Text = "Arguments:"
  69. const L_Help_Help_General04_Text = "-a - add local printer"
  70. const L_Help_Help_General05_Text = "-ac - add printer connection"
  71. const L_Help_Help_General06_Text = "-d - delete printer"
  72. const L_Help_Help_General07_Text = "-g - get the default printer"
  73. const L_Help_Help_General08_Text = "-l - list printers"
  74. const L_Help_Help_General09_Text = "-m - driver model"
  75. const L_Help_Help_General10_Text = "-p - printer name"
  76. const L_Help_Help_General11_Text = "-r - port name"
  77. const L_Help_Help_General12_Text = "-s - server name"
  78. const L_Help_Help_General13_Text = "-t - set the default printer"
  79. const L_Help_Help_General14_Text = "-u - user name"
  80. const L_Help_Help_General15_Text = "-w - password"
  81. const L_Help_Help_General16_Text = "-x - delete all printers"
  82. const L_Help_Help_General17_Text = "-? - display command usage"
  83. const L_Help_Help_General18_Text = "Examples:"
  84. const L_Help_Help_General19_Text = "prnmngr -a -p ""printer"" -m ""driver"" -r ""lpt1:"""
  85. const L_Help_Help_General20_Text = "prnmngr -d -p ""printer"" -s server"
  86. const L_Help_Help_General21_Text = "prnmngr -ac -p ""\\server\printer"""
  87. const L_Help_Help_General22_Text = "prnmngr -d -p ""\\server\printer"""
  88. const L_Help_Help_General23_Text = "prnmngr -x -s server"
  89. const L_Help_Help_General24_Text = "prnmngr -l -s server"
  90. const L_Help_Help_General25_Text = "prnmngr -g"
  91. const L_Help_Help_General26_Text = "prnmngr -t -p ""\\server\printer"""
  92. '
  93. ' Messages to be displayed if the scripting host is not cscript
  94. '
  95. const L_Help_Help_Host01_Text = "Please run this script using CScript."
  96. const L_Help_Help_Host02_Text = "This can be achieved by"
  97. const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
  98. const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
  99. const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
  100. const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
  101. '
  102. ' General error messages
  103. '
  104. const L_Text_Error_General01_Text = "The scripting host could not be determined."
  105. const L_Text_Error_General02_Text = "Unable to parse command line."
  106. const L_Text_Error_General03_Text = "Win32 error code"
  107. '
  108. ' Miscellaneous messages
  109. '
  110. const L_Text_Msg_General01_Text = "Added printer"
  111. const L_Text_Msg_General02_Text = "Unable to add printer"
  112. const L_Text_Msg_General03_Text = "Added printer connection"
  113. const L_Text_Msg_General04_Text = "Unable to add printer connection"
  114. const L_Text_Msg_General05_Text = "Deleted printer (connection)"
  115. const L_Text_Msg_General06_Text = "Unable to delete printer (connection)"
  116. const L_Text_Msg_General07_Text = "Attempting to delete printer"
  117. const L_Text_Msg_General08_Text = "Unable to delete printers"
  118. const L_Text_Msg_General09_Text = "Number of printers enumerated"
  119. const L_Text_Msg_General10_Text = "Number of printers deleted"
  120. const L_Text_Msg_General11_Text = "Unable to enumarate printers"
  121. const L_Text_Msg_General12_Text = "The default printer is"
  122. const L_Text_Msg_General13_Text = "Unable to get the default printer"
  123. const L_Text_Msg_General14_Text = "Unable to set the default printer"
  124. const L_Text_Msg_General15_Text = "The default printer is now"
  125. '
  126. ' Printer properties
  127. '
  128. const L_Text_Msg_Printer01_Text = "Server name"
  129. const L_Text_Msg_Printer02_Text = "Printer name"
  130. const L_Text_Msg_Printer03_Text = "Share name"
  131. const L_Text_Msg_Printer04_Text = "Driver name"
  132. const L_Text_Msg_Printer05_Text = "Port name"
  133. const L_Text_Msg_Printer06_Text = "Comment"
  134. const L_Text_Msg_Printer07_Text = "Location"
  135. const L_Text_Msg_Printer08_Text = "Separator file"
  136. const L_Text_Msg_Printer09_Text = "Print processor"
  137. const L_Text_Msg_Printer10_Text = "Data type"
  138. const L_Text_Msg_Printer11_Text = "Parameters"
  139. const L_Text_Msg_Printer12_Text = "Attributes"
  140. const L_Text_Msg_Printer13_Text = "Priority"
  141. const L_Text_Msg_Printer14_Text = "Default priority"
  142. const L_Text_Msg_Printer15_Text = "Start time"
  143. const L_Text_Msg_Printer16_Text = "Until time"
  144. const L_Text_Msg_Printer17_Text = "Status"
  145. const L_Text_Msg_Printer18_Text = "Job count"
  146. const L_Text_Msg_Printer19_Text = "Average pages per minute"
  147. '
  148. ' Debug messages
  149. '
  150. const L_Text_Dbg_Msg01_Text = "In function AddPrinter"
  151. const L_Text_Dbg_Msg02_Text = "In function AddPrinterConnection"
  152. const L_Text_Dbg_Msg03_Text = "In function DelPrinter"
  153. const L_Text_Dbg_Msg04_Text = "In function DelAllPrinters"
  154. const L_Text_Dbg_Msg05_Text = "In function ListPrinters"
  155. const L_Text_Dbg_Msg06_Text = "In function GetDefaultPrinter"
  156. const L_Text_Dbg_Msg07_Text = "In function SetDefaultPrinter"
  157. const L_Text_Dbg_Msg08_Text = "In function ParseCommandLine"
  158. main
  159. '
  160. ' Main execution starts here
  161. '
  162. sub main
  163. dim iAction
  164. dim iRetval
  165. dim strServer
  166. dim strPrinter
  167. dim strDriver
  168. dim strPort
  169. dim strUser
  170. dim strPassword
  171. '
  172. ' Abort if the host is not cscript
  173. '
  174. if not IsHostCscript() then
  175. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  176. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  177. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  178. wscript.quit
  179. end if
  180. '
  181. ' Get command line parameters
  182. '
  183. iRetval = ParseCommandLine(iAction, strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  184. if iRetval = kErrorSuccess then
  185. select case iAction
  186. case kActionAdd
  187. iRetval = AddPrinter(strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  188. case kActionAddConn
  189. iRetval = AddPrinterConnection(strPrinter, strUser, strPassword)
  190. case kActionDel
  191. iRetval = DelPrinter(strServer, strPrinter, strUser, strPassword)
  192. case kActionDelAll
  193. iRetval = DelAllPrinters(strServer, strUser, strPassword)
  194. case kActionList
  195. iRetval = ListPrinters(strServer, strUser, strPassword)
  196. case kActionGetDefaultPrinter
  197. iRetval = GetDefaultPrinter(strUser, strPassword)
  198. case kActionSetDefaultPrinter
  199. iRetval = SetDefaultPrinter(strPrinter, strUser, strPassword)
  200. case kActionUnknown
  201. Usage(true)
  202. exit sub
  203. case else
  204. Usage(true)
  205. exit sub
  206. end select
  207. end if
  208. end sub
  209. '
  210. ' Add a printer with minimum settings. Use prncnfg.vbs to
  211. ' set the complete configuration of a printer
  212. '
  213. function AddPrinter(strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  214. on error resume next
  215. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  216. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  217. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & strPrinter
  218. DebugPrint kDebugTrace, L_Text_Msg_Printer04_Text & L_Space_Text & strDriver
  219. DebugPrint kDebugTrace, L_Text_Msg_Printer05_Text & L_Space_Text & strPort
  220. dim oPrinter
  221. dim oService
  222. dim iRetval
  223. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  224. set oPrinter = oService.Get("Win32_Printer").SpawnInstance_
  225. else
  226. AddPrinter = kErrorFailure
  227. exit function
  228. end if
  229. oPrinter.DriverName = strDriver
  230. oPrinter.PortName = strPort
  231. oPrinter.DeviceID = strPrinter
  232. oPrinter.Put_(kFlagCreateOnly)
  233. if Err.Number = kErrorSuccess then
  234. wscript.echo L_Text_Msg_General01_Text & L_Space_Text & strPrinter
  235. iRetval = kErrorSuccess
  236. else
  237. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & strPrinter & L_Space_Text & L_Error_Text _
  238. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  239. '
  240. ' Try getting extended error information
  241. '
  242. call LastError()
  243. iRetval = kErrorFailure
  244. end if
  245. AddPrinter = iRetval
  246. end function
  247. '
  248. ' Add a printer connection
  249. '
  250. function AddPrinterConnection(strPrinter, strUser, strPassword)
  251. on error resume next
  252. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text
  253. dim oPrinter
  254. dim oService
  255. dim iRetval
  256. dim uResult
  257. '
  258. ' Initialize return value
  259. '
  260. iRetval = kErrorFailure
  261. '
  262. ' We connect to the local server
  263. '
  264. if WmiConnect("", kNameSpace, strUser, strPassword, oService) then
  265. set oPrinter = oService.Get("Win32_Printer")
  266. else
  267. AddPrinterConnection = kErrorFailure
  268. exit function
  269. end if
  270. '
  271. ' Check if Get was successful
  272. '
  273. if Err.Number = kErrorSuccess then
  274. '
  275. ' The Err object indicates whether the WMI provider reached the execution
  276. ' of the function that adds a printer connection. The uResult is the Win32
  277. ' error code returned by the static method that adds a printer connection
  278. '
  279. uResult = oPrinter.AddPrinterConnection(strPrinter)
  280. if Err.Number = kErrorSuccess then
  281. if uResult = kErrorSuccess then
  282. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & strPrinter
  283. iRetval = kErrorSuccess
  284. else
  285. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & L_Text_Error_General03_Text _
  286. & L_Space_text & uResult
  287. end if
  288. else
  289. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  290. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  291. & Err.Description
  292. end if
  293. else
  294. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  295. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  296. & Err.Description
  297. end if
  298. AddPrinterConnection = iRetval
  299. end function
  300. '
  301. ' Delete a printer or a printer connection
  302. '
  303. function DelPrinter(strServer, strPrinter, strUser, strPassword)
  304. on error resume next
  305. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
  306. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  307. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & strPrinter
  308. dim oService
  309. dim oPrinter
  310. dim iRetval
  311. iRetval = kErrorFailure
  312. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  313. set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")
  314. else
  315. DelPrinter = kErrorFailure
  316. exit function
  317. end if
  318. '
  319. ' Check if Get was successful
  320. '
  321. if Err.Number = kErrorSuccess then
  322. oPrinter.Delete_
  323. if Err.Number = kErrorSuccess then
  324. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & strPrinter
  325. iRetval = kErrorSuccess
  326. else
  327. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & strPrinter & L_Space_Text _
  328. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  329. & L_Space_Text & Err.Description
  330. '
  331. ' Try getting extended error information
  332. '
  333. call LastError()
  334. end if
  335. else
  336. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & strPrinter & L_Space_Text _
  337. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  338. & L_Space_Text & Err.Description
  339. '
  340. ' Try getting extended error information
  341. '
  342. call LastError()
  343. end if
  344. DelPrinter = iRetval
  345. end function
  346. '
  347. ' Delete all local printers and connections on a machine
  348. '
  349. function DelAllPrinters(strServer, strUser, strPassword)
  350. on error resume next
  351. DebugPrint kDebugTrace, L_Text_Dbg_Msg04_Text
  352. dim Printers
  353. dim oPrinter
  354. dim oService
  355. dim iResult
  356. dim iTotal
  357. dim iTotalDeleted
  358. dim strPrinterName
  359. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  360. set Printers = oService.InstancesOf("Win32_Printer")
  361. else
  362. DelAllPrinters = kErrorFailure
  363. exit function
  364. end if
  365. if Err.Number <> kErrorSuccess then
  366. wscript.echo L_Text_Msg_General11_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  367. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  368. DelAllPrinters = kErrorFailure
  369. exit function
  370. end if
  371. iTotal = 0
  372. iTotalDeleted = 0
  373. for each oPrinter in Printers
  374. iTotal = iTotal + 1
  375. wscript.echo L_Empty_Text
  376. wscript.echo L_Text_Msg_General07_Text & L_Space_Text & oPrinter.DeviceID
  377. strPrinterName = oPrinter.DeviceID
  378. '
  379. ' Delete printer instance
  380. '
  381. oPrinter.Delete_
  382. if Err.Number = kErrorSuccess then
  383. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & oPrinter.DeviceID
  384. iTotalDeleted = iTotalDeleted + 1
  385. else
  386. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & strPrinterName _
  387. & L_Space_Text & L_Error_Text & L_Space_Text & L_Hex_Text _
  388. & hex(Err.Number) & L_Space_Text & Err.Description
  389. '
  390. ' Try getting extended error information
  391. '
  392. call LastError()
  393. '
  394. ' Continue deleting the rest of the printers despite this error
  395. '
  396. Err.Clear
  397. end if
  398. next
  399. wscript.echo L_Empty_Text
  400. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & iTotal
  401. wscript.echo L_Text_Msg_General10_Text & L_Space_Text & iTotalDeleted
  402. DelAllPrinters = kErrorSuccess
  403. end function
  404. '
  405. ' List the printers
  406. '
  407. function ListPrinters(strServer, strUser, strPassword)
  408. on error resume next
  409. DebugPrint kDebugTrace, L_Text_Dbg_Msg05_Text
  410. dim Printers
  411. dim oService
  412. dim oPrinter
  413. dim iTotal
  414. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  415. set Printers = oService.InstancesOf("Win32_Printer")
  416. else
  417. ListPrinters = 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. ListPrinters = kErrorFailure
  424. exit function
  425. end if
  426. iTotal = 0
  427. for each oPrinter in Printers
  428. iTotal = iTotal + 1
  429. wscript.echo L_Empty_Text
  430. wscript.echo L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  431. wscript.echo L_Text_Msg_Printer02_Text & L_Space_Text & oPrinter.DeviceID
  432. wscript.echo L_Text_Msg_Printer03_Text & L_Space_Text & oPrinter.ShareName
  433. wscript.echo L_Text_Msg_Printer04_Text & L_Space_Text & oPrinter.DriverName
  434. wscript.echo L_Text_Msg_Printer05_Text & L_Space_Text & oPrinter.PortName
  435. wscript.echo L_Text_Msg_Printer06_Text & L_Space_Text & oPrinter.Comment
  436. wscript.echo L_Text_Msg_Printer07_Text & L_Space_Text & oPrinter.Location
  437. wscript.echo L_Text_Msg_Printer08_Text & L_Space_Text & oPrinter.SepFile
  438. wscript.echo L_Text_Msg_Printer09_Text & L_Space_Text & oPrinter.PrintProcessor
  439. wscript.echo L_Text_Msg_Printer10_Text & L_Space_Text & oPrinter.PrintJobDataType
  440. wscript.echo L_Text_Msg_Printer11_Text & L_Space_Text & oPrinter.Parameters
  441. wscript.echo L_Text_Msg_Printer12_Text & L_Space_Text & CSTR(oPrinter.Attributes)
  442. wscript.echo L_Text_Msg_Printer13_Text & L_Space_Text & CSTR(oPrinter.Priority)
  443. wscript.echo L_Text_Msg_Printer14_Text & L_Space_Text & CStr(oPrinter.DefaultPriority)
  444. if CStr(oPrinter.StartTime) <> "" and CStr(oPrinter.UntilTime) <> "" then
  445. 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)
  446. 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)
  447. end if
  448. wscript.echo L_Text_Msg_Printer17_Text & L_Space_Text & CStr(oPrinter.Status)
  449. wscript.echo L_Text_Msg_Printer18_Text & L_Space_Text & CStr(oPrinter.Jobs)
  450. wscript.echo L_Text_Msg_Printer19_Text & L_Space_Text & CStr(oPrinter.AveragePagesPerMinute)
  451. Err.Clear
  452. next
  453. wscript.echo L_Empty_Text
  454. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & iTotal
  455. ListPrinters = kErrorSuccess
  456. end function
  457. '
  458. ' Get the default printer
  459. '
  460. function GetDefaultPrinter(strUser, strPassword)
  461. on error resume next
  462. DebugPrint kDebugTrace, L_Text_Dbg_Msg06_Text
  463. dim oService
  464. dim oPrinter
  465. dim iRetval
  466. dim oEnum
  467. iRetval = kErrorFailure
  468. '
  469. ' We connect to the local server
  470. '
  471. if WmiConnect("", kNameSpace, strUser, strPassword, oService) then
  472. set oEnum = oService.ExecQuery("select DeviceID from Win32_Printer where default=true")
  473. else
  474. SetDefaultPrinter = kErrorFailure
  475. exit function
  476. end if
  477. if Err.Number = kErrorSuccess then
  478. for each oPrinter in oEnum
  479. wscript.echo L_Text_Msg_General12_Text & L_Space_Text & oPrinter.DeviceID
  480. next
  481. iRetval = kErrorSuccess
  482. else
  483. wscript.echo L_Text_Msg_General13_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  484. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  485. end if
  486. GetDefaultPrinter = iRetval
  487. end function
  488. '
  489. ' Set the default printer
  490. '
  491. function SetDefaultPrinter(strPrinter, strUser, strPassword)
  492. 'on error resume next
  493. DebugPrint kDebugTrace, L_Text_Dbg_Msg07_Text
  494. dim oService
  495. dim oPrinter
  496. dim iRetval
  497. dim uResult
  498. iRetval = kErrorFailure
  499. '
  500. ' We connect to the local server
  501. '
  502. if WmiConnect("", kNameSpace, strUser, strPassword, oService) then
  503. set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")
  504. else
  505. SetDefaultPrinter = kErrorFailure
  506. exit function
  507. end if
  508. '
  509. ' Check if Get was successful
  510. '
  511. if Err.Number = kErrorSuccess then
  512. '
  513. ' The Err object indicates whether the WMI provider reached the execution
  514. ' of the function that sets the default printer. The uResult is the Win32
  515. ' error code of the spooler function that sets the default printer
  516. '
  517. uResult = oPrinter.SetDefaultPrinter
  518. if Err.Number = kErrorSuccess then
  519. if uResult = kErrorSuccess then
  520. wscript.echo L_Text_Msg_General15_Text & L_Space_Text & strPrinter
  521. iRetval = kErrorSuccess
  522. else
  523. wscript.echo L_Text_Msg_General14_Text & L_Space_Text _
  524. & L_Text_Error_General03_Text& L_Space_Text & uResult
  525. end if
  526. else
  527. wscript.echo L_Text_Msg_General14_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  528. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  529. end if
  530. else
  531. wscript.echo L_Text_Msg_General14_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  532. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  533. '
  534. ' Try getting extended error information
  535. '
  536. call LastError()
  537. end if
  538. SetDefaultPrinter = iRetval
  539. end function
  540. '
  541. ' Debug display helper function
  542. '
  543. sub DebugPrint(uFlags, strString)
  544. if gDebugFlag = true then
  545. if uFlags = kDebugTrace then
  546. wscript.echo L_Debug_Text & L_Space_Text & strString
  547. end if
  548. if uFlags = kDebugError then
  549. if Err <> 0 then
  550. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  551. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  552. & L_Space_Text & Err.Description
  553. end if
  554. end if
  555. end if
  556. end sub
  557. '
  558. ' Parse the command line into its components
  559. '
  560. function ParseCommandLine(iAction, strServer, strPrinter, strDriver, strPort, strUser, strPassword)
  561. on error resume next
  562. DebugPrint kDebugTrace, L_Text_Dbg_Msg08_Text
  563. dim oArgs
  564. dim iIndex
  565. iAction = kActionUnknown
  566. iIndex = 0
  567. set oArgs = wscript.Arguments
  568. while iIndex < oArgs.Count
  569. select case oArgs(iIndex)
  570. case "-a"
  571. iAction = kActionAdd
  572. case "-ac"
  573. iAction = kActionAddConn
  574. case "-d"
  575. iAction = kActionDel
  576. case "-x"
  577. iAction = kActionDelAll
  578. case "-l"
  579. iAction = kActionList
  580. case "-g"
  581. iAction = kActionGetDefaultPrinter
  582. case "-t"
  583. iAction = kActionSetDefaultPrinter
  584. case "-s"
  585. iIndex = iIndex + 1
  586. strServer = RemoveBackslashes(oArgs(iIndex))
  587. case "-p"
  588. iIndex = iIndex + 1
  589. strPrinter = oArgs(iIndex)
  590. case "-m"
  591. iIndex = iIndex + 1
  592. strDriver = oArgs(iIndex)
  593. case "-u"
  594. iIndex = iIndex + 1
  595. strUser = oArgs(iIndex)
  596. case "-w"
  597. iIndex = iIndex + 1
  598. strPassword = oArgs(iIndex)
  599. case "-r"
  600. iIndex = iIndex + 1
  601. strPort = oArgs(iIndex)
  602. case "-?"
  603. Usage(true)
  604. exit function
  605. case else
  606. Usage(true)
  607. exit function
  608. end select
  609. iIndex = iIndex + 1
  610. wend
  611. if Err = kErrorSuccess then
  612. ParseCommandLine = kErrorSuccess
  613. else
  614. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  615. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  616. ParseCommandLine = kErrorFailure
  617. end if
  618. end function
  619. '
  620. ' Display command usage.
  621. '
  622. sub Usage(bExit)
  623. wscript.echo L_Help_Help_General01_Text
  624. wscript.echo L_Help_Help_General02_Text
  625. wscript.echo L_Help_Help_General03_Text
  626. wscript.echo L_Help_Help_General04_Text
  627. wscript.echo L_Help_Help_General05_Text
  628. wscript.echo L_Help_Help_General06_Text
  629. wscript.echo L_Help_Help_General07_Text
  630. wscript.echo L_Help_Help_General08_Text
  631. wscript.echo L_Help_Help_General09_Text
  632. wscript.echo L_Help_Help_General10_Text
  633. wscript.echo L_Help_Help_General11_Text
  634. wscript.echo L_Help_Help_General12_Text
  635. wscript.echo L_Help_Help_General13_Text
  636. wscript.echo L_Help_Help_General14_Text
  637. wscript.echo L_Help_Help_General15_Text
  638. wscript.echo L_Help_Help_General16_Text
  639. wscript.echo L_Help_Help_General17_Text
  640. wscript.echo L_Empty_Text
  641. wscript.echo L_Help_Help_General18_Text
  642. wscript.echo L_Help_Help_General19_Text
  643. wscript.echo L_Help_Help_General20_Text
  644. wscript.echo L_Help_Help_General21_Text
  645. wscript.echo L_Help_Help_General22_Text
  646. wscript.echo L_Help_Help_General23_Text
  647. wscript.echo L_Help_Help_General24_Text
  648. wscript.echo L_Help_Help_General25_Text
  649. wscript.echo L_Help_Help_General26_Text
  650. if bExit then
  651. wscript.quit(1)
  652. end if
  653. end sub
  654. '
  655. ' Determines which program is being used to run this script.
  656. ' Returns true if the script host is cscript.exe
  657. '
  658. function IsHostCscript()
  659. on error resume next
  660. dim strFullName
  661. dim strCommand
  662. dim i, j
  663. dim bReturn
  664. bReturn = false
  665. strFullName = WScript.FullName
  666. i = InStr(1, strFullName, ".exe", 1)
  667. if i <> 0 then
  668. j = InStrRev(strFullName, "\", i, 1)
  669. if j <> 0 then
  670. strCommand = Mid(strFullName, j+1, i-j-1)
  671. if LCase(strCommand) = "cscript" then
  672. bReturn = true
  673. end if
  674. end if
  675. end if
  676. if Err <> 0 then
  677. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  678. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  679. end if
  680. IsHostCscript = bReturn
  681. end function
  682. '
  683. ' Retrieves extended information about the last error that occured
  684. ' during a WBEM operation. The methods that set an SWbemLastError
  685. ' object are GetObject, PutInstance, DeleteInstance
  686. '
  687. sub LastError()
  688. on error resume next
  689. dim oError
  690. set oError = CreateObject("WbemScripting.SWbemLastError")
  691. if Err = kErrorSuccess then
  692. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  693. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  694. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  695. wscript.echo L_Text_Error_General03_Text & L_Space_Text & oError.StatusCode
  696. end if
  697. end sub
  698. '
  699. ' Connects to the WMI service on a server. oService is returned as a service
  700. ' object (SWbemServices)
  701. '
  702. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  703. on error resume next
  704. dim oLocator
  705. dim bResult
  706. oService = null
  707. bResult = false
  708. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  709. if Err = kErrorSuccess then
  710. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  711. if Err = kErrorSuccess then
  712. bResult = true
  713. oService.Security_.impersonationlevel = 3
  714. '
  715. ' Required to perform administrative tasks on the spooler service
  716. '
  717. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  718. Err.Clear
  719. else
  720. wscript.echo L_Text_Msg_General11_Text & L_Space_Text & L_Error_Text _
  721. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  722. & Err.Description
  723. end if
  724. else
  725. wscript.echo L_Text_Msg_General10_Text & L_Space_Text & L_Error_Text _
  726. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  727. & Err.Description
  728. end if
  729. WmiConnect = bResult
  730. end function
  731. '
  732. ' Remove leading "\\" from server name
  733. '
  734. function RemoveBackslashes(strServer)
  735. dim strRet
  736. strRet = strServer
  737. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  738. strRet = Mid(strServer, 3)
  739. end if
  740. RemoveBackslashes = strRet
  741. end function