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.

989 lines
28 KiB

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. ' Abstract:
  6. ' prnport.vbs - Port script for WMI on Whistler
  7. ' used to add, delete and list ports
  8. ' also for getting and setting the port configuration
  9. '
  10. ' Usage:
  11. ' prnport [-adlgt?] [-r port] [-s server] [-u user name] [-w password]
  12. ' [-o raw|lpr] [-h host address] [-q queue] [-n number]
  13. ' [-me | -md ] [-i SNMP index] [-y community] [-2e | -2d]"
  14. '
  15. ' Examples
  16. ' prnport -a -s server -r IP_1.2.3.4 -e 1.2.3.4 -o raw -n 9100
  17. ' prnport -d -s server -r c:\temp\foo.prn
  18. ' prnport -l -s server
  19. ' prnport -g -s server -r IP_1.2.3.4
  20. ' prnport -t -s server -r IP_1.2.3.4 -me -y public -i 1 -n 9100
  21. '
  22. '----------------------------------------------------------------------
  23. option explicit
  24. '
  25. ' Debugging trace flags, to enable debug output trace message
  26. ' change gDebugFlag to true.
  27. '
  28. dim gDebugFlag
  29. const kDebugTrace = 1
  30. const kDebugError = 2
  31. gDebugFlag = false
  32. '
  33. ' Operation action values.
  34. '
  35. const kActionAdd = 0
  36. const kActionDelete = 1
  37. const kActionList = 2
  38. const kActionUnknown = 3
  39. const kActionGet = 4
  40. const kActionSet = 5
  41. const kErrorSuccess = 0
  42. const KErrorFailure = 1
  43. const kFlagCreateOrUpdate = 0
  44. const kNameSpace = "root\cimv2"
  45. '
  46. ' Constants for the parameter dictionary
  47. '
  48. const kServerName = 1
  49. const kPortName = 2
  50. const kDoubleSpool = 3
  51. const kPortNumber = 4
  52. const kPortType = 5
  53. const kHostAddress = 6
  54. const kSNMPDeviceIndex = 7
  55. const kCommunityName = 8
  56. const kSNMP = 9
  57. const kQueueName = 10
  58. const kUserName = 11
  59. const kPassword = 12
  60. '
  61. ' Generic strings
  62. '
  63. const L_Empty_Text = ""
  64. const L_Space_Text = " "
  65. const L_Error_Text = "Error"
  66. const L_Success_Text = "Success"
  67. const L_Failed_Text = "Failed"
  68. const L_Hex_Text = "0x"
  69. const L_Printer_Text = "Printer"
  70. const L_Operation_Text = "Operation"
  71. const L_Provider_Text = "Provider"
  72. const L_Description_Text = "Description"
  73. const L_Debug_Text = "Debug:"
  74. '
  75. ' General usage messages
  76. '
  77. const L_Help_Help_General01_Text = "Usage: prnport [-adlgt?] [-r port][-s server][-u user name][-w password]"
  78. const L_Help_Help_General02_Text = " [-o raw|lpr][-h host address][-q queue][-n number]"
  79. const L_Help_Help_General03_Text = " [-me | -md ][-i SNMP index][-y community][-2e | -2d]"
  80. const L_Help_Help_General04_Text = "Arguments:"
  81. const L_Help_Help_General05_Text = "-a - add a port"
  82. const L_Help_Help_General06_Text = "-d - delete the specified port"
  83. const L_Help_Help_General07_Text = "-g - get configuration for a TCP port"
  84. const L_Help_Help_General08_Text = "-h - IP address of the device"
  85. const L_Help_Help_General09_Text = "-i - SNMP index, if SNMP is enabled"
  86. const L_Help_Help_General10_Text = "-l - list all TCP ports"
  87. const L_Help_Help_General11_Text = "-m - SNMP type. [e] enable, [d] disable"
  88. const L_Help_Help_General12_Text = "-n - port number, applies to TCP RAW ports"
  89. const L_Help_Help_General13_Text = "-o - port type, raw or lpr"
  90. const L_Help_Help_General14_Text = "-q - queue name, applies to TCP LPR ports"
  91. const L_Help_Help_General15_Text = "-r - port name"
  92. const L_Help_Help_General16_Text = "-s - server name"
  93. const L_Help_Help_General17_Text = "-t - set configuration for a TCP port"
  94. const L_Help_Help_General18_Text = "-u - user name"
  95. const L_Help_Help_General19_Text = "-w - password"
  96. const L_Help_Help_General20_Text = "-y - community name, if SNMP is enabled"
  97. const L_Help_Help_General21_Text = "-2 - double spool, applies to TCP LPR ports. [e] enable, [d] disable"
  98. const L_Help_Help_General22_Text = "-? - display command usage"
  99. const L_Help_Help_General23_Text = "Examples:"
  100. const L_Help_Help_General24_Text = "prnport -l -s server"
  101. const L_Help_Help_General25_Text = "prnport -d -s server -r IP_1.2.3.4"
  102. const L_Help_Help_General26_Text = "prnport -a -s server -r IP_1.2.3.4 -h 1.2.3.4 -o raw -n 9100"
  103. const L_Help_Help_General27_Text = "prnport -t -s server -r IP_1.2.3.4 -me -y public -i 1 -n 9100"
  104. const L_Help_Help_General28_Text = "prnport -g -s server -r IP_1.2.3.4"
  105. const L_Help_Help_General29_Text = "prnport -a -r IP_1.2.3.4 -h 1.2.3.4"
  106. const L_Help_Help_General30_Text = "Remark:"
  107. const L_Help_Help_General31_Text = "The last example will try to get the device settings at the specified IP address."
  108. const L_Help_Help_General32_Text = "If a device is detected, then a TCP port is added with the preferred settings for that device."
  109. '
  110. ' Messages to be displayed if the scripting host is not cscript
  111. '
  112. const L_Help_Help_Host01_Text = "Please run this script using CScript."
  113. const L_Help_Help_Host02_Text = "This can be achieved by"
  114. const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
  115. const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
  116. const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
  117. const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
  118. '
  119. ' General error messages
  120. '
  121. const L_Text_Error_General01_Text = "The scripting host could not be determined."
  122. const L_Text_Error_General02_Text = "Unable to parse command line."
  123. const L_Text_Error_General03_Text = "Win32 error code"
  124. '
  125. ' Miscellaneous messages
  126. '
  127. const L_Text_Msg_General01_Text = "Added port"
  128. const L_Text_Msg_General02_Text = "Unable to delete port"
  129. const L_Text_Msg_General03_Text = "Unable to get port"
  130. const L_Text_Msg_General04_Text = "Created/updated port"
  131. const L_Text_Msg_General05_Text = "Unable to create/update port"
  132. const L_Text_Msg_General06_Text = "Unable to enumerate ports"
  133. const L_Text_Msg_General07_Text = "Number of ports enumerated"
  134. const L_Text_Msg_General08_Text = "Deleted port"
  135. const L_Text_Msg_General09_Text = "Unable to get SWbemLocator object"
  136. const L_Text_Msg_General10_Text = "Unable to connect to WMI service"
  137. '
  138. ' Port properties
  139. '
  140. const L_Text_Msg_Port01_Text = "Server name"
  141. const L_Text_Msg_Port02_Text = "Port name"
  142. const L_Text_Msg_Port03_Text = "Host address"
  143. const L_Text_Msg_Port04_Text = "Protocol RAW"
  144. const L_Text_Msg_Port05_Text = "Protocol LPR"
  145. const L_Text_Msg_Port06_Text = "Port number"
  146. const L_Text_Msg_Port07_Text = "Queue"
  147. const L_Text_Msg_Port08_Text = "Byte Count Enabled"
  148. const L_Text_Msg_Port09_Text = "Byte Count Disabled"
  149. const L_Text_Msg_Port10_Text = "SNMP Enabled"
  150. const L_Text_Msg_Port11_Text = "SNMP Disabled"
  151. const L_Text_Msg_Port12_Text = "Community"
  152. const L_Text_Msg_Port13_Text = "Device index"
  153. '
  154. ' Debug messages
  155. '
  156. const L_Text_Dbg_Msg01_Text = "In function DelPort"
  157. const L_Text_Dbg_Msg02_Text = "In function CreateOrSetPort"
  158. const L_Text_Dbg_Msg03_Text = "In function ListPorts"
  159. const L_Text_Dbg_Msg04_Text = "In function GetPort"
  160. const L_Text_Dbg_Msg05_Text = "In function ParseCommandLine"
  161. main
  162. '
  163. ' Main execution starts here
  164. '
  165. sub main
  166. on error resume next
  167. dim iAction
  168. dim iRetval
  169. dim oParamDict
  170. '
  171. ' Abort if the host is not cscript
  172. '
  173. if not IsHostCscript() then
  174. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  175. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  176. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  177. wscript.quit
  178. end if
  179. set oParamDict = CreateObject("Scripting.Dictionary")
  180. iRetval = ParseCommandLine(iAction, oParamDict)
  181. if iRetval = 0 then
  182. select case iAction
  183. case kActionAdd
  184. iRetval = CreateOrSetPort(oParamDict)
  185. case kActionDelete
  186. iRetval = DelPort(oParamDict)
  187. case kActionList
  188. iRetval = ListPorts(oParamDict)
  189. case kActionGet
  190. iRetVal = GetPort(oParamDict)
  191. case kActionSet
  192. iRetVal = CreateOrSetPort(oParamDict)
  193. case else
  194. Usage(true)
  195. exit sub
  196. end select
  197. end if
  198. end sub
  199. '
  200. ' Delete a port
  201. '
  202. function DelPort(oParamDict)
  203. on error resume next
  204. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  205. DebugPrint kDebugTrace, L_Text_Msg_Port01_Text & L_Space_Text & oParamDict(kServerName)
  206. DebugPrint kDebugTrace, L_Text_Msg_Port02_Text & L_Space_Text & oParamDict(kPortName)
  207. dim oService
  208. dim oPort
  209. dim iResult
  210. dim strServer
  211. dim strPort
  212. dim strUser
  213. dim strPassword
  214. iResult = kErrorFailure
  215. strServer = oParamDict(kServerName)
  216. strPort = oParamDict(kPortName)
  217. strUser = oParamDict(kUserName)
  218. strPassword = oParamDict(kPassword)
  219. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  220. set oPort = oService.Get("Win32_TCPIPPrinterPort='" & strPort & "'")
  221. else
  222. DelPort = kErrorFailure
  223. exit function
  224. end if
  225. '
  226. ' Check if Get succeeded
  227. '
  228. if Err.Number = kErrorSuccess then
  229. '
  230. ' Try deleting the instance
  231. '
  232. oPort.Delete_
  233. if Err.Number = kErrorSuccess then
  234. wscript.echo L_Text_Msg_General08_Text & L_Space_Text & strPort
  235. else
  236. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  237. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  238. '
  239. ' Try getting extended error information
  240. '
  241. call LastError()
  242. end if
  243. else
  244. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  245. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  246. '
  247. ' Try getting extended error information
  248. '
  249. call LastError()
  250. end if
  251. DelPort = iResult
  252. end function
  253. '
  254. ' Add or update a port
  255. '
  256. function CreateOrSetPort(oParamDict)
  257. on error resume next
  258. dim oPort
  259. dim oService
  260. dim iResult
  261. dim PortType
  262. dim strServer
  263. dim strPort
  264. dim strUser
  265. dim strPassword
  266. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text
  267. DebugPrint kDebugTrace, L_Text_Msg_Port01_Text & L_Space_Text & oParamDict.Item(kServerName)
  268. DebugPrint kDebugTrace, L_Text_Msg_Port02_Text & L_Space_Text & oParamDict.Item(kPortName)
  269. DebugPrint kDebugTrace, L_Text_Msg_Port06_Text & L_Space_Text & oParamDict.Item(kPortNumber)
  270. DebugPrint kDebugTrace, L_Text_Msg_Port07_Text & L_Space_Text & oParamDict.Item(kQueueName)
  271. DebugPrint kDebugTrace, L_Text_Msg_Port13_Text & L_Space_Text & oParamDict.Item(kSNMPDeviceIndex)
  272. DebugPrint kDebugTrace, L_Text_Msg_Port12_Text & L_Space_Text & oParamDict.Item(kCommunityName)
  273. DebugPrint kDebugTrace, L_Text_Msg_Port03_Text & L_Space_Text & oParamDict.Item(kHostAddress)
  274. strServer = oParamDict(kServerName)
  275. strPort = oParamDict(kPortName)
  276. strUser = oParamDict(kUserName)
  277. strPassword = oParamDict(kPassword)
  278. '
  279. ' If the port exists, then get the settings. Later PutInstance will do an update
  280. '
  281. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  282. set oPort = oService.Get("Win32_TCPIPPrinterPort.Name='" & strPort & "'")
  283. '
  284. ' If get was unsuccessful then spwan a new port instance. Later PutInstance will do a create
  285. '
  286. if Err.Number <> kErrorSuccess then
  287. '
  288. ' Clear the previous error
  289. '
  290. Err.Clear
  291. set oPort = oService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
  292. end if
  293. else
  294. CreateOrSetPort = kErrorFailure
  295. exit function
  296. end if
  297. if Err.Number <> kErrorSuccess then
  298. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  299. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  300. CreateOrSetPort = kErrorFailure
  301. exit function
  302. end if
  303. oPort.Name = oParamDict.Item(kPortName)
  304. PortType = oParamDict.Item(kPortType)
  305. '
  306. ' Update the port object with the settings corresponding
  307. ' to the port type of the port to be added
  308. '
  309. select case lcase(PortType)
  310. case "raw"
  311. oPort.Protocol = 1
  312. case "lpr"
  313. oPort.Protocol = 2
  314. case else
  315. '
  316. ' PutInstance will attempt to get the configuration of
  317. ' the device based on its IP address. Those settings
  318. ' will be used to add a new port
  319. '
  320. end select
  321. oPort.HostAddress = oParamDict.Item(kHostAddress)
  322. oPort.PortNumber = oParamDict.Item(kPortNumber)
  323. oPort.SNMPEnabled = oParamDict.Item(kSNMP)
  324. oPort.SNMPDevIndex = oParamDict.Item(kSNMPDeviceIndex)
  325. oPort.SNMPCommunity = oParamDict.Item(kCommunityName)
  326. oPort.Queue = oParamDict.Item(kQueueName)
  327. oPort.ByteCount = oParamDict.Item(kDoubleSpool)
  328. '
  329. ' Try creating or updating the port
  330. '
  331. oPort.Put_(kFlagCreateOrUpdate)
  332. if Err.Number = kErrorSuccess then
  333. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & oPort.Name
  334. iResult = kErrorSuccess
  335. else
  336. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & oPort.Name & 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. iResult = kErrorFailure
  344. end if
  345. CreateOrSetPort = iResult
  346. end function
  347. '
  348. ' List ports on a machine.
  349. '
  350. function ListPorts(oParamDict)
  351. on error resume next
  352. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
  353. dim Ports
  354. dim oPort
  355. dim oService
  356. dim iRetval
  357. dim iTotal
  358. dim strServer
  359. dim strUser
  360. dim strPassword
  361. iResult = kErrorFailure
  362. strServer = oParamDict(kServerName)
  363. strUser = oParamDict(kUserName)
  364. strPassword = oParamDict(kPassword)
  365. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  366. set Ports = oService.InstancesOf("Win32_TCPIPPrinterPort")
  367. else
  368. ListPorts = kErrorFailure
  369. exit function
  370. end if
  371. if Err.Number <> kErrorSuccess then
  372. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  373. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  374. ListPrinters = kErrorFailure
  375. exit function
  376. end if
  377. iTotal = 0
  378. for each oPort in Ports
  379. iTotal = iTotal + 1
  380. wscript.echo L_Empty_Text
  381. wscript.echo L_Text_Msg_Port01_Text & L_Space_Text & strServer
  382. wscript.echo L_Text_Msg_Port02_Text & L_Space_Text & oPort.Name
  383. wscript.echo L_Text_Msg_Port03_Text & L_Space_Text & oPort.HostAddress
  384. if oPort.Protocol = 1 then
  385. wscript.echo L_Text_Msg_Port04_Text
  386. wscript.echo L_Text_Msg_Port06_Text & L_Space_Text & oPort.PortNumber
  387. else
  388. wscript.echo L_Text_Msg_Port05_Text
  389. wscript.echo L_Text_Msg_Port07_Text & L_Space_Text & oPort.Queue
  390. if oPort.ByteCount then
  391. wscript.echo L_Text_Msg_Port08_Text
  392. else
  393. wscript.echo L_Text_Msg_Port08_Text
  394. end if
  395. end if
  396. if oPort.SNMPEnabled then
  397. wscript.echo L_Text_Msg_Port10_Text
  398. wscript.echo L_Text_Msg_Port12_Text & L_Space_Text & oPort.SNMPCommunity
  399. wscript.echo L_Text_Msg_Port13_Text & L_Space_Text & oPort.SNMPDevIndex
  400. else
  401. wscript.echo L_Text_Msg_Port11_Text
  402. end if
  403. Err.Clear
  404. next
  405. wscript.echo L_Empty_Text
  406. wscript.echo L_Text_Msg_General07_Text & L_Space_Text & iTotal
  407. ListPorts = kErrorSuccess
  408. end function
  409. '
  410. ' Gets the configuration of a port
  411. '
  412. function GetPort(oParamDict)
  413. on error resume next
  414. DebugPrint kDebugTrace, L_Text_Dbg_Msg04_Text
  415. DebugPrint kDebugTrace, L_Text_Msg_Port01_Text & L_Space_Text & oParamDict(kServerName)
  416. DebugPrint kDebugTrace, L_Text_Msg_Port02_Text & L_Space_Text & oParamDict(kPortName)
  417. dim oService
  418. dim oPort
  419. dim iResult
  420. dim strServer
  421. dim strPort
  422. dim strUser
  423. dim strPassword
  424. iResult = kErrorFailure
  425. strServer = oParamDict(kServerName)
  426. strPort = oParamDict(kPortName)
  427. strUser = oParamDict(kUserName)
  428. strPassword = oParamDict(kPassword)
  429. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  430. set oPort = oService.Get("Win32_TCPIPPrinterPort.Name='" & strPort & "'")
  431. else
  432. GetPort = kErrorFailure
  433. exit function
  434. end if
  435. if Err.Number = kErrorSuccess then
  436. wscript.echo L_Empty_Text
  437. wscript.echo L_Text_Msg_Port01_Text & L_Space_Text & strServer
  438. wscript.echo L_Text_Msg_Port02_Text & L_Space_Text & oPort.Name
  439. wscript.echo L_Text_Msg_Port03_Text & L_Space_Text & oPort.HostAddress
  440. if oPort.Protocol = 1 then
  441. wscript.echo L_Text_Msg_Port04_Text
  442. wscript.echo L_Text_Msg_Port06_Text & L_Space_Text & oPort.PortNumber
  443. else
  444. wscript.echo L_Text_Msg_Port05_Text
  445. wscript.echo L_Text_Msg_Port07_Text & L_Space_Text & oPort.Queue
  446. if oPort.ByteCount then
  447. wscript.echo L_Text_Msg_Port08_Text
  448. else
  449. wscript.echo L_Text_Msg_Port09_Text
  450. end if
  451. end if
  452. if oPort.SNMPEnabled then
  453. wscript.echo L_Text_Msg_Port10_Text
  454. wscript.echo L_Text_Msg_Port12_Text & L_Space_Text & oPort.SNMPCommunity
  455. wscript.echo L_Text_Msg_Port13_Text & L_Space_Text & oPort.SNMPDevIndex
  456. else
  457. wscript.echo L_Text_Msg_Port11_Text
  458. end if
  459. iResult = kErrorSuccess
  460. else
  461. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  462. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  463. '
  464. ' Try getting extended error information
  465. '
  466. call LastError()
  467. end if
  468. GetPort = iResult
  469. end function
  470. '
  471. ' Debug display helper function
  472. '
  473. sub DebugPrint(uFlags, strString)
  474. if gDebugFlag = true then
  475. if uFlags = kDebugTrace then
  476. wscript.echo L_Debug_Text & L_Space_Text & strString
  477. end if
  478. if uFlags = kDebugError then
  479. if Err <> 0 then
  480. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  481. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  482. & L_Space_Text & Err.Description
  483. end if
  484. end if
  485. end if
  486. end sub
  487. '
  488. ' Parse the command line into its components
  489. '
  490. function ParseCommandLine(iAction, oParamDict)
  491. on error resume next
  492. DebugPrint kDebugTrace, L_Text_Dbg_Msg05_Text
  493. dim oArgs
  494. dim iIndex
  495. iAction = kActionUnknown
  496. set oArgs = Wscript.Arguments
  497. while iIndex < oArgs.Count
  498. select case oArgs(iIndex)
  499. case "-g"
  500. iAction = kActionGet
  501. case "-t"
  502. iAction = kActionSet
  503. case "-a"
  504. iAction = kActionAdd
  505. case "-d"
  506. iAction = kActionDelete
  507. case "-l"
  508. iAction = kActionList
  509. case "-2e"
  510. oParamDict.Add kDoubleSpool, true
  511. case "-2d"
  512. oParamDict.Add kDoubleSpool, false
  513. case "-s"
  514. iIndex = iIndex + 1
  515. oParamDict.Add kServerName, RemoveBackslashes(oArgs(iIndex))
  516. case "-u"
  517. iIndex = iIndex + 1
  518. oParamDict.Add kUserName, oArgs(iIndex)
  519. case "-w"
  520. iIndex = iIndex + 1
  521. oParamDict.Add kPassword, oArgs(iIndex)
  522. case "-n"
  523. iIndex = iIndex + 1
  524. oParamDict.Add kPortNumber, oArgs(iIndex)
  525. case "-r"
  526. iIndex = iIndex + 1
  527. oParamDict.Add kPortName, oArgs(iIndex)
  528. case "-o"
  529. iIndex = iIndex + 1
  530. oParamDict.Add kPortType, oArgs(iIndex)
  531. case "-h"
  532. iIndex = iIndex + 1
  533. oParamDict.Add kHostAddress, oArgs(iIndex)
  534. case "-q"
  535. iIndex = iIndex + 1
  536. oParamDict.Add kQueueName, oArgs(iIndex)
  537. case "-i"
  538. iIndex = iIndex + 1
  539. oParamDict.Add kSNMPDeviceIndex, oArgs(iIndex)
  540. case "-y"
  541. iIndex = iIndex + 1
  542. oParamDict.Add kCommunityName, oArgs(iIndex)
  543. case "-me"
  544. oParamDict.Add kSNMP, true
  545. case "-md"
  546. oParamDict.Add kSNMP, false
  547. case "-?"
  548. Usage(True)
  549. exit function
  550. case else
  551. Usage(True)
  552. exit function
  553. end select
  554. iIndex = iIndex + 1
  555. wend
  556. if Err = kErrorSuccess then
  557. ParseCommandLine = kErrorSuccess
  558. else
  559. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  560. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  561. ParseCommandLine = kErrorFailure
  562. end if
  563. end function
  564. '
  565. ' Display command usage.
  566. '
  567. sub Usage(bExit)
  568. wscript.echo L_Help_Help_General01_Text
  569. wscript.echo L_Help_Help_General02_Text
  570. wscript.echo L_Help_Help_General03_Text
  571. wscript.echo L_Help_Help_General04_Text
  572. wscript.echo L_Help_Help_General05_Text
  573. wscript.echo L_Help_Help_General06_Text
  574. wscript.echo L_Help_Help_General07_Text
  575. wscript.echo L_Help_Help_General08_Text
  576. wscript.echo L_Help_Help_General09_Text
  577. wscript.echo L_Help_Help_General10_Text
  578. wscript.echo L_Help_Help_General11_Text
  579. wscript.echo L_Help_Help_General12_Text
  580. wscript.echo L_Help_Help_General13_Text
  581. wscript.echo L_Help_Help_General14_Text
  582. wscript.echo L_Help_Help_General15_Text
  583. wscript.echo L_Help_Help_General16_Text
  584. wscript.echo L_Help_Help_General17_Text
  585. wscript.echo L_Help_Help_General18_Text
  586. wscript.echo L_Help_Help_General19_Text
  587. wscript.echo L_Help_Help_General20_Text
  588. wscript.echo L_Help_Help_General21_Text
  589. wscript.echo L_Help_Help_General22_Text
  590. wscript.echo L_Empty_Text
  591. wscript.echo L_Help_Help_General23_Text
  592. wscript.echo L_Help_Help_General24_Text
  593. wscript.echo L_Help_Help_General25_Text
  594. wscript.echo L_Help_Help_General26_Text
  595. wscript.echo L_Help_Help_General27_Text
  596. wscript.echo L_Help_Help_General28_Text
  597. wscript.echo L_Help_Help_General29_Text
  598. wscript.echo L_Empty_Text
  599. wscript.echo L_Help_Help_General30_Text
  600. wscript.echo L_Help_Help_General31_Text
  601. wscript.echo L_Help_Help_General32_Text
  602. if bExit then
  603. wscript.quit(1)
  604. end if
  605. end sub
  606. '
  607. ' Determines which program is being used to run this script.
  608. ' Returns true if the script host is cscript.exe
  609. '
  610. function IsHostCscript()
  611. on error resume next
  612. dim strFullName
  613. dim strCommand
  614. dim i, j
  615. dim bReturn
  616. bReturn = false
  617. strFullName = WScript.FullName
  618. i = InStr(1, strFullName, ".exe", 1)
  619. if i <> 0 then
  620. j = InStrRev(strFullName, "\", i, 1)
  621. if j <> 0 then
  622. strCommand = Mid(strFullName, j+1, i-j-1)
  623. if LCase(strCommand) = "cscript" then
  624. bReturn = true
  625. end if
  626. end if
  627. end if
  628. if Err <> 0 then
  629. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  630. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  631. end if
  632. IsHostCscript = bReturn
  633. end function
  634. '
  635. ' Retrieves extended information about the last error that occured
  636. ' during a WBEM operation. The methods that set an SWbemLastError
  637. ' object are GetObject, PutInstance, DeleteInstance
  638. '
  639. sub LastError()
  640. on error resume next
  641. dim oError
  642. set oError = CreateObject("WbemScripting.SWbemLastError")
  643. if Err = kErrorSuccess then
  644. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  645. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  646. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  647. wscript.echo L_Text_Error_General04_Text & L_Space_Text & oError.StatusCode
  648. end if
  649. end sub
  650. '
  651. ' Connects to the WMI service on a server. oService is returned as a service
  652. ' object (SWbemServices)
  653. '
  654. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  655. on error resume next
  656. dim oLocator
  657. dim bResult
  658. oService = null
  659. bResult = false
  660. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  661. if Err = kErrorSuccess then
  662. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  663. if Err = kErrorSuccess then
  664. bResult = true
  665. oService.Security_.impersonationlevel = 3
  666. '
  667. ' Required to perform administrative tasks on the spooler service
  668. '
  669. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  670. Err.Clear
  671. else
  672. wscript.echo L_Text_Msg_General10_Text & L_Space_Text & L_Error_Text _
  673. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  674. & Err.Description
  675. end if
  676. else
  677. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & L_Error_Text _
  678. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  679. & Err.Description
  680. end if
  681. WmiConnect = bResult
  682. end function
  683. '
  684. ' Remove leading "\\" from server name
  685. '
  686. function RemoveBackslashes(strServer)
  687. dim strRet
  688. strRet = strServer
  689. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  690. strRet = Mid(strServer, 3)
  691. end if
  692. RemoveBackslashes = strRet
  693. end function