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.

1065 lines
34 KiB

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. ' Abstract:
  6. '
  7. ' prncnfg.vbs - printer configuration script for WMI on Whistler used to get
  8. ' and set printer configuration also used to rename a printer
  9. '
  10. ' Usage:
  11. ' prncnfg [-gtx?] [-s server] [-p printer] [-u user name] [-w password]
  12. ' [-z new printer name] [-r port name] [-l location] [-m comment]
  13. ' [-h share name] [-f sep-file] [-y data-type] [-st start time]
  14. ' [-ut until time] [-o priority] [-i default priority]
  15. ' [<+|->rawonly][<+|->keepprintedjobs][<+|->queued][<+|->workoffline]
  16. ' [<+|->enabledevq][<+|->docompletefirst][<+|->enablebidi]
  17. '
  18. ' Examples:
  19. ' prncnfg -g -s server -p printer
  20. ' prncnfg -x -p printer -w "new Printer"
  21. ' prncnfg -t -s server -p Printer -l "Building A/Floor 100/Office 1" -m "Color Printer"
  22. ' prncnfg -t -p printer -h "Share" +shared -direct
  23. ' prncnfg -t -p printer +rawonly +keepprintedjobs
  24. ' prncnfg -t -p printer -st 2300 -ut 0215 -o 10 -i 5
  25. '
  26. '----------------------------------------------------------------------
  27. option explicit
  28. '
  29. ' Debugging trace flags, to enable debug output trace message
  30. ' change gDebugFlag to true.
  31. '
  32. const kDebugTrace = 1
  33. const kDebugError = 2
  34. dim gDebugFlag
  35. gDebugFlag = false
  36. const kFlagUpdateOnly = 1
  37. '
  38. ' Operation action values.
  39. '
  40. const kActionUnknown = 0
  41. const kActionSet = 1
  42. const kActionGet = 2
  43. const kActionRename = 3
  44. const kErrorSuccess = 0
  45. const kErrorFailure = 1
  46. '
  47. ' Constants for the parameter dictionary
  48. '
  49. const kServerName = 1
  50. const kPrinterName = 2
  51. const kNewPrinterName = 3
  52. const kShareName = 4
  53. const kPortName = 5
  54. const kDriverName = 6
  55. const kComment = 7
  56. const kLocation = 8
  57. const kSepFile = 9
  58. const kPrintProc = 10
  59. const kDataType = 11
  60. const kParameters = 12
  61. const kPriority = 13
  62. const kDefaultPriority = 14
  63. const kStartTime = 15
  64. const kUntilTime = 16
  65. const kQueued = 17
  66. const kDirect = 18
  67. const kDefault = 19
  68. const kShared = 20
  69. const kNetwork = 21
  70. const kHidden = 23
  71. const kLocal = 24
  72. const kEnableDevq = 25
  73. const kKeepPrintedJobs = 26
  74. const kDoCompleteFirst = 27
  75. const kWorkOffline = 28
  76. const kEnableBidi = 29
  77. const kRawOnly = 30
  78. const kPublished = 31
  79. const kUserName = 32
  80. const kPassword = 33
  81. const kNameSpace = "root\cimv2"
  82. '
  83. ' Generic strings
  84. '
  85. const L_Empty_Text = ""
  86. const L_Space_Text = " "
  87. const L_Error_Text = "Error"
  88. const L_Success_Text = "Success"
  89. const L_Failed_Text = "Failed"
  90. const L_Hex_Text = "0x"
  91. const L_Printer_Text = "Printer"
  92. const L_Operation_Text = "Operation"
  93. const L_Provider_Text = "Provider"
  94. const L_Description_Text = "Description"
  95. const L_Debug_Text = "Debug:"
  96. '
  97. ' General usage messages
  98. '
  99. const L_Help_Help_General01_Text = "Usage: prncnfg [-gtx?] [-s server][-p printer][-z new printer name]"
  100. const L_Help_Help_General02_Text = " [-u user name][-w password][-r port name][-l location]"
  101. const L_Help_Help_General03_Text = " [-m comment][-h share name][-f sep file][-y datatype]"
  102. const L_Help_Help_General04_Text = " [-st start time][-ut until time][-i default priority]"
  103. const L_Help_Help_General05_Text = " [-o priority][<+|->shared][<+|->direct][<+|->hidden]"
  104. const L_Help_Help_General06_Text = " [<+|->published][<+|->rawonly][<+|->queued][<+|->enablebidi]"
  105. const L_Help_Help_General07_Text = " [<+|->keepprintedjobs][<+|->workoffline][<+|->enabledevq]"
  106. const L_Help_Help_General08_Text = " [<+|->docompletefirst]"
  107. const L_Help_Help_General09_Text = "Arguments:"
  108. const L_Help_Help_General10_Text = "-f - separator file name"
  109. const L_Help_Help_General11_Text = "-g - get configuration"
  110. const L_Help_Help_General12_Text = "-h - share name"
  111. const L_Help_Help_General13_Text = "-i - default priority"
  112. const L_Help_Help_General14_Text = "-l - location string"
  113. const L_Help_Help_General15_Text = "-m - comment string"
  114. const L_Help_Help_General16_Text = "-o - priority"
  115. const L_Help_Help_General17_Text = "-p - printer name"
  116. const L_Help_Help_General18_Text = "-r - port name"
  117. const L_Help_Help_General19_Text = "-s - server name"
  118. const L_Help_Help_General20_Text = "-st - start time"
  119. const L_Help_Help_General21_Text = "-t - set configuration"
  120. const L_Help_Help_General22_Text = "-u - user name"
  121. const L_Help_Help_General23_Text = "-ut - until time"
  122. const L_Help_Help_General24_Text = "-w - password"
  123. const L_Help_Help_General25_Text = "-x - change printer name"
  124. const L_Help_Help_General26_Text = "-y - data type string"
  125. const L_Help_Help_General27_Text = "-z - new printer name"
  126. const L_Help_Help_General28_Text = "-? - display command usage"
  127. const L_Help_Help_General29_Text = "Examples:"
  128. const L_Help_Help_General30_Text = "prncnfg -g -s server -p printer"
  129. const L_Help_Help_General31_Text = "prncnfg -x -s server -p printer -z ""new printer"""
  130. const L_Help_Help_General32_Text = "prncnfg -t -p printer -l ""Building A/Floor 100/Office 1"" -m ""Color Printer"""
  131. const L_Help_Help_General33_Text = "prncnfg -t -p printer -h ""Share"" +shared -direct"
  132. const L_Help_Help_General34_Text = "prncnfg -t -p printer +rawonly +keepprintedjobs"
  133. const L_Help_Help_General35_Text = "prncnfg -t -p printer -st 2300 -ut 0215 -o 1 -i 5"
  134. '
  135. ' Messages to be displayed if the scripting host is not cscript
  136. '
  137. const L_Help_Help_Host01_Text = "Please run this script using CScript."
  138. const L_Help_Help_Host02_Text = "This can be achieved by"
  139. const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
  140. const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
  141. const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
  142. const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
  143. '
  144. ' General error messages
  145. '
  146. const L_Text_Error_General01_Text = "The scripting host could not be determined."
  147. const L_Text_Error_General02_Text = "Unable to parse command line."
  148. const L_Text_Error_General03_Text = "Win32 error code"
  149. '
  150. ' Miscellaneous messages
  151. '
  152. const L_Text_Msg_General01_Text = "Renamed printer"
  153. const L_Text_Msg_General02_Text = "New printer name"
  154. const L_Text_Msg_General03_Text = "Unable to rename printer"
  155. const L_Text_Msg_General04_Text = "Unable to get configuration for printer"
  156. const L_Text_Msg_General05_Text = "Printer always available"
  157. const L_Text_Msg_General06_Text = "Configured printer"
  158. const L_Text_Msg_General07_Text = "Unable to configure printer"
  159. const L_Text_Msg_General08_Text = "Unable to get SWbemLocator object"
  160. const L_Text_Msg_General09_Text = "Unable to connect to WMI service"
  161. '
  162. ' Printer properties
  163. '
  164. const L_Text_Msg_Printer01_Text = "Server name"
  165. const L_Text_Msg_Printer02_Text = "Printer name"
  166. const L_Text_Msg_Printer03_Text = "Share name"
  167. const L_Text_Msg_Printer04_Text = "Driver name"
  168. const L_Text_Msg_Printer05_Text = "Port name"
  169. const L_Text_Msg_Printer06_Text = "Comment"
  170. const L_Text_Msg_Printer07_Text = "Location"
  171. const L_Text_Msg_Printer08_Text = "Separator file"
  172. const L_Text_Msg_Printer09_Text = "Print processor"
  173. const L_Text_Msg_Printer10_Text = "Data type"
  174. const L_Text_Msg_Printer11_Text = "Parameters"
  175. const L_Text_Msg_Printer12_Text = "Attributes"
  176. const L_Text_Msg_Printer13_Text = "Priority"
  177. const L_Text_Msg_Printer14_Text = "Default priority"
  178. const L_Text_Msg_Printer15_Text = "Start time"
  179. const L_Text_Msg_Printer16_Text = "Until time"
  180. const L_Text_Msg_Printer17_Text = "Status"
  181. const L_Text_Msg_Printer18_Text = "Job count"
  182. const L_Text_Msg_Printer19_Text = "Average pages per minute"
  183. '
  184. ' Printer attributes
  185. '
  186. const L_Text_Msg_Attrib01_Text = "direct"
  187. const L_Text_Msg_Attrib02_Text = "raw_only"
  188. const L_Text_Msg_Attrib03_Text = "local"
  189. const L_Text_Msg_Attrib04_Text = "shared"
  190. const L_Text_Msg_Attrib05_Text = "keep_printed_jobs"
  191. const L_Text_Msg_Attrib06_Text = "published"
  192. const L_Text_Msg_Attrib07_Text = "queued"
  193. const L_Text_Msg_Attrib08_Text = "default"
  194. const L_Text_Msg_Attrib09_Text = "network"
  195. const L_Text_Msg_Attrib10_Text = "enable_bidi"
  196. const L_Text_Msg_Attrib11_Text = "do_complete_first"
  197. const L_Text_Msg_Attrib12_Text = "work_offline"
  198. const L_Text_Msg_Attrib13_Text = "hidden"
  199. const L_Text_Msg_Attrib14_Text = "enable_devq_print"
  200. '
  201. ' Debug messages
  202. '
  203. const L_Text_Dbg_Msg01_Text = "In function RenamePrinter"
  204. const L_Text_Dbg_Msg02_Text = "New printer name"
  205. const L_Text_Dbg_Msg03_Text = "In function GetPrinter"
  206. const L_Text_Dbg_Msg04_Text = "In function SetPrinter"
  207. const L_Text_Dbg_Msg05_Text = "In function ParseCommandLine"
  208. main
  209. '
  210. ' Main execution starts here
  211. '
  212. sub main
  213. dim iAction
  214. dim iRetval
  215. dim oParamDict
  216. '
  217. ' Abort if the host is not cscript
  218. '
  219. if not IsHostCscript() then
  220. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  221. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  222. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  223. wscript.quit
  224. end if
  225. set oParamDict = CreateObject("Scripting.Dictionary")
  226. iRetval = ParseCommandLine(iAction, oParamDict)
  227. if iRetval = kErrorSuccess then
  228. select case iAction
  229. case kActionSet
  230. iRetval = SetPrinter(oParamDict)
  231. case kActionGet
  232. iRetval = GetPrinter(oParamDict)
  233. case kActionRename
  234. iRetval = RenamePrinter(oParamDict)
  235. case else
  236. Usage(True)
  237. exit sub
  238. end select
  239. end if
  240. end sub
  241. '
  242. ' Rename printer
  243. '
  244. function RenamePrinter(oParamDict)
  245. on error resume next
  246. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  247. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & oParamDict.Item(kServerName)
  248. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & oParamDict.Item(kPrinterName)
  249. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text & L_Space_Text & oParamDict.Item(kNewPrinterName)
  250. dim oPrinter
  251. dim oService
  252. dim iRetval
  253. dim uResult
  254. dim strServer
  255. dim strPrinter
  256. dim strNewName
  257. dim strUser
  258. dim strPassword
  259. iRetval = kErrorFailure
  260. strServer = oParamDict.Item(kServerName)
  261. strPrinter = oParamDict.Item(kPrinterName)
  262. strNewName = oParamDict.Item(kNewPrinterName)
  263. strUser = oParamDict.Item(kUserName)
  264. strPassword = oParamDict.Item(kPassword)
  265. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  266. set oPrinter = oService.Get("Win32_Printer.DeviceID='" & strPrinter & "'")
  267. else
  268. RenamePrinter = kErrorFailure
  269. exit function
  270. end if
  271. '
  272. ' Check if Get was successful
  273. '
  274. if Err.Number = kErrorSuccess then
  275. uResult = oPrinter.RenamePrinter(strNewName)
  276. if Err.Number = kErrorSuccess then
  277. if uResult = kErrorSuccess then
  278. wscript.echo L_Text_Msg_General01_Text & L_Space_Text & strPrinter
  279. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & strNewName
  280. iRetval = kErrorSuccess
  281. else
  282. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & strPrinter & L_Space_Text _
  283. & L_Text_Error_General03_Text & L_Space_Text & uResult
  284. end if
  285. else
  286. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  287. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  288. & Err.Description
  289. end if
  290. else
  291. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  292. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  293. & Err.Description
  294. '
  295. ' Try getting extended error information
  296. '
  297. call LastError()
  298. end if
  299. RenamePrinter = iRetval
  300. end function
  301. '
  302. ' Get printer configuration
  303. '
  304. function GetPrinter(oParamDict)
  305. on error resume next
  306. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
  307. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & oParamDict.Item(kServerName)
  308. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & oParamDict.Item(kPrinterName)
  309. dim oPrinter
  310. dim oService
  311. dim iRetval
  312. dim uResult
  313. dim strServer
  314. dim strPrinter
  315. dim strAttributes
  316. dim strStart
  317. dim strEnd
  318. dim strUser
  319. dim strPassword
  320. iRetval = kErrorFailure
  321. strServer = oParamDict.Item(kServerName)
  322. strPrinter = oParamDict.Item(kPrinterName)
  323. strUser = oParamDict.Item(kUserName)
  324. strPassword = oParamDict.Item(kPassword)
  325. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  326. set oPrinter = oService.Get("Win32_Printer='" & strPrinter & "'")
  327. else
  328. GetPrinter = kErrorFailure
  329. exit function
  330. end if
  331. '
  332. ' Check if Get was successful
  333. '
  334. if Err.Number = kErrorSuccess then
  335. wscript.echo L_Text_Msg_Printer01_Text & L_Space_Text & strServer
  336. wscript.echo L_Text_Msg_Printer02_Text & L_Space_Text & oPrinter.DeviceID
  337. wscript.echo L_Text_Msg_Printer03_Text & L_Space_Text & oPrinter.ShareName
  338. wscript.echo L_Text_Msg_Printer04_Text & L_Space_Text & oPrinter.DriverName
  339. wscript.echo L_Text_Msg_Printer05_Text & L_Space_Text & oPrinter.PortName
  340. wscript.echo L_Text_Msg_Printer06_Text & L_Space_Text & oPrinter.Comment
  341. wscript.echo L_Text_Msg_Printer07_Text & L_Space_Text & oPrinter.Location
  342. wscript.echo L_Text_Msg_Printer08_Text & L_Space_Text & oPrinter.SeparatorFile
  343. wscript.echo L_Text_Msg_Printer09_Text & L_Space_Text & oPrinter.PrintProcessor
  344. wscript.echo L_Text_Msg_Printer10_Text & L_Space_Text & oPrinter.PrintJobDatatype
  345. wscript.echo L_Text_Msg_Printer11_Text & L_Space_Text & oPrinter.Parameters
  346. wscript.echo L_Text_Msg_Printer13_Text & L_Space_Text & CStr(oPrinter.Priority)
  347. wscript.echo L_Text_Msg_Printer14_Text & L_Space_Text & CStr(oPrinter.DefaultPriority)
  348. strStart = Mid(CStr(oPrinter.StartTime), 9, 4)
  349. strEnd = Mid(CStr(oPrinter.UntilTime), 9, 4)
  350. if strStart <> "" and strEnd <> "" then
  351. wscript.echo L_Text_Msg_Printer15_Text & L_Space_Text & Mid(strStart, 1, 2) & "h" & Mid(strStart, 3, 2)
  352. wscript.echo L_Text_Msg_Printer16_Text & L_Space_Text & Mid(strEnd, 1, 2) & "h" & Mid(strEnd, 3, 2)
  353. else
  354. wscript.echo L_Text_Msg_General05_Text
  355. end if
  356. strAttributes = L_Text_Msg_Printer12_Text
  357. if oPrinter.Direct then
  358. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib01_Text
  359. end if
  360. if oPrinter.RawOnly then
  361. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib02_Text
  362. end if
  363. if oPrinter.Local then
  364. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib03_Text
  365. end if
  366. if oPrinter.Shared then
  367. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib04_Text
  368. end if
  369. if oPrinter.KeepPrintedJobs then
  370. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib05_Text
  371. end if
  372. if oPrinter.Published then
  373. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib06_Text
  374. end if
  375. if oPrinter.Queued then
  376. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib07_Text
  377. end if
  378. if oPrinter.Default then
  379. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib08_Text
  380. end if
  381. if oPrinter.Network then
  382. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib09_Text
  383. end if
  384. if oPrinter.EnableBiDi then
  385. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib10_Text
  386. end if
  387. if oPrinter.DoCompleteFirst then
  388. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib11_Text
  389. end if
  390. if oPrinter.WorkOffline then
  391. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib12_Text
  392. end if
  393. if oPrinter.Hidden then
  394. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib13_Text
  395. end if
  396. if oPrinter.EnableDevQueryPrint then
  397. strAttributes = strAttributes + L_Space_Text + L_Text_Msg_Attrib14_Text
  398. end if
  399. wscript.echo strAttributes
  400. wscript.echo
  401. iRetval = kErrorSuccess
  402. else
  403. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & oParamDict.Item(kPrinterName) & L_Space_Text _
  404. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  405. & Err.Description
  406. '
  407. ' Try getting extended error information
  408. '
  409. call LastError()
  410. end if
  411. GetPrinter = iRetval
  412. end function
  413. '
  414. ' Configure a printer
  415. '
  416. function SetPrinter(oParamDict)
  417. on error resume next
  418. DebugPrint kDebugTrace, L_Text_Dbg_Msg04_Text
  419. DebugPrint kDebugTrace, L_Text_Msg_Printer01_Text & L_Space_Text & oParamDict.Item(kServerName)
  420. DebugPrint kDebugTrace, L_Text_Msg_Printer02_Text & L_Space_Text & oParamDict.Item(kPrinterName)
  421. dim oPrinter
  422. dim oService
  423. dim iRetval
  424. dim uResult
  425. dim strServer
  426. dim strPrinter
  427. dim strUser
  428. dim strPassword
  429. iRetval = kErrorFailure
  430. strServer = oParamDict.Item(kServerName)
  431. strPrinter = oParamDict.Item(kPrinterName)
  432. strNewName = oParamDict.Item(kNewPrinterName)
  433. strUser = oParamDict.Item(kUserName)
  434. strPassword = oParamDict.Item(kPassword)
  435. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  436. set oPrinter = oService.Get("Win32_Printer='" & strPrinter & "'")
  437. else
  438. SetPrinter = kErrorFailure
  439. exit function
  440. end if
  441. '
  442. ' Check if Get was successful
  443. '
  444. if Err.Number = kErrorSuccess then
  445. if oParamdict.Exists(kPortName) then oPrinter.PortName = oParamDict.Item(kPortName) end if
  446. if oParamdict.Exists(kDriverName) then oPrinter.DriverName = oParamDict.Item(kDriverName) end if
  447. if oParamdict.Exists(kShareName) then oPrinter.ShareName = oParamDict.Item(kShareName) end if
  448. if oParamdict.Exists(kLocation) then oPrinter.Location = oParamDict.Item(kLocation) end if
  449. if oParamdict.Exists(kComment) then oPrinter.Comment = oParamDict.Item(kComment) end if
  450. if oParamdict.Exists(kDataType) then oPrinter.PrintJobDataType = oParamDict.Item(kDataType) end if
  451. if oParamdict.Exists(kSepFile) then oPrinter.SeparatorFile = oParamDict.Item(kSepfile) end if
  452. if oParamdict.Exists(kParameters) then oPrinter.Parameters = oParamDict.Item(kParameters) end if
  453. if oParamdict.Exists(kPriority) then oPrinter.Priority = oParamDict.Item(kPriority) end if
  454. if oParamdict.Exists(kDefaultPriority) then oPrinter.DefaultPriority = oParamDict.Item(kDefaultPriority) end if
  455. if oParamdict.Exists(kPrintProc) then oPrinter.PrintProc = oParamDict.Item(kPrintProc) end if
  456. if oParamdict.Exists(kStartTime) then oPrinter.StartTime = oParamDict.Item(kStartTime) end if
  457. if oParamdict.Exists(kUntilTime) then oPrinter.UntilTime = oParamDict.Item(kUntilTime) end if
  458. if oParamdict.Exists(kQueued) then oPrinter.Queued = oParamDict.Item(kQueued) end if
  459. if oParamdict.Exists(kDirect) then oPrinter.Direct = oParamDict.Item(kDirect) end if
  460. if oParamdict.Exists(kShared) then oPrinter.Shared = oParamDict.Item(kShared) end if
  461. if oParamdict.Exists(kHidden) then oPrinter.Hidden = oParamDict.Item(kHidden) end if
  462. if oParamdict.Exists(kEnabledevq) then oPrinter.EnableDevQueryPrint = oParamDict.Item(kEnabledevq) end if
  463. if oParamdict.Exists(kKeepPrintedJobs) then oPrinter.KeepPrintedJobs = oParamDict.Item(kKeepPrintedJobs) end if
  464. if oParamdict.Exists(kDoCompleteFirst) then oPrinter.DoCompleteFirst = oParamDict.Item(kDoCompleteFirst) end if
  465. if oParamdict.Exists(kWorkOffline) then oPrinter.WorkOffline = oParamDict.Item(kWorkOffline) end if
  466. if oParamdict.Exists(kEnableBidi) then oPrinter.EnableBidi = oParamDict.Item(kEnableBidi) end if
  467. if oParamdict.Exists(kRawonly) then oPrinter.RawOnly = oParamDict.Item(kRawonly) end if
  468. if oParamdict.Exists(kPublished) then oPrinter.Published = oParamDict.Item(kPublished) end if
  469. oPrinter.Put_(kFlagUpdateOnly)
  470. if Err.Number = kErrorSuccess then
  471. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & strPrinter
  472. iRetval = kErrorSuccess
  473. else
  474. wscript.echo L_Text_Msg_General07_Text & L_Space_Text & strPrinter & L_Space_Text _
  475. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  476. & Err.Description
  477. '
  478. ' Try getting extended error information
  479. '
  480. call LastError()
  481. end if
  482. else
  483. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & strPrinter & L_Space_Text _
  484. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  485. & Err.Description
  486. '
  487. ' Try getting extended error information
  488. '
  489. call LastError()
  490. end if
  491. SetPrinter = iRetval
  492. end function
  493. '
  494. ' Debug display helper function
  495. '
  496. sub DebugPrint(uFlags, strString)
  497. if gDebugFlag = true then
  498. if uFlags = kDebugTrace then
  499. wscript.echo L_Debug_Text & L_Space_Text & strString
  500. end if
  501. if uFlags = kDebugError then
  502. if Err <> 0 then
  503. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  504. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  505. & L_Space_Text & Err.Description
  506. end if
  507. end if
  508. end if
  509. end sub
  510. '
  511. ' Parse the command line into its components
  512. '
  513. function ParseCommandLine(iAction, oParamdict)
  514. on error resume next
  515. DebugPrint kDebugTrace, L_Text_Dbg_Msg05_Text
  516. dim oArgs
  517. dim iIndex
  518. iAction = kActionUnknown
  519. iIndex = 0
  520. set oArgs = wscript.Arguments
  521. while iIndex < oArgs.Count
  522. select case oArgs(iIndex)
  523. case "-g"
  524. iAction = kActionGet
  525. case "-t"
  526. iAction = kActionSet
  527. case "-x"
  528. iAction = kActionRename
  529. case "-p"
  530. iIndex = iIndex + 1
  531. oParamdict.Add kPrinterName, oArgs(iIndex)
  532. case "-s"
  533. iIndex = iIndex + 1
  534. oParamdict.Add kServerName, RemoveBackslashes(oArgs(iIndex))
  535. case "-r"
  536. iIndex = iIndex + 1
  537. oParamdict.Add kPortName, oArgs(iIndex)
  538. case "-h"
  539. iIndex = iIndex + 1
  540. oParamdict.Add kShareName, oArgs(iIndex)
  541. case "-m"
  542. iIndex = iIndex + 1
  543. oParamdict.Add kComment, oArgs(iIndex)
  544. case "-l"
  545. iIndex = iIndex + 1
  546. oParamdict.Add kLocation, oArgs(iIndex)
  547. case "-y"
  548. iIndex = iIndex + 1
  549. oParamdict.Add kDataType, oArgs(iIndex)
  550. case "-f"
  551. iIndex = iIndex + 1
  552. oParamdict.Add kSepFile, oArgs(iIndex)
  553. case "-z"
  554. iIndex = iIndex + 1
  555. oParamdict.Add kNewPrinterName, oArgs(iIndex)
  556. case "-u"
  557. iIndex = iIndex + 1
  558. oParamdict.Add kUserName, oArgs(iIndex)
  559. case "-w"
  560. iIndex = iIndex + 1
  561. oParamdict.Add kPassword, oArgs(iIndex)
  562. case "-st"
  563. iIndex = iIndex + 1
  564. oParamdict.Add kStartTime, "********" & oArgs(iIndex) & "00.000000+000"
  565. case "-o"
  566. iIndex = iIndex + 1
  567. oParamdict.Add kPriority, oArgs(iIndex)
  568. case "-i"
  569. iIndex = iIndex + 1
  570. oParamdict.Add kDefaultPriority, oArgs(iIndex)
  571. case "-ut"
  572. iIndex = iIndex + 1
  573. oParamdict.Add kUntilTime, "********" & oArgs(iIndex) & "00.000000+000"
  574. case "-queued"
  575. oParamdict.Add kQueued, false
  576. case "+queued"
  577. oParamdict.Add kQueued, true
  578. case "-direct"
  579. oParamdict.Add kDirect, false
  580. case "+direct"
  581. oParamdict.Add kDirect, true
  582. case "-shared"
  583. oParamdict.Add kShared, false
  584. case "+shared"
  585. oParamdict.Add kShared, true
  586. case "-hidden"
  587. oParamdict.Add kHidden, false
  588. case "+hidden"
  589. oParamdict.Add kHidden, true
  590. case "-enabledevq"
  591. oParamdict.Add kEnabledevq, false
  592. case "+enabledevq"
  593. oParamdict.Add kEnabledevq, true
  594. case "-keepprintedjobs"
  595. oParamdict.Add kKeepprintedjobs, false
  596. case "+keepprintedjobs"
  597. oParamdict.Add kKeepprintedjobs, true
  598. case "-docompletefirst"
  599. oParamdict.Add kDocompletefirst, false
  600. case "+docompletefirst"
  601. oParamdict.Add kDocompletefirst, true
  602. case "-workoffline"
  603. oParamdict.Add kWorkoffline, false
  604. case "+workoffline"
  605. oParamdict.Add kWorkoffline, true
  606. case "-enablebidi"
  607. oParamdict.Add kEnablebidi, false
  608. case "+enablebidi"
  609. oParamdict.Add kEnablebidi, true
  610. case "-rawonly"
  611. oParamdict.Add kRawonly, false
  612. case "+rawonly"
  613. oParamdict.Add kRawonly, true
  614. case "-published"
  615. oParamdict.Add kPublished, false
  616. case "+published"
  617. oParamdict.Add kPublished, true
  618. case "-?"
  619. Usage(true)
  620. exit function
  621. case else
  622. Usage(true)
  623. exit function
  624. end select
  625. iIndex = iIndex + 1
  626. wend
  627. if Err = kErrorSuccess then
  628. ParseCommandLine = kErrorSuccess
  629. else
  630. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  631. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  632. ParseCommandLine = kErrorFailure
  633. end if
  634. end function
  635. '
  636. ' Display command usage.
  637. '
  638. sub Usage(bExit)
  639. wscript.echo L_Help_Help_General01_Text
  640. wscript.echo L_Help_Help_General02_Text
  641. wscript.echo L_Help_Help_General03_Text
  642. wscript.echo L_Help_Help_General04_Text
  643. wscript.echo L_Help_Help_General05_Text
  644. wscript.echo L_Help_Help_General06_Text
  645. wscript.echo L_Help_Help_General07_Text
  646. wscript.echo L_Help_Help_General08_Text
  647. wscript.echo L_Help_Help_General09_Text
  648. wscript.echo L_Help_Help_General10_Text
  649. wscript.echo L_Help_Help_General11_Text
  650. wscript.echo L_Help_Help_General12_Text
  651. wscript.echo L_Help_Help_General13_Text
  652. wscript.echo L_Help_Help_General14_Text
  653. wscript.echo L_Help_Help_General15_Text
  654. wscript.echo L_Help_Help_General16_Text
  655. wscript.echo L_Help_Help_General17_Text
  656. wscript.echo L_Help_Help_General18_Text
  657. wscript.echo L_Help_Help_General19_Text
  658. wscript.echo L_Help_Help_General20_Text
  659. wscript.echo L_Help_Help_General21_Text
  660. wscript.echo L_Help_Help_General22_Text
  661. wscript.echo L_Help_Help_General23_Text
  662. wscript.echo L_Help_Help_General24_Text
  663. wscript.echo L_Help_Help_General25_Text
  664. wscript.echo L_Help_Help_General26_Text
  665. wscript.echo L_Help_Help_General27_Text
  666. wscript.echo L_Empty_Text
  667. wscript.echo L_Help_Help_General28_Text
  668. wscript.echo L_Help_Help_General29_Text
  669. wscript.echo L_Help_Help_General30_Text
  670. wscript.echo L_Help_Help_General31_Text
  671. wscript.echo L_Help_Help_General32_Text
  672. wscript.echo L_Help_Help_General33_Text
  673. wscript.echo L_Help_Help_General34_Text
  674. wscript.echo L_Help_Help_General35_Text
  675. if bExit then
  676. wscript.quit(1)
  677. end if
  678. end sub
  679. '
  680. ' Determines which program is being used to run this script.
  681. ' Returns true if the script host is cscript.exe
  682. '
  683. function IsHostCscript()
  684. on error resume next
  685. dim strFullName
  686. dim strCommand
  687. dim i, j
  688. dim bReturn
  689. bReturn = false
  690. strFullName = WScript.FullName
  691. i = InStr(1, strFullName, ".exe", 1)
  692. if i <> 0 then
  693. j = InStrRev(strFullName, "\", i, 1)
  694. if j <> 0 then
  695. strCommand = Mid(strFullName, j+1, i-j-1)
  696. if LCase(strCommand) = "cscript" then
  697. bReturn = true
  698. end if
  699. end if
  700. end if
  701. if Err <> 0 then
  702. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  703. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  704. end if
  705. IsHostCscript = bReturn
  706. end function
  707. '
  708. ' Retrieves extended information about the last error that occured
  709. ' during a WBEM operation. The methods that set an SWbemLastError
  710. ' object are GetObject, PutInstance, DeleteInstance
  711. '
  712. sub LastError()
  713. on error resume next
  714. dim oError
  715. set oError = CreateObject("WbemScripting.SWbemLastError")
  716. if Err = kErrorSuccess then
  717. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  718. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  719. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  720. wscript.echo L_Text_Error_General03_Text & L_Space_Text & oError.StatusCode
  721. end if
  722. end sub
  723. '
  724. ' Connects to the WMI service on a server. oService is returned as a service
  725. ' object (SWbemServices)
  726. '
  727. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  728. on error resume next
  729. dim oLocator
  730. dim bResult
  731. oService = null
  732. bResult = false
  733. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  734. if Err = kErrorSuccess then
  735. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  736. if Err = kErrorSuccess then
  737. bResult = true
  738. oService.Security_.impersonationlevel = 3
  739. '
  740. ' Required to perform administrative tasks on the spooler service
  741. '
  742. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  743. Err.Clear
  744. else
  745. wscript.echo L_Text_Msg_General08_Text & L_Space_Text & L_Error_Text _
  746. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  747. & Err.Description
  748. end if
  749. else
  750. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & L_Error_Text _
  751. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  752. & Err.Description
  753. end if
  754. WmiConnect = bResult
  755. end function
  756. '
  757. ' Remove leading "\\" from server name
  758. '
  759. function RemoveBackslashes(strServer)
  760. dim strRet
  761. strRet = strServer
  762. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  763. strRet = Mid(strServer, 3)
  764. end if
  765. RemoveBackslashes = strRet
  766. end function