home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / netmgmt.cab / drvmgr.vbs < prev    next >
Text File  |  1999-11-04  |  17KB  |  664 lines

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation 1998-1999
  4. ' All Rights Reserved
  5. '
  6. ' Abstract:
  7. '
  8. ' drvmgr.vbs - driver script for Windows 2000
  9. '
  10. ' Usage:
  11. ' drvmgr [-adl?] [-m model name] [-v Version] [-p Path]
  12. '                [-c Server] [-t Architecture] [-i InfFile]
  13. '
  14. ' Example:
  15. ' drvmgr -d -m "driver" -v "Windows 2000" -t Intel
  16. ' drvmgr -l -c \\server
  17. '----------------------------------------------------------------------
  18.  
  19. option explicit
  20.  
  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.  
  29. gDebugFlag = false
  30.  
  31. '
  32. ' Messages to be displayed if the scripting host is not cscript
  33. '                            
  34. const kMessage1 = "Please run this script using CScript."  
  35. const kMessage2 = "This can be achieved by"
  36. const kMessage3 = "1. Using ""CScript script.vbs arguments"" or" 
  37. const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
  38. const kMessage5 = "   using ""CScript //H:CScript //S"" and running the script "
  39. const kMessage6 = "   ""script.vbs arguments""."
  40.  
  41. '
  42. ' Operation action values.
  43. '
  44. const kActionUnknown    = 0
  45. const kActionAdd        = 1
  46. const kActionDel        = 2
  47. const kActionDelAll     = 3
  48. const kActionList       = 4
  49.  
  50. const kErrorSuccess     = 0
  51. const kErrorFailure     = 1
  52.  
  53. '
  54. ' Strings identifying environments
  55. '
  56. const kEnvironmentIntel   = "Windows NT x86"
  57. const kEnvironmentMIPS    = "Windows NT R4000"
  58. const kEnvironmentAlpha   = "Windows NT Alpha_AXP"
  59. const kEnvironmentPowerPC = "Windows NT PowerPC"
  60. const kEnvironmentWindows = "Windows 4.0"
  61. const kEnvironmentUnknown = "unknown"
  62.  
  63. '
  64. ' Strings identifying architectures
  65. '
  66. const kArchIntel   = "Intel"
  67. const kArchMIPS    = "MIPS"
  68. const kArchAlpha   = "Alpha"
  69. const kArchPowerPC = "PowerPC"
  70. const kArchUnknown = "Unknown"
  71.  
  72. '
  73. ' Strings identifying driver versions
  74. ' Change these strings on localized builds
  75. '
  76. const kVersionWindows95 = "Windows 95 or 98"
  77. const kVersion_NT31     = "Windows NT 3.1"
  78. const kVersion35x       = "Windows NT 3.5 or 3.51"
  79. const kVersion351       = "Windows NT 3.51"
  80. const kVersion40        = "Windows NT 4.0"
  81. const kVersion4050      = "Windows NT 4.0 or 2000"
  82. const kVersion50        = "Windows 2000"
  83.     
  84. main
  85.  
  86. '
  87. ' Main execution starts here
  88. '
  89. sub main
  90.  
  91.     dim iAction
  92.     dim iRetval
  93.     dim strServer
  94.     dim strModel
  95.     dim strPath
  96.     dim strVersion
  97.     dim strArchitecture
  98.     dim strInfFile
  99.     
  100.     '
  101.     ' Abort if the host is not cscript
  102.     '
  103.     if not IsHostCscript() then
  104.    
  105.         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  106.                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  107.                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  108.         
  109.         wscript.quit
  110.    
  111.     end if
  112.  
  113.     iRetval = ParseCommandLine(iAction, strServer, strModel, strPath, strVersion, _
  114.                                strArchitecture, strInfFile)
  115.  
  116.     if iRetval = kErrorSuccess  then
  117.  
  118.         select case iAction
  119.  
  120.             case kActionAdd
  121.                 iRetval = AddDriver(strServer, strModel, strPath, strVersion, _
  122.                                     strArchitecture, strInfFile)
  123.  
  124.             case kActionDel
  125.                 iRetval = DelDriver(strServer, strModel, strVersion, strArchitecture)
  126.  
  127.             case kActionDelAll
  128.                 iRetval = DelAllDrivers(strServer)
  129.  
  130.             case kActionList
  131.                 iRetval = ListDrivers(strServer)
  132.  
  133.             case kActionUnknown
  134.                 Usage(true)
  135.                 exit sub
  136.  
  137.             case else
  138.                 Usage(true)
  139.                 exit sub
  140.  
  141.         end select
  142.    
  143.     end if
  144.  
  145. end sub
  146.  
  147. '
  148. ' Add a driver
  149. '
  150. function AddDriver(strServer, strModel, strPath, strVersion, strArchitecture, strInfFile)
  151.  
  152.     on error resume next
  153.     
  154.     DebugPrint kDebugTrace, "In AddDriver"
  155.  
  156.     dim oMaster
  157.     dim oDriver
  158.     dim iResult
  159.  
  160.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  161.     set oDriver = CreateObject("Driver.Driver.1")
  162.  
  163.     oDriver.ModelName          = strModel
  164.     oDriver.Path               = strPath
  165.     oDriver.DriverArchitecture = strArchitecture
  166.     oDriver.InfFile            = strInfFile
  167.     oDriver.ServerName         = strServer
  168.     odriver.DriverVersion      = strVersion
  169.  
  170.     oMaster.DriverAdd oDriver
  171.  
  172.     if Err.Number = kErrorSuccess then
  173.  
  174.         wscript.echo "Added driver """ & oDriver.ModelName & """"
  175.  
  176.         iResult = kErrorSuccess
  177.  
  178.     else
  179.  
  180.         wscript.echo "Unable to add driver """ & oDriver.ModelName & """, error: 0x" _
  181.                      & Hex(Err.Number) & ". " & Err.Description
  182.         
  183.         iResult = kErrorFailure
  184.  
  185.     end if
  186.  
  187.     AddDriver = iResult
  188.  
  189. end function
  190.  
  191. '
  192. ' Delete a driver
  193. '
  194. function DelDriver(strServer, strModel, strVersion, strArchitecture)
  195.  
  196.     on error resume next
  197.  
  198.     DebugPrint kDebugTrace, "In DelDriver"
  199.  
  200.     dim oMaster
  201.     dim oDriver
  202.     dim iRetval
  203.  
  204.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  205.     set oDriver = CreateObject("Driver.Driver.1")
  206.  
  207.     oDriver.ModelName          = strModel
  208.     oDriver.DriverArchitecture = strArchitecture
  209.     oDriver.ServerName         = strServer
  210.     odriver.DriverVersion      = strVersion
  211.  
  212.     oMaster.DriverDel oDriver
  213.  
  214.     if Err.Number = kErrorSuccess then
  215.  
  216.         wscript.echo "Deleted driver """ & oDriver.ModelName & """"
  217.  
  218.         iRetval = kErrorSuccess
  219.  
  220.     else
  221.  
  222.         wscript.echo "Unable to delete driver """ & oDriver.ModelName  & """, error: 0x" _ 
  223.                      & Hex(Err.Number) & ". " & Err.Description
  224.         
  225.         iRetval = kErrorFailure
  226.  
  227.     end if
  228.  
  229.     DelDriver = iRetval
  230.  
  231. end function
  232.  
  233. '
  234. ' Delete all drivers
  235. '
  236. function DelAllDrivers(strServer)
  237.  
  238.     on error resume next 
  239.     
  240.     DebugPrint kDebugTrace, "In DelAllDrivers"
  241.  
  242.     dim oMaster
  243.     dim oDriver
  244.     dim iResult
  245.     dim iTotal
  246.     dim iTotalDeleted
  247.  
  248.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  249.     set oDriver = CreateObject("Driver.Driver.1")
  250.     
  251.     iTotal = 0
  252.     iTotalDeleted = 0   
  253.         
  254.     for each oDriver in oMaster.Drivers(strServer)
  255.         
  256.         if Err.Number = kErrorSuccess then
  257.         
  258.             iTotal = iTotal + 1 
  259.    
  260.             wscript.echo
  261.             wscript.echo "Attempting to delete driver: " & oDriver.ModelName 
  262.             wscript.echo "               architecture: " & GetArchitecture(oDriver.Environment) 
  263.             wscript.echo "                    version: " & GetVersion(oDriver.Version, oDriver.Environment)
  264.             wscript.echo "                from server: " & oDriver.ServerName
  265.            
  266.             oMaster.DriverDel oDriver
  267.    
  268.             if Err.Number = kErrorSuccess then
  269.    
  270.                 wscript.echo "Success: Driver """ & oDriver.ModelName & """ was deleted"
  271.                 
  272.                 iTotalDeleted = iTotalDeleted + 1   
  273.    
  274.             else
  275.    
  276.                 wscript.echo "Unable to delete driver """ & oDriver.ModelName  & """, error: 0x" _
  277.                              & Hex(Err.Number) & ". " & Err.Description
  278.                
  279.                 Err.Clear
  280.                
  281.             end if
  282.  
  283.          else
  284.          
  285.              wscript.echo "Unable to delete drivers on server, error: 0x" & _
  286.                           Hex(Err.Number) & ". " &  Err.Description 
  287.  
  288.              DelAllDrivers = kErrorFailure
  289.                           
  290.              exit function
  291.          
  292.          end if
  293.          
  294.     next
  295.  
  296.     wscript.echo "Number of drivers " & iTotal & ". Drivers deleted " & iTotalDeleted
  297.  
  298.     DelAllDrivers = kErrorSuccess
  299.     
  300. end function
  301.  
  302. '
  303. ' List drivers
  304. '
  305. function ListDrivers(strServer)
  306.  
  307.     on error resume next
  308.  
  309.     DebugPrint kDebugTrace, "In ListDriver"
  310.  
  311.     dim oMaster
  312.     dim oDriver
  313.     dim iResult
  314.     dim iTotal
  315.     dim vntDependentFiles
  316.     
  317.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  318.     
  319.     for each oDriver in oMaster.Drivers(strServer)
  320.  
  321.         if Err.Number = kErrorSuccess then
  322.  
  323.             wscript.echo ""
  324.             wscript.echo "ServerName    : " & oDriver.ServerName
  325.             wscript.echo "DriverName    : " & oDriver.ModelName
  326.             wscript.echo "Version       : " & oDriver.Version
  327.             wscript.echo "DriverVersion : " & GetVersion(oDriver.Version, oDriver.Environment)
  328.             wscript.echo "DriverPath    : " & oDriver.Path
  329.             wscript.echo "Environment   : " & oDriver.Environment
  330.             wscript.echo "Architecture  : " & GetArchitecture(oDriver.Environment)      
  331.             wscript.echo "MonitorName   : " & oDriver.MonitorName
  332.             wscript.echo "DataFile      : " & oDriver.DataFile
  333.             wscript.echo "ConfigFile    : " & oDriver.ConfigFile
  334.             wscript.echo "HelpFile      : " & oDriver.HelpFile
  335.            
  336.             vntDependentFiles = oDriver.DependentFiles
  337.            
  338.             '
  339.             ' If there are no dependent files, the method will set DependentFiles to
  340.             ' an empty variant, so we check if the variant is an array of variants 
  341.             '
  342.             if VarType(vntDependentFiles) = (vbArray + vbVariant) then
  343.            
  344.                 PrintDepFiles oDriver.DependentFiles
  345.                
  346.             end if    
  347.            
  348.             Err.Clear
  349.             
  350.         else
  351.         
  352.             wscript.echo "Unable to list drivers, error: 0x" & Hex(Err.Number) & _
  353.                          ". " & Err.Description
  354.         
  355.             ListDrivers = iErrorFailure
  356.                          
  357.             exit function
  358.             
  359.         end if    
  360.  
  361.     next
  362.     
  363.     wscript.echo "Success listing drivers"
  364.     
  365.     ListDrivers = kErrorSuccess
  366.  
  367. end function
  368.  
  369. '
  370. ' Prints the contents of an array of variants
  371. '
  372. sub PrintDepFiles(Param)
  373.  
  374.    on error resume next 
  375.     
  376.    dim iIndex
  377.    
  378.    iIndex = LBound(Param)
  379.    
  380.    if Err.Number = 0 then
  381.    
  382.       wscript.echo "Dependent Files "
  383.    
  384.       for iIndex = LBound(Param) to UBound(Param)
  385.        
  386.           wscript.echo "                " & Param(iIndex)
  387.        
  388.       next
  389.  
  390.    else
  391.  
  392.         wscript.echo "Unable to print the dependent files, error 0x" & _
  393.                      Hex(Err.Number ) & ". " & Err.Description
  394.  
  395.    end if
  396.       
  397. end sub  
  398.  
  399. '
  400. ' Debug display helper function
  401. '
  402. sub DebugPrint(uFlags, strString)
  403.  
  404.     if gDebugFlag = true then
  405.  
  406.         if uFlags = kDebugTrace then
  407.  
  408.             wscript.echo "Debug: " & strString
  409.  
  410.         end if
  411.  
  412.         if uFlags = kDebugError then
  413.  
  414.             if Err <> 0 then
  415.  
  416.                 wscript.echo "Debug: " & strString & " Failed with " & Hex(Err)
  417.  
  418.             end if
  419.  
  420.         end if
  421.  
  422.     end if
  423.  
  424. end sub
  425.  
  426. '
  427. ' Parse the command line into it's components
  428. '
  429. function ParseCommandLine(iAction, strServer, strModel, strPath, strVersion, strArchitecture, strInfFile)
  430.  
  431.     on error resume next
  432.  
  433.     DebugPrint kDebugTrace, "In the ParseCommandLine"
  434.  
  435.     dim oArgs
  436.     dim iIndex
  437.  
  438.     iAction = kActionUnknown
  439.     iIndex = 0
  440.  
  441.     set oArgs = wscript.Arguments
  442.  
  443.     while iIndex < oArgs.Count
  444.  
  445.         select case oArgs(iIndex)
  446.  
  447.             case "-a"
  448.                 iAction = kActionAdd
  449.  
  450.             case "-d"
  451.                 iAction = kActionDel
  452.  
  453.             case "-l"
  454.                 iAction = kActionList
  455.  
  456.             case "-x"
  457.                 iAction = kActionDelAll
  458.  
  459.             case "-c"
  460.                 iIndex = iIndex + 1
  461.                 strServer = oArgs(iIndex)
  462.  
  463.             case "-m"
  464.                 iIndex = iIndex + 1
  465.                 strModel = oArgs(iIndex)
  466.  
  467.             case "-p"
  468.                 iIndex = iIndex + 1
  469.                 strPath = oArgs(iIndex)
  470.  
  471.             case "-v"
  472.                 iIndex = iIndex + 1
  473.                 strVersion = oArgs(iIndex)
  474.  
  475.             case "-t"
  476.                 iIndex = iIndex + 1
  477.                 strArchitecture = oArgs(iIndex)
  478.  
  479.             case "-i"
  480.                 iIndex = iIndex + 1
  481.                 strInfFile = oArgs(iIndex)
  482.  
  483.             case "-?"
  484.                 Usage(true)
  485.                 exit function
  486.  
  487.             case else
  488.                 Usage(true)
  489.                 exit function
  490.  
  491.         end select
  492.  
  493.         iIndex = iIndex + 1
  494.  
  495.     wend
  496.  
  497.     if Err.Number <> 0 then
  498.  
  499.         wscript.echo "Unable to parse command line, error 0x" & _
  500.                      Hex(Err.Number) & ". " & Err.Description
  501.         
  502.         ParseCommandLine = kErrorFailure
  503.  
  504.     else
  505.  
  506.         ParseCommandLine = kErrorSuccess
  507.  
  508.     end if
  509.  
  510. end  function
  511.  
  512. '
  513. ' Display command usage.
  514. '
  515. sub Usage(bExit)
  516.  
  517.     wscript.echo "Usage: drvmgr [-adlx?] [-m model] [-v version] [-p path]"
  518.     wscript.echo "                       [-c server] [-t architecture] [-i inf file]" 
  519.     wscript.echo "Arguments:"
  520.     wscript.echo "-a     - add the specified driver"
  521.     wscript.echo "-c     - server name"
  522.     wscript.echo "-d     - delete the specified driver"
  523.     wscript.echo "-i     - inf file name"   
  524.     wscript.echo "-l     - list all drivers"
  525.     wscript.echo "-m     - driver model name"
  526.     wscript.echo "-p     - driver file path"
  527.     wscript.echo "-t     - architecture"
  528.     wscript.echo "-v     - version"
  529.     wscript.echo "-x     - delete all drivers that are not in use"  
  530.     wscript.echo "-?     - display command usage"
  531.     wscript.echo ""
  532.     wscript.echo "Examples:"
  533.     wscript.echo "drvmgr -a -m ""driver"" -v ""Windows 2000"" -t Intel"
  534.     wscript.echo "drvmgr -d -m ""driver"" -v ""Windows 2000"" -t Intel"
  535.     wscript.echo "drvmgr -l -c \\server"
  536.     wscript.echo "drvmgr -x -c \\server"
  537.  
  538.     if bExit then
  539.     
  540.         wscript.quit(1)
  541.         
  542.     end if
  543.  
  544. end sub
  545.  
  546. '
  547. ' Determines which program is used to run this script. 
  548. ' Returns true if the script host is cscript.exe
  549. '
  550. function IsHostCscript()
  551.  
  552.     on error resume next
  553.     
  554.     dim strFullName 
  555.     dim strCommand 
  556.     dim i, j 
  557.     dim bReturn
  558.     
  559.     bReturn = false
  560.     
  561.     strFullName = WScript.FullName
  562.     
  563.     i = InStr(1, strFullName, ".exe", 1)
  564.     
  565.     if i <> 0 then
  566.         
  567.         j = InStrRev(strFullName, "\", i, 1)
  568.         
  569.         if j <> 0 then
  570.             
  571.             strCommand = Mid(strFullName, j+1, i-j-1)
  572.             
  573.             if LCase(strCommand) = "cscript" then
  574.             
  575.                 bReturn = true  
  576.             
  577.             end if    
  578.                 
  579.         end if
  580.         
  581.     end if
  582.     
  583.     if Err <> 0 then
  584.     
  585.         call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  586.                           & ". " & vbCRLF & "The scripting host could not be determined.")       
  587.         
  588.     end if
  589.     
  590.     IsHostCscript = bReturn
  591.  
  592. end function
  593.  
  594. '
  595. ' Converts a driver environment string to a string 
  596. ' representing the architecture of the driver. 
  597. '
  598. function GetArchitecture(strEnvironment)
  599.                            
  600.     dim strArchitecture
  601.     
  602.     if strEnvironment = kEnvironmentIntel then
  603.         strArchitecture = kArchIntel
  604.     elseif strEnvironment = kEnvironmentMIPS then
  605.         strArchitecture = kArchMIPS
  606.     elseif strEnvironment = kEnvironmentAlpha then
  607.         strArchitecture = kArchAlpha
  608.     elseif strEnvironment = kEnvironmentPowerPC then
  609.         strArchitecture = kArchPowerPC
  610.     elseif strEnvironment = kEnvironmentWindows then 
  611.         strArchitecture = kArchIntel
  612.     else 
  613.         strArchitecture = kArchUnknown
  614.     end if    
  615.     
  616.     GetArchitecture = strArchitecture
  617.     
  618. end function
  619.  
  620. '
  621. ' Converts a driver environment string and a number to 
  622. ' a string representing the driver version
  623. '
  624. function GetVersion(uVersion, strEnvironment)
  625.  
  626.     dim strVersion
  627.  
  628.     select case uVersion
  629.     case 0:
  630.         if strEnvironment = kEnvironmentWindows then
  631.             strVersion = kVersionWindows95
  632.         else 
  633.             strVersion = kVersionNT31
  634.             
  635.         end if    
  636.         
  637.     case 1:
  638.         if strEnvironment = kEnvironmentPowerPC then
  639.             strVersion = kVersion351
  640.         else
  641.             strVersion = kVersion35x
  642.         end if    
  643.         
  644.     case 2:
  645.         if strEnvironment = kEnvironmentPowerPC or _
  646.            strEnvironment = kEnvironmentMIPS    or _
  647.            strEnvironment = kEnvironmentAlpha   then
  648.             strVersion = kVersion40
  649.         else
  650.             strVersion = kVersion4050
  651.         end if
  652.  
  653.     case 3:
  654.         strVersion = kVersion50
  655.         
  656.     case else:
  657.         strVersion = kArchUnknown
  658.         
  659.     end select    
  660.     
  661.     GetVersion = strVersion
  662.     
  663. end function
  664.