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.

873 lines
24 KiB

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. ' Abstract:
  6. ' prndrvr.vbs - driver script for WMI on Whistler
  7. ' used to add, delete, and list drivers.
  8. '
  9. ' Usage:
  10. ' prndrvr [-adxl?] [-m model][-v version][-e environment][-s server]
  11. ' [-u user name][-w password][-h file path][-i inf file]
  12. '
  13. ' Example:
  14. ' prndrvr -a -m "driver" -v 3 -e "Windows NT x86"
  15. ' prndrvr -d -m "driver" -v 2 -e "Windows NT x86"
  16. ' prndrvr -x -s server
  17. ' prndrvr -l -s server
  18. '
  19. '----------------------------------------------------------------------
  20. option explicit
  21. '
  22. ' Debugging trace flags, to enable debug output trace message
  23. ' change gDebugFlag to true.
  24. '
  25. const kDebugTrace = 1
  26. const kDebugError = 2
  27. dim gDebugFlag
  28. gDebugFlag = false
  29. '
  30. ' Operation action values.
  31. '
  32. const kActionUnknown = 0
  33. const kActionAdd = 1
  34. const kActionDel = 2
  35. const kActionDelAll = 3
  36. const kActionList = 4
  37. const kErrorSuccess = 0
  38. const kErrorFailure = 1
  39. const kNameSpace = "root\cimv2"
  40. '
  41. ' Generic strings
  42. '
  43. const L_Empty_Text = ""
  44. const L_Space_Text = " "
  45. const L_Error_Text = "Error"
  46. const L_Success_Text = "Success"
  47. const L_Failed_Text = "Failed"
  48. const L_Hex_Text = "0x"
  49. const L_Printer_Text = "Printer"
  50. const L_Operation_Text = "Operation"
  51. const L_Provider_Text = "Provider"
  52. const L_Description_Text = "Description"
  53. const L_Debug_Text = "Debug:"
  54. '
  55. ' General usage messages
  56. '
  57. const L_Help_Help_General01_Text = "Usage: prndrvr [-adlx?] [-m model][-v version][-e environment][-s server]"
  58. const L_Help_Help_General02_Text = " [-u user name][-w password][-h path][-i inf file]"
  59. const L_Help_Help_General03_Text = "Arguments:"
  60. const L_Help_Help_General04_Text = "-a - add the specified driver"
  61. const L_Help_Help_General05_Text = "-d - delete the specified driver"
  62. const L_Help_Help_General06_Text = "-e - environment"
  63. const L_Help_Help_General07_Text = "-h - driver file path"
  64. const L_Help_Help_General08_Text = "-i - inf file name"
  65. const L_Help_Help_General09_Text = "-l - list all drivers"
  66. const L_Help_Help_General10_Text = "-m - driver model name"
  67. const L_Help_Help_General11_Text = "-s - server name"
  68. const L_Help_Help_General12_Text = "-u - user name"
  69. const L_Help_Help_General13_Text = "-v - version"
  70. const L_Help_Help_General14_Text = "-w - password"
  71. const L_Help_Help_General15_Text = "-x - delete all drivers that are not in use"
  72. const L_Help_Help_General16_Text = "-? - display command usage"
  73. const L_Help_Help_General17_Text = "Examples:"
  74. const L_Help_Help_General18_Text = "prndrvr -a -m ""driver"" -v 2 -e ""Windows NT x86"""
  75. const L_Help_Help_General19_Text = "prndrvr -d -m ""driver"" -v 3 -e ""Windows NT x86"""
  76. const L_Help_Help_General20_Text = "prndrvr -l -s server"
  77. const L_Help_Help_General21_Text = "prndrvr -x -s server"
  78. '
  79. ' Messages to be displayed if the scripting host is not cscript
  80. '
  81. const L_Help_Help_Host01_Text = "Please run this script using CScript."
  82. const L_Help_Help_Host02_Text = "This can be achieved by"
  83. const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
  84. const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
  85. const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
  86. const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
  87. '
  88. ' General error messages
  89. '
  90. const L_Text_Error_General01_Text = "The scripting host could not be determined."
  91. const L_Text_Error_General02_Text = "Unable to parse command line."
  92. const L_Text_Error_General03_Text = "Win32 error code"
  93. '
  94. ' Miscellaneous messages
  95. '
  96. const L_Text_Msg_General01_Text = "Added printer driver"
  97. const L_Text_Msg_General02_Text = "Unable to add printer driver"
  98. const L_Text_Msg_General03_Text = "Unable to delete printer driver"
  99. const L_Text_Msg_General04_Text = "Deleted printer driver"
  100. const L_Text_Msg_General05_Text = "Unable to enumerate printer drivers"
  101. const L_Text_Msg_General06_Text = "Number of printer drivers enumerated"
  102. const L_Text_Msg_General07_Text = "Number of printer drivers deleted"
  103. const L_Text_Msg_General08_Text = "Attempting to delete printer driver"
  104. const L_Text_Msg_General09_Text = "Unable to list dependent files"
  105. const L_Text_Msg_General10_Text = "Unable to get SWbemLocator object"
  106. const L_Text_Msg_General11_Text = "Unable to connect to WMI service"
  107. '
  108. ' Printer driver properties
  109. '
  110. const L_Text_Msg_Driver01_Text = "Server name"
  111. const L_Text_Msg_Driver02_Text = "Driver name"
  112. const L_Text_Msg_Driver03_Text = "Version"
  113. const L_Text_Msg_Driver04_Text = "Environment"
  114. const L_Text_Msg_Driver05_Text = "Monitor name"
  115. const L_Text_Msg_Driver06_Text = "Driver path"
  116. const L_Text_Msg_Driver07_Text = "Data file"
  117. const L_Text_Msg_Driver08_Text = "Config file"
  118. const L_Text_Msg_Driver09_Text = "Help file"
  119. const L_Text_Msg_Driver10_Text = "Dependent files"
  120. '
  121. ' Debug messages
  122. '
  123. const L_Text_Dbg_Msg01_Text = "In function AddDriver"
  124. const L_Text_Dbg_Msg02_Text = "In function DelDriver"
  125. const L_Text_Dbg_Msg03_Text = "In function DelAllDrivers"
  126. const L_Text_Dbg_Msg04_Text = "In function ListDrivers"
  127. const L_Text_Dbg_Msg05_Text = "In function ParseCommandLine"
  128. main
  129. '
  130. ' Main execution starts here
  131. '
  132. sub main
  133. dim iAction
  134. dim iRetval
  135. dim strServer
  136. dim strModel
  137. dim strPath
  138. dim uVersion
  139. dim strEnvironment
  140. dim strInfFile
  141. dim strUser
  142. dim strPassword
  143. '
  144. ' Abort if the host is not cscript
  145. '
  146. if not IsHostCscript() then
  147. call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
  148. L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
  149. L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
  150. wscript.quit
  151. end if
  152. '
  153. ' Get command line parameters
  154. '
  155. iRetval = ParseCommandLine(iAction, strServer, strModel, strPath, uVersion, _
  156. strEnvironment, strInfFile, strUser, strPAssword)
  157. if iRetval = kErrorSuccess then
  158. select case iAction
  159. case kActionAdd
  160. iRetval = AddDriver(strServer, strModel, strPath, uVersion, _
  161. strEnvironment, strInfFile, strUser, strPassword)
  162. case kActionDel
  163. iRetval = DelDriver(strServer, strModel, uVersion, strEnvironment, strUser, strPassword)
  164. case kActionDelAll
  165. iRetval = DelAllDrivers(strServer, strUser, strPassword)
  166. case kActionList
  167. iRetval = ListDrivers(strServer, strUser, strPassword)
  168. case kActionUnknown
  169. Usage(true)
  170. exit sub
  171. case else
  172. Usage(true)
  173. exit sub
  174. end select
  175. end if
  176. end sub
  177. '
  178. ' Add a driver
  179. '
  180. function AddDriver(strServer, strModel, strFilePath, uVersion, strEnvironment, strInfFile, strUser, strPassword)
  181. on error resume next
  182. DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
  183. dim oDriver
  184. dim oService
  185. dim iResult
  186. dim uResult
  187. '
  188. ' Initialize return value
  189. '
  190. iResult = kErrorFailure
  191. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  192. set oDriver = oService.Get("Win32_PrinterDriver")
  193. else
  194. AddDriver = kErrorFailure
  195. exit function
  196. end if
  197. '
  198. ' Check if Get was successful
  199. '
  200. if Err.Number = kErrorSuccess then
  201. oDriver.Name = strModel
  202. oDriver.SupportedPlatform = strEnvironment
  203. oDriver.Version = uVersion
  204. oDriver.FilePath = strFilePath
  205. oDriver.InfName = strInfFile
  206. uResult = oDriver.AddPrinterDriver(oDriver)
  207. if Err.Number = kErrorSuccess then
  208. if uResult = kErrorSuccess then
  209. wscript.echo L_Text_Msg_General01_Text & L_Space_Text & oDriver.Name
  210. iResult = kErrorSuccess
  211. else
  212. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & strModel & L_Space_Text _
  213. & L_Text_Error_General03_Text & L_Space_Text & uResult
  214. end if
  215. else
  216. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & strModel & L_Space_Text _
  217. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  218. end if
  219. else
  220. wscript.echo L_Text_Msg_General02_Text & L_Space_Text & strModel & L_Space_Text _
  221. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  222. end if
  223. AddDriver = iResult
  224. end function
  225. '
  226. ' Delete a driver
  227. '
  228. function DelDriver(strServer, strModel, uVersion, strEnvironment, strUser, strPassword)
  229. on error resume next
  230. DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text
  231. dim oDriver
  232. dim oService
  233. dim iResult
  234. dim strObject
  235. '
  236. ' Initialize return value
  237. '
  238. iResult = kErrorFailure
  239. '
  240. ' Build the key that identifies the driver instance.
  241. '
  242. strObject = strModel & "," & CStr(uVersion) & "," & strEnvironment
  243. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  244. set oDriver = oService.Get("Win32_PrinterDriver.Name='" & strObject & "'")
  245. else
  246. DelDriver = kErrorFailure
  247. exit function
  248. end if
  249. '
  250. ' Check if Get was successful
  251. '
  252. if Err.Number = kErrorSuccess then
  253. '
  254. ' Delete the printer driver instance
  255. '
  256. oDriver.Delete_
  257. if Err.Number = kErrorSuccess then
  258. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & oDriver.Name
  259. iResult = kErrorSuccess
  260. else
  261. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & strModel & L_Space_Text _
  262. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  263. & L_Space_Text & Err.Description
  264. call LastError()
  265. end if
  266. else
  267. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & strModel & L_Space_Text _
  268. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  269. & L_Space_Text & Err.Description
  270. end if
  271. DelDriver = iResult
  272. end function
  273. '
  274. ' Delete all drivers
  275. '
  276. function DelAllDrivers(strServer, strUser, strPassword)
  277. on error resume next
  278. DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
  279. dim Drivers
  280. dim oDriver
  281. dim oService
  282. dim iResult
  283. dim iTotal
  284. dim iTotalDeleted
  285. dim vntDependentFiles
  286. dim strDriverName
  287. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  288. set Drivers = oService.InstancesOf("Win32_PrinterDriver")
  289. else
  290. DelAllDrivers = kErrorFailure
  291. exit function
  292. end if
  293. if Err.Number <> kErrorSuccess then
  294. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  295. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  296. DelAllDrivers = kErrorFailure
  297. exit function
  298. end if
  299. iTotal = 0
  300. iTotalDeleted = 0
  301. for each oDriver in Drivers
  302. iTotal = iTotal + 1
  303. wscript.echo
  304. wscript.echo L_Text_Msg_General08_Text
  305. wscript.echo L_Text_Msg_Driver01_Text & L_Space_Text & strServer
  306. wscript.echo L_Text_Msg_Driver02_Text & L_Space_Text & oDriver.Name
  307. wscript.echo L_Text_Msg_Driver03_Text & L_Space_Text & oDriver.Version
  308. wscript.echo L_Text_Msg_Driver04_Text & L_Space_Text & oDriver.SupportedPlatform
  309. strDriverName = oDriver.Name
  310. '
  311. ' Example of how to delete an instance of a printer driver
  312. '
  313. oDriver.Delete_
  314. if Err.Number = kErrorSuccess then
  315. wscript.echo L_Text_Msg_General04_Text & L_Space_Text & oDriver.Name
  316. iTotalDeleted = iTotalDeleted + 1
  317. else
  318. '
  319. ' We cannot use oDriver.Name to display the driver name, because the SWbemLastError
  320. ' that the function LastError() looks at would be overwritten. For that reason we
  321. ' use strDriverName for accessing the driver name
  322. '
  323. wscript.echo L_Text_Msg_General03_Text & L_Space_Text & strDriverName & L_Space_Text _
  324. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  325. & L_Space_Text & Err.Description
  326. '
  327. ' Try getting extended error information
  328. '
  329. call LastError()
  330. Err.Clear
  331. end if
  332. next
  333. wscript.echo L_Empty_Text
  334. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & iTotal
  335. wscript.echo L_Text_Msg_General07_Text & L_Space_Text & iTotalDeleted
  336. DelAllDrivers = kErrorSuccess
  337. end function
  338. '
  339. ' List drivers
  340. '
  341. function ListDrivers(strServer, strUser, strPassword)
  342. on error resume next
  343. DebugPrint kDebugTrace, L_Text_Dbg_Msg04_Text
  344. dim Drivers
  345. dim oDriver
  346. dim oService
  347. dim iResult
  348. dim iTotal
  349. dim vntDependentFiles
  350. if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
  351. set Drivers = oService.InstancesOf("Win32_PrinterDriver")
  352. else
  353. ListDrivers = kErrorFailure
  354. exit function
  355. end if
  356. if Err.Number <> kErrorSuccess then
  357. wscript.echo L_Text_Msg_General05_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  358. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  359. ListDrivers = kErrorFailure
  360. exit function
  361. end if
  362. iTotal = 0
  363. for each oDriver in Drivers
  364. iTotal = iTotal + 1
  365. wscript.echo
  366. wscript.echo L_Text_Msg_Driver01_Text & L_Space_Text & strServer
  367. wscript.echo L_Text_Msg_Driver02_Text & L_Space_Text & oDriver.Name
  368. wscript.echo L_Text_Msg_Driver03_Text & L_Space_Text & oDriver.Version
  369. wscript.echo L_Text_Msg_Driver04_Text & L_Space_Text & oDriver.SupportedPlatform
  370. wscript.echo L_Text_Msg_Driver05_Text & L_Space_Text & oDriver.MonitorName
  371. wscript.echo L_Text_Msg_Driver06_Text & L_Space_Text & oDriver.DriverPath
  372. wscript.echo L_Text_Msg_Driver07_Text & L_Space_Text & oDriver.DataFile
  373. wscript.echo L_Text_Msg_Driver08_Text & L_Space_Text & oDriver.ConfigFile
  374. wscript.echo L_Text_Msg_Driver09_Text & L_Space_Text & oDriver.HelpFile
  375. vntDependentFiles = oDriver.DependentFiles
  376. '
  377. ' If there are no dependent files, the method will set DependentFiles to
  378. ' an empty variant, so we check if the variant is an array of variants
  379. '
  380. if VarType(vntDependentFiles) = (vbArray + vbVariant) then
  381. PrintDepFiles oDriver.DependentFiles
  382. end if
  383. Err.Clear
  384. next
  385. wscript.echo L_Empty_Text
  386. wscript.echo L_Text_Msg_General06_Text & L_Space_Text & iTotal
  387. ListDrivers = kErrorSuccess
  388. end function
  389. '
  390. ' Prints the contents of an array of variants
  391. '
  392. sub PrintDepFiles(Param)
  393. on error resume next
  394. dim iIndex
  395. iIndex = LBound(Param)
  396. if Err.Number = 0 then
  397. wscript.echo L_Text_Msg_Driver10_Text
  398. for iIndex = LBound(Param) to UBound(Param)
  399. wscript.echo L_Space_Text & Param(iIndex)
  400. next
  401. else
  402. wscript.echo L_Text_Msg_General09_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  403. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  404. end if
  405. end sub
  406. '
  407. ' Debug display helper function
  408. '
  409. sub DebugPrint(uFlags, strString)
  410. if gDebugFlag = true then
  411. if uFlags = kDebugTrace then
  412. wscript.echo L_Debug_Text & L_Space_Text & strString
  413. end if
  414. if uFlags = kDebugError then
  415. if Err <> 0 then
  416. wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
  417. & L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
  418. & L_Space_Text & Err.Description
  419. end if
  420. end if
  421. end if
  422. end sub
  423. '
  424. ' Parse the command line into its components
  425. '
  426. function ParseCommandLine(iAction, strServer, strModel, strPath, uVersion, _
  427. strEnvironment, strInfFile, strUser, strPassword)
  428. on error resume next
  429. DebugPrint kDebugTrace, L_Text_Dbg_Msg05_Text
  430. dim oArgs
  431. dim iIndex
  432. iAction = kActionUnknown
  433. iIndex = 0
  434. set oArgs = wscript.Arguments
  435. while iIndex < oArgs.Count
  436. select case oArgs(iIndex)
  437. case "-a"
  438. iAction = kActionAdd
  439. case "-d"
  440. iAction = kActionDel
  441. case "-l"
  442. iAction = kActionList
  443. case "-x"
  444. iAction = kActionDelAll
  445. case "-s"
  446. iIndex = iIndex + 1
  447. strServer = RemoveBackslashes(oArgs(iIndex))
  448. case "-m"
  449. iIndex = iIndex + 1
  450. strModel = oArgs(iIndex)
  451. case "-h"
  452. iIndex = iIndex + 1
  453. strPath = oArgs(iIndex)
  454. case "-v"
  455. iIndex = iIndex + 1
  456. uVersion = oArgs(iIndex)
  457. case "-e"
  458. iIndex = iIndex + 1
  459. strEnvironment = oArgs(iIndex)
  460. case "-i"
  461. iIndex = iIndex + 1
  462. strInfFile = oArgs(iIndex)
  463. case "-u"
  464. iIndex = iIndex + 1
  465. strUser = oArgs(iIndex)
  466. case "-w"
  467. iIndex = iIndex + 1
  468. strPassword = oArgs(iIndex)
  469. case "-?"
  470. Usage(true)
  471. exit function
  472. case else
  473. Usage(true)
  474. exit function
  475. end select
  476. iIndex = iIndex + 1
  477. wend
  478. if Err.Number <> 0 then
  479. wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  480. & L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
  481. ParseCommandLine = kErrorFailure
  482. else
  483. ParseCommandLine = kErrorSuccess
  484. end if
  485. end function
  486. '
  487. ' Display command usage.
  488. '
  489. sub Usage(bExit)
  490. wscript.echo L_Help_Help_General01_Text
  491. wscript.echo L_Help_Help_General02_Text
  492. wscript.echo L_Help_Help_General03_Text
  493. wscript.echo L_Help_Help_General04_Text
  494. wscript.echo L_Help_Help_General05_Text
  495. wscript.echo L_Help_Help_General06_Text
  496. wscript.echo L_Help_Help_General07_Text
  497. wscript.echo L_Help_Help_General08_Text
  498. wscript.echo L_Help_Help_General09_Text
  499. wscript.echo L_Help_Help_General10_Text
  500. wscript.echo L_Help_Help_General11_Text
  501. wscript.echo L_Help_Help_General12_Text
  502. wscript.echo L_Help_Help_General13_Text
  503. wscript.echo L_Help_Help_General14_Text
  504. wscript.echo L_Help_Help_General15_Text
  505. wscript.echo L_Help_Help_General16_Text
  506. wscript.echo L_Empty_Text
  507. wscript.echo L_Help_Help_General17_Text
  508. wscript.echo L_Help_Help_General18_Text
  509. wscript.echo L_Help_Help_General19_Text
  510. wscript.echo L_Help_Help_General20_Text
  511. wscript.echo L_Help_Help_General21_Text
  512. if bExit then
  513. wscript.quit(1)
  514. end if
  515. end sub
  516. '
  517. ' Determines which program is being used to run this script.
  518. ' Returns true if the script host is cscript.exe
  519. '
  520. function IsHostCscript()
  521. on error resume next
  522. dim strFullName
  523. dim strCommand
  524. dim i, j
  525. dim bReturn
  526. bReturn = false
  527. strFullName = WScript.FullName
  528. i = InStr(1, strFullName, ".exe", 1)
  529. if i <> 0 then
  530. j = InStrRev(strFullName, "\", i, 1)
  531. if j <> 0 then
  532. strCommand = Mid(strFullName, j+1, i-j-1)
  533. if LCase(strCommand) = "cscript" then
  534. bReturn = true
  535. end if
  536. end if
  537. end if
  538. if Err <> 0 then
  539. wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
  540. & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
  541. end if
  542. IsHostCscript = bReturn
  543. end function
  544. '
  545. ' Retrieves extended information about the last error that occured
  546. ' during a WBEM operation. The methods that set an SWbemLastError
  547. ' object are GetObject, PutInstance, DeleteInstance
  548. '
  549. sub LastError()
  550. on error resume next
  551. dim oError
  552. set oError = CreateObject("WbemScripting.SWbemLastError")
  553. if Err = kErrorSuccess then
  554. wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
  555. wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
  556. wscript.echo L_Description_Text & L_Space_Text & oError.Description
  557. wscript.echo L_Text_Error_General03_Text & L_Space_Text & oError.StatusCode
  558. end if
  559. end sub
  560. '
  561. ' Connects to the WMI service on a server. oService is returned as a service
  562. ' object (SWbemServices)
  563. '
  564. function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
  565. on error resume next
  566. dim oLocator
  567. dim bResult
  568. oService = null
  569. bResult = false
  570. set oLocator = CreateObject("WbemScripting.SWbemLocator")
  571. if Err = kErrorSuccess then
  572. set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
  573. if Err = kErrorSuccess then
  574. bResult = true
  575. oService.Security_.impersonationlevel = 3
  576. '
  577. ' Required to perform administrative tasks on the spooler service
  578. '
  579. oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
  580. Err.Clear
  581. else
  582. wscript.echo L_Text_Msg_General11_Text & L_Space_Text & L_Error_Text _
  583. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  584. & Err.Description
  585. end if
  586. else
  587. wscript.echo L_Text_Msg_General10_Text & L_Space_Text & L_Error_Text _
  588. & L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
  589. & Err.Description
  590. end if
  591. WmiConnect = bResult
  592. end function
  593. '
  594. ' Remove leading "\\" from server name
  595. '
  596. function RemoveBackslashes(strServer)
  597. dim strRet
  598. strRet = strServer
  599. if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
  600. strRet = Mid(strServer, 3)
  601. end if
  602. RemoveBackslashes = strRet
  603. end function