'***************************************************************************************************************
'*                                    INTEL DISCLOSURE And COPYRIGHT INFORMATION                              '*
'*                                                                                                            '*  
'*                THIS DOCUMENT AND ALL INFORMATION PROVIDED HEREIN, INCLUDING CODE, IS PROVIDED              '*  
'*                "AS IS" WITH NO WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY,           '*
'*                NONINFRINGEMENT, FITNESS FOR ANY PARTICULAR PURPOSE, OR ANY WARRANTY OTHERWISE              '* 
'*                ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.                                       '*
'*                Intel disclaims all liability, including liability for infringement of any proprietary      '*
'*                rights, relating to use of information or code in this document.   Recipient may use        '*
'*                this document and all information herein, including code, but all such use is and shall     '*
'*                be at recipient's sole risk and subject to the disclaimers and limitations of liability     '*
'*                herein.  No other license, express or implied, by estoppel or otherwise, to any             '*  
'*                intellectual property rights is granted herein.                                             '*  
'*                Copyright (c) Intel Corporation 2008. *Third-party brands and names are                     '*  
'*                the property of their respective owners.                                                    '*  
                                                                                                       
'***************************************************************************************************************
' File              :     Adapters_Enumerate.VBS
' Version           :     1.0
'
'  Purpose : Print a list of installed adapters in a computer along with its status.
'            * The name of the adapter comes from the root\intelncs2\IANet_PhysicalEthernetAdpater.Name parameter
'            * The status of an adapter comes from the .Status parameter of the same class
'
'  Arguments : Adapters_Enumerate.vbs [<target> username password, help]
'
'              (no arguments) - defaults to local computer
'              <remote>       - the name of a remote computer; requires the following:
'              <username>     - user name
'              <password>     - password
'
'  Notes : Phantom adapter instances are not printed (these are adapters which have drivers installed but
'          are not physically present. By specifying 'StatusInfo=3' we can skip phantom instances.
'
'  Instructions for use:
'  =====================
'  1) Execute script by invoking "cscript Adapters_Enumerate.vbs" from the command line with arguments listed above
'  2) The common script library vbslib.vbs must be located in the same directory
'
'***************************************************************************************************************
   Option Explicit
   On Error Resume Next
   Include "VBSLIB.vbs"

     Const SCRIPT_NAME = "Adapter_Enumerate.vbs"
     Const SCRIPT_DESCRIPTION = "Enumerates inserted network adapters in a machine"

     'Global variables
          Dim Target, Username, Password
          Dim WbemServices
          Dim Adapters
          Dim sError
     
     'Check command line is correct
          If ParseCommandLine(Target, Username, Password) = false then 
               PrintScriptHelp
               Wscript.Quit 0
          End if

     'Connect
          If WMIConnect(WbemServices, "root\Intelncs2", Target, Username, Password, sError) = false then
               Wscript.Echo "Failed to connect to WMI namespace. " & VbNewLine
               wscript.echo "Make sure Intel(R) Network Connections software is installed."
               Wscript.Quit 0
          End if
          
     'Declare new instance of cAdapters class
          Set Adapters = new cAdapters

     'Enumerate adapters using the Wbem handle obtained above 
          If Adapters.Enumerate (WbemServices, sError) = False Then
               Wscript.Echo "Failed to enumerate adapters. " & sError
               Wscript.Quit 0
          End If     

     dim count
     'Print out an indexed list of adapters in the machine, starting with index 1
          if Adapters.AdapterCount > 0 then
               Wscript.Echo "Installed adapters:" & VbNewLine
               For count = 0 to Adapters.AdapterCount - 1
                    Wscript.Echo "   " & Cstr(count + 1) & ") " & Adapters.Name(count)
               Next
          else
               Wscript.Echo "No adapters found"
          end if

          wscript.Quit 1
'===================================================
'  Function : ParseCommandLine
'   Purpose : Evaluates command line arguments passed to the script and determines if they are correct
' Arguments : 
'             Target - the machine which will receive the request
'             UserName - the user name passed in the command line
'             Password - the password passed in the command line
'   Returns : True (if the command line has the correct format), False otherwise
'   Notes   : User name can be in the form of either a user name or a Domain\Username
'===================================================
     Function ParseCommandLine (byref Target, byref Username, byref Password)
     
          Err.Clear
          On Error Resume Next
          
          'Capture command line arguments
          dim cmd_args : set cmd_args = wscript.Arguments
     
          'Examine number of arguments
          Select Case cmd_args.count
               Case 0     ' no arguments defaults to local machine
                    Target = "."
                    UserName = ""
                    Password = ""
                    ParseCommandLine = true
                    exit function
               Case 1     
                    if instr(LCase(cmd_args(0)), "help") >= 0 then
                         ParseCommandLine = false
                         Exit Function
                    end if                         
               Case 3 ' the first argument cannot be 'local'
                    Target = cmd_args(0)
                    UserName = cmd_args(1)
                    Password = cmd_args(2)
                    ParseCommandLine = true
                    exit function
          End Select

          ParseCommandLine = false
          
     End Function

     '===================================================
     '       Sub : PrintScriptHelp
     '   Purpose : Prints command line help for the user
     '             on how to use this script
     '===================================================
     Sub PrintScriptHelp
     
          Wscript.Echo "Usage :" & VbNewLine & VbNewLine &_ 
                         "  " & SCRIPT_NAME & " [Target Username Password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    None required" & VbNewLine & VbNewLine &_
                         OptionalCmdLineArgsString
     End Sub
     
' DO NOT REMOVE THIS Function
    function Include(vbsFile)

      dim fso, ts, buf, path
      Set fso = CreateObject("Scripting.FileSystemObject")
      
      path = fso.GetParentFolderName (WScript.ScriptFullName) & "\" & vbsFile
      
      If fso.FileExists(path) Then  
           set ts = fso.OpenTextFile(path)
           buf = ts.ReadAll()
           ts.Close()
           Executeglobal buf
      Else
           Wscript.Echo "Unable to open include file: " & path
      End If
      
    end function
