Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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