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

  1. '********************************************************************
  2. '*
  3. '* File:           Bus.vbs 
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Get the bus information for a machine.
  8. '*
  9. '*  Bus.vbs  [/S <server>] [/U <username>] [/W <password>]
  10. '*           [/O <outputfile>]
  11. '*
  12. '* Copyright (C) 1999 Microsoft Corporation
  13. '*
  14. '********************************************************************
  15.  
  16. OPTION EXPLICIT
  17.  
  18.     'Define constants
  19.     CONST CONST_ERROR                   = 0
  20.     CONST CONST_WSCRIPT                 = 1
  21.     CONST CONST_CSCRIPT                 = 2
  22.     CONST CONST_SHOW_USAGE              = 3
  23.     CONST CONST_LIST                    = 4
  24.  
  25.     'Declare variables
  26.     Dim intOpMode
  27.     Dim strServer, strUserName, strPassword, strOutputFile
  28.  
  29.     'Make sure the host is csript, if not then abort
  30.     VerifyHostIsCscript()
  31.  
  32.     'Parse the command line
  33.     intOpMode = intParseCmdLine(strServer     ,  _
  34.                                 strUserName   ,  _
  35.                                 strPassword   ,  _
  36.                                 strOutputFile    )
  37.  
  38.  
  39.     Select Case intOpMode
  40.  
  41.         Case CONST_SHOW_USAGE
  42.             Call ShowUsage()
  43.  
  44.         Case CONST_LIST
  45.             Call GetBusInfo(strServer,      _
  46.                             strOutputFile,  _
  47.                             strUserName,    _
  48.                             strPassword     )
  49.  
  50.         Case CONST_ERROR
  51.             'Do Nothing
  52.  
  53.         Case Else                    'Default -- should never happen
  54.             Call Wscript.Echo("Error occurred in passing parameters.")
  55.  
  56.     End Select
  57.  
  58. '********************************************************************
  59. '* End of Script
  60. '********************************************************************
  61.  
  62. '********************************************************************
  63. '*
  64. '* Sub GetBusInfo()
  65. '*
  66. '* Purpose: Get the bus information for a machine.
  67. '*
  68. '* Input:   strServer           a machine name
  69. '*          strOutputFile       an output file name
  70. '*          strUserName         the current user's name
  71. '*          strPassword         the current user's password
  72. '*
  73. '* Output:  Results are either printed on screen or saved in strOutputFile.
  74. '*
  75. '********************************************************************
  76. Private Sub GetBusInfo(strServer, strOutputFile, strUserName, strPassword)
  77.  
  78.     ON ERROR RESUME NEXT
  79.  
  80.     Dim objFileSystem, objOutputFile, objService, objBusSet, objBus
  81.     Dim strQuery, strMessage
  82.     Dim strWBEMClass
  83.  
  84.     strWBEMClass = "Win32_Bus"
  85.  
  86.     'Open a text file for output if the file is requested
  87.     If Not IsEmpty(strOutputFile) Then
  88.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  89.             Call Wscript.Echo ("Could not open an output file.")
  90.             Exit Sub
  91.         End If
  92.     End If
  93.  
  94.     'Establish a connection with the server.
  95.     If blnConnect("root\cimv2" , _
  96.                    strUserName , _
  97.                    strPassword , _
  98.                    strServer   , _
  99.                    objService  ) Then
  100.         Call Wscript.Echo("")
  101.         Call Wscript.Echo("Please check the server name, " _
  102.                         & "credentials and WBEM Core.")
  103.         Exit Sub
  104.     End If
  105.  
  106.     'Get the set
  107.     Set objBusSet = objService.InstancesOf(strWBEMClass)
  108.     If blnErrorOccurred("Could not obtain " & strWBEMClass _
  109.                       & " instance.") Then
  110.         Exit Sub
  111.     End If
  112.  
  113.     If objBusSet.Count = 0 Then
  114.         Call WriteLine("No bus information is available.", objOutputFile)    
  115.         Exit Sub
  116.     End If
  117.  
  118.     Call WriteLine("Bus information for Machine " & strServer, objOutputFile)
  119.     Call WriteLine("", objOutputFile)
  120.  
  121.     For Each objBus In objBusSet
  122.         Call WriteLine("Name = " & objBus.Name, objOutputFile)
  123.         Call WriteLine("Bus Num = " & objBus.BusNum, objOutputFile)
  124.         Call WriteLine("Bus Type = " & objBus.BusType, objOutputFile)
  125.         Call WriteLine("Device ID = " & objBus.DeviceID, objOutputFile)
  126.         Call WriteLine("PNP Device ID = " & objBus.PNPDeviceID, objOutputFile)
  127.         Call WriteLine("", objOutputFile)
  128.     Next
  129.  
  130.     If IsObject(objOutputFile) Then
  131.         objOutputFile.Close
  132.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  133.     End If
  134.  
  135. End Sub
  136.  
  137. '********************************************************************
  138. '*
  139. '* Function intParseCmdLine()
  140. '*
  141. '* Purpose: Parses the command line.
  142. '* Input:   
  143. '*
  144. '* Output:  strServer         a remote server ("" = local server")
  145. '*          strUserName       the current user's name
  146. '*          strPassword       the current user's password
  147. '*          strOutputFile     an output file name
  148. '*
  149. '********************************************************************
  150. Private Function intParseCmdLine( ByRef strServer,        _
  151.                                   ByRef strUserName,      _
  152.                                   ByRef strPassword,      _
  153.                                   ByRef strOutputFile     )
  154.  
  155.  
  156.     ON ERROR RESUME NEXT
  157.  
  158.     Dim strFlag
  159.     Dim intState, intArgIter
  160.     Dim objFileSystem
  161.  
  162.     If Wscript.Arguments.Count > 0 Then
  163.         strFlag = Wscript.arguments.Item(0)
  164.     End If
  165.  
  166.     If IsEmpty(strFlag) Then                'No arguments have been received
  167.         intParseCmdLine = CONST_LIST
  168.         Exit Function
  169.     End If
  170.  
  171.     'Check if the user is asking for help or is just confused
  172.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  173.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  174.         OR (strFlag="h") Then
  175.         intParseCmdLine = CONST_SHOW_USAGE
  176.         Exit Function
  177.     End If
  178.  
  179.     'Retrieve the command line and set appropriate variables
  180.      intArgIter = 0
  181.     Do While intArgIter <= Wscript.arguments.Count - 1
  182.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  183.   
  184.             Case "/s"
  185.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  186.                     intParseCmdLine = CONST_ERROR
  187.                     Exit Function
  188.                 End If
  189.                 intArgIter = intArgIter + 1
  190.  
  191.             Case "/o"
  192.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  193.                     intParseCmdLine = CONST_ERROR
  194.                     Exit Function
  195.                 End If
  196.                 intArgIter = intArgIter + 1
  197.  
  198.             Case "/u"
  199.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  200.                     intParseCmdLine = CONST_ERROR
  201.                     Exit Function
  202.                 End If
  203.                 intArgIter = intArgIter + 1
  204.  
  205.             Case "/w"
  206.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  207.                     intParseCmdLine = CONST_ERROR
  208.                     Exit Function
  209.                 End If
  210.                 intArgIter = intArgIter + 1
  211.  
  212.             Case Else 'We shouldn't get here
  213.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  214.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  215.                    & "Please check the input and try again," & vbCRLF _
  216.                    & "or invoke with '/?' for help with the syntax.")
  217.                 Wscript.Quit
  218.  
  219.         End Select
  220.  
  221.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  222.  
  223.     If IsEmpty(intParseCmdLine) Then _
  224.         intParseCmdLine = CONST_LIST
  225.  
  226. End Function
  227.  
  228. '********************************************************************
  229. '*
  230. '* Sub ShowUsage()
  231. '*
  232. '* Purpose: Shows the correct usage to the user.
  233. '*
  234. '* Input:   None
  235. '*
  236. '* Output:  Help messages are displayed on screen.
  237. '*
  238. '********************************************************************
  239. Private Sub ShowUsage()
  240.  
  241.     Wscript.Echo ""
  242.     Wscript.Echo "Get the bus information for a machine."
  243.     Wscript.Echo ""
  244.     Wscript.Echo "SYNTAX:"
  245.     Wscript.Echo "  Bus.vbs [/S <server>] [/U <username>] [/W <password>]"
  246.     Wscript.Echo "  [/O <outputfile>]"
  247.     Wscript.Echo ""
  248.     Wscript.Echo "PARAMETER SPECIFIERS:"
  249.     Wscript.Echo "   server        A machine name."
  250.     Wscript.Echo "   username      The current user's name."
  251.     Wscript.Echo "   password      Password of the current user."
  252.     Wscript.Echo "   outputfile    The output file name."
  253.     Wscript.Echo ""
  254.     Wscript.Echo "EXAMPLE:"
  255.     Wscript.Echo "1. cscript Bus.vbs"
  256.     Wscript.Echo "   Get the bus information for the current machine."
  257.     Wscript.Echo "2. cscript Bus.vbs /S MyMachine2"
  258.     Wscript.Echo "   Get the bus information for the machine MyMachine2."
  259.  
  260. End Sub
  261.  
  262. '********************************************************************
  263. '* General Routines
  264. '********************************************************************
  265.  
  266. '********************************************************************
  267. '*
  268. '* Function strPackString()
  269. '*
  270. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  271. '*
  272. '* Input:   strString   a string
  273. '*          intWidth    the intended length of the string
  274. '*          blnAfter    Should spaces be added after the string?
  275. '*          blnTruncate specifies whether to truncate the string or not if
  276. '*                      the string length is longer than intWidth
  277. '*
  278. '* Output:  strPackString is returned as the packed string.
  279. '*
  280. '********************************************************************
  281. Private Function strPackString( ByVal strString, _
  282.                                 ByVal intWidth,  _
  283.                                 ByVal blnAfter,  _
  284.                                 ByVal blnTruncate)
  285.  
  286.     ON ERROR RESUME NEXT
  287.  
  288.     intWidth      = CInt(intWidth)
  289.     blnAfter      = CBool(blnAfter)
  290.     blnTruncate   = CBool(blnTruncate)
  291.  
  292.     If Err.Number Then
  293.         Call Wscript.Echo ("Argument type is incorrect!")
  294.         Err.Clear
  295.         Wscript.Quit
  296.     End If
  297.  
  298.     If IsNull(strString) Then
  299.         strPackString = "null" & Space(intWidth-4)
  300.         Exit Function
  301.     End If
  302.  
  303.     strString = CStr(strString)
  304.     If Err.Number Then
  305.         Call Wscript.Echo ("Argument type is incorrect!")
  306.         Err.Clear
  307.         Wscript.Quit
  308.     End If
  309.  
  310.     If intWidth > Len(strString) Then
  311.         If blnAfter Then
  312.             strPackString = strString & Space(intWidth-Len(strString))
  313.         Else
  314.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  315.         End If
  316.     Else
  317.         If blnTruncate Then
  318.             strPackString = Left(strString, intWidth-1) & " "
  319.         Else
  320.             strPackString = strString & " "
  321.         End If
  322.     End If
  323.  
  324. End Function
  325.  
  326. '********************************************************************
  327. '* 
  328. '*  Function blnGetArg()
  329. '*
  330. '*  Purpose: Helper to intParseCmdLine()
  331. '* 
  332. '*  Usage:
  333. '*
  334. '*     Case "/s" 
  335. '*       blnGetArg ("server name", strServer, intArgIter)
  336. '*
  337. '********************************************************************
  338. Private Function blnGetArg ( ByVal StrVarName,   _
  339.                              ByRef strVar,       _
  340.                              ByRef intArgIter) 
  341.  
  342.     blnGetArg = False 'failure, changed to True upon successful completion
  343.  
  344.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  345.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  346.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  347.                 strVar = Right(Wscript.Arguments(intArgIter), _
  348.                          Len(Wscript.Arguments(intArgIter)) - 3)
  349.                 blnGetArg = True
  350.                 Exit Function
  351.             Else
  352.                 intArgIter = intArgIter + 1
  353.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  354.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  355.                     Call Wscript.Echo( "Please check the input and try again.")
  356.                     Exit Function
  357.                 End If
  358.  
  359.                 strVar = Wscript.Arguments.Item(intArgIter)
  360.                 If Err.Number Then
  361.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  362.                     Call Wscript.Echo( "Please check the input and try again.")
  363.                     Exit Function
  364.                 End If
  365.  
  366.                 If InStr(strVar, "/") Then
  367.                     Call Wscript.Echo( "Invalid " & StrVarName)
  368.                     Call Wscript.Echo( "Please check the input and try again.")
  369.                     Exit Function
  370.                 End If
  371.  
  372.                 blnGetArg = True 'success
  373.             End If
  374.         Else
  375.             strVar = Right(Wscript.Arguments(intArgIter), _
  376.                      Len(Wscript.Arguments(intArgIter)) - 2)
  377.             blnGetArg = True 'success
  378.             Exit Function
  379.         End If
  380.     Else
  381.         intArgIter = intArgIter + 1
  382.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  383.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  384.             Call Wscript.Echo( "Please check the input and try again.")
  385.             Exit Function
  386.         End If
  387.  
  388.         strVar = Wscript.Arguments.Item(intArgIter)
  389.         If Err.Number Then
  390.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  391.             Call Wscript.Echo( "Please check the input and try again.")
  392.             Exit Function
  393.         End If
  394.  
  395.         If InStr(strVar, "/") Then
  396.             Call Wscript.Echo( "Invalid " & StrVarName)
  397.             Call Wscript.Echo( "Please check the input and try again.")
  398.             Exit Function
  399.         End If
  400.         blnGetArg = True 'success
  401.     End If
  402. End Function
  403.  
  404. '********************************************************************
  405. '*
  406. '* Function blnConnect()
  407. '*
  408. '* Purpose: Connects to machine strServer.
  409. '*
  410. '* Input:   strServer       a machine name
  411. '*          strNameSpace    a namespace
  412. '*          strUserName     name of the current user
  413. '*          strPassword     password of the current user
  414. '*
  415. '* Output:  objService is returned  as a service object.
  416. '*          strServer is set to local host if left unspecified
  417. '*
  418. '********************************************************************
  419. Private Function blnConnect(ByVal strNameSpace, _
  420.                             ByVal strUserName,  _
  421.                             ByVal strPassword,  _
  422.                             ByRef strServer,    _
  423.                             ByRef objService)
  424.  
  425.     ON ERROR RESUME NEXT
  426.  
  427.     Dim objLocator, objWshNet
  428.  
  429.     blnConnect = False     'There is no error.
  430.  
  431.     'Create Locator object to connect to remote CIM object manager
  432.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  433.     If Err.Number then
  434.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  435.                            " occurred in creating a locator object." )
  436.         If Err.Description <> "" Then
  437.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  438.         End If
  439.         Err.Clear
  440.         blnConnect = True     'An error occurred
  441.         Exit Function
  442.     End If
  443.  
  444.     'Connect to the namespace which is either local or remote
  445.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  446.        strUserName, strPassword)
  447.     ObjService.Security_.impersonationlevel = 3
  448.     If Err.Number then
  449.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  450.                            " occurred in connecting to server " _
  451.            & strServer & ".")
  452.         If Err.Description <> "" Then
  453.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  454.         End If
  455.         Err.Clear
  456.         blnConnect = True     'An error occurred
  457.     End If
  458.  
  459.     'Get the current server's name if left unspecified
  460.     If IsEmpty(strServer) Then
  461.         Set objWshNet = CreateObject("Wscript.Network")
  462.     strServer     = objWshNet.ComputerName
  463.     End If
  464.  
  465. End Function
  466.  
  467. '********************************************************************
  468. '*
  469. '* Sub      VerifyHostIsCscript()
  470. '*
  471. '* Purpose: Determines which program is used to run this script.
  472. '*
  473. '* Input:   None
  474. '*
  475. '* Output:  If host is not cscript, then an error message is printed 
  476. '*          and the script is aborted.
  477. '*
  478. '********************************************************************
  479. Sub VerifyHostIsCscript()
  480.  
  481.     ON ERROR RESUME NEXT
  482.  
  483.     Dim strFullName, strCommand, i, j, intStatus
  484.  
  485.     strFullName = WScript.FullName
  486.  
  487.     If Err.Number then
  488.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  489.         If Err.Description <> "" Then
  490.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  491.         End If
  492.         intStatus =  CONST_ERROR
  493.     End If
  494.  
  495.     i = InStr(1, strFullName, ".exe", 1)
  496.     If i = 0 Then
  497.         intStatus =  CONST_ERROR
  498.     Else
  499.         j = InStrRev(strFullName, "\", i, 1)
  500.         If j = 0 Then
  501.             intStatus =  CONST_ERROR
  502.         Else
  503.             strCommand = Mid(strFullName, j+1, i-j-1)
  504.             Select Case LCase(strCommand)
  505.                 Case "cscript"
  506.                     intStatus = CONST_CSCRIPT
  507.                 Case "wscript"
  508.                     intStatus = CONST_WSCRIPT
  509.                 Case Else       'should never happen
  510.                     Call Wscript.Echo( "An unexpected program was used to " _
  511.                                        & "run this script." )
  512.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  513.                                        & "be used to run this script." )
  514.                     intStatus = CONST_ERROR
  515.                 End Select
  516.         End If
  517.     End If
  518.  
  519.     If intStatus <> CONST_CSCRIPT Then
  520.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  521.              "This can be achieved by" & vbCRLF & _
  522.              "1. Using ""CScript bus.vbs arguments"" for Windows 95/98 or" _
  523.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  524.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  525.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  526.              "    ""bus.vbs arguments"" for Windows NT/2000." )
  527.         WScript.Quit
  528.     End If
  529.  
  530. End Sub
  531.  
  532. '********************************************************************
  533. '*
  534. '* Sub WriteLine()
  535. '* Purpose: Writes a text line either to a file or on screen.
  536. '* Input:   strMessage  the string to print
  537. '*          objFile     an output file object
  538. '* Output:  strMessage is either displayed on screen or written to a file.
  539. '*
  540. '********************************************************************
  541. Sub WriteLine(ByVal strMessage, ByVal objFile)
  542.  
  543.     On Error Resume Next
  544.     If IsObject(objFile) then        'objFile should be a file object
  545.         objFile.WriteLine strMessage
  546.     Else
  547.         Call Wscript.Echo( strMessage )
  548.     End If
  549.  
  550. End Sub
  551.  
  552. '********************************************************************
  553. '* 
  554. '* Function blnErrorOccurred()
  555. '*
  556. '* Purpose: Reports error with a string saying what the error occurred in.
  557. '*
  558. '* Input:   strIn        string saying what the error occurred in.
  559. '*
  560. '* Output:  displayed on screen 
  561. '* 
  562. '********************************************************************
  563. Private Function blnErrorOccurred (ByVal strIn)
  564.  
  565.     If Err.Number Then
  566.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  567.         If Err.Description <> "" Then
  568.             Call Wscript.Echo( "Error description: " & Err.Description)
  569.         End If
  570.         Err.Clear
  571.         blnErrorOccurred = True
  572.     Else
  573.         blnErrorOccurred = False
  574.     End If
  575.  
  576. End Function
  577.  
  578. '********************************************************************
  579. '* 
  580. '* Function blnOpenFile
  581. '*
  582. '* Purpose: Opens a file.
  583. '*
  584. '* Input:   strFileName        A string with the name of the file.
  585. '*
  586. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  587. '*            Nothing upon Failure.
  588. '* 
  589. '********************************************************************
  590. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  591.  
  592.     ON ERROR RESUME NEXT
  593.  
  594.     Dim objFileSystem
  595.  
  596.     Set objFileSystem = Nothing
  597.  
  598.     If IsEmpty(strFileName) OR strFileName = "" Then
  599.         blnOpenFile = False
  600.         Set objOpenFile = Nothing
  601.         Exit Function
  602.     End If
  603.  
  604.     'Create a file object
  605.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  606.     If blnErrorOccurred("Could not create filesystem object.") Then
  607.         blnOpenFile = False
  608.         Set objOpenFile = Nothing
  609.         Exit Function
  610.     End If
  611.  
  612.     'Open the file for output
  613.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  614.     If blnErrorOccurred("Could not open") Then
  615.         blnOpenFile = False
  616.         Set objOpenFile = Nothing
  617.         Exit Function
  618.     End If
  619.     blnOpenFile = True
  620.  
  621. End Function
  622.  
  623. '********************************************************************
  624. '*                                                                  *
  625. '*                           End of File                            *
  626. '*                                                                  *
  627. '********************************************************************
  628.