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

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