'***************************************************************************************************************
'*                                    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              :     Adapter_EnumerateProperties.vbs
' Version           :     1.0
'
'  Purpose :   Enumerates the given adapter's properties
'                   
'
'  Arguments : Adapter_EnumerateProperties.vbs [AdapterIndex] [target username password] [help]
'
'               <AdapterIndex>  - Adapter to get the properties for 
'               <remote>        - the name of a remote computer; requires the following:
'               <username>      - user name
'               <password>      - password
'
'
'  Instructions for use:
'  =====================
'  1) Execute script by invoking "cscript Adapter_EnumerateProperties.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_EnumerateProperties.vbs"
     Const SCRIPT_DESCRIPTION = "Returns all properties from a given class"

     'Global variables
     Dim nExternalIndex, nInternalIndex, sClass
     dim AdapterSet
     Dim Target, Username, Password
     Dim WbemServices
     Dim sError
     
     'Check command line is correct
     If ParseCommandLine(nExternalIndex, 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          

     ' Subtract 1 since the user is going to give an index based on 
     ' a 1-indexed array and the adapter array is 0-indexed 
     nInternalIndex = nExternalIndex - 1 
     
     'Declare new instance of cAdapters class
     Set AdapterSet = new cAdapters
          
     'Enumerate adapters using the Wbem handle obtained above 
     If AdapterSet.Enumerate (WbemServices, sError) = False Then
          Wscript.Echo "Failed to enumerate adapters. " & sError
          Wscript.Quit 0
     End If  
     
     ' Verify the adapter specified is a valid index, bail if not
     if (nInternalIndex < 0) then
          wscript.echo "Invalid adapter index. Adapter index must be greater than 0"
          wscript.quit 0
     end if
     
     if  (nInternalIndex >= AdapterSet.AdapterCount) then
          wscript.echo "Adapter index not found: " & nExternalIndex
          wscript.quit 0
     end if
           
     dim adapter : set adapter = AdapterSet.GetAdapterInstance(nInternalIndex)

     ' Give the name of the desired adapter
     wscript.echo CStr(nExternalIndex) & ") " & adapter.Name & " properties: " & VbNewLine

     ' Loop through the property set and print them on the screen
     dim property
     for each property in adapter.DeviceInstance.Properties_
          ' Blank spaces are placed for so it lines up 
          ' properly with the adapter name
          dim propertyString : propertyString = "   "
               
          ' if its an array, we cant display it.  
          if not IsArray(property.value) then
               ' Format the property string so its format is
               ' <property name> = <value>
               ' Blank spaces are placed for so it lines up 
               ' properly with the adapter name
               propertyString = propertyString & property.name & " - "
               
               ' if the value is blank, we need to indicate that its empty
               if property.value <> "" then
                    propertyString = propertyString & CStr(property.value)
               else
                    propertyString = propertyString & "<empty>"
               end if     
               wscript.echo propertyString
          else 
               ' Format the property string so its format is
               ' <property name> = <value>
               propertyString =  propertyString & property.name & " - " & join(property.value,", ")
               wscript.echo propertyString
               
          end if
          
     next
     
     wscript.Quit 0

'===================================================
' Function       : ParseCommandLine
' Purpose        : Evaluates command line arguments passed to the script and determines If they are correct
' Arguments      :     
'                       
'              nIndex    - Adapter to get the properties for
'              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\argUserName
'                'Help' will return false - the script does not need to do anything else
'===================================================
Function ParseCommandLine (byref nIndex, byref sTarget, byref sUserName, byref sPassword)

     Err.Clear
     On Error Resume Next
     
     'Capture command line arguments
     Dim cmd_args : Set cmd_args = wscript.Arguments
     
     'Check number of command line arguments and exit If it is not correct
     Select Case cmd_args.count
          Case 1
               ' Check if the user specified help and show the script help
               If instr(LCase(cmd_args(0)), "help") > 0 Then
                    ParseCommandLine = false
                    Exit Function
               end if
               
               ' Check if the first arg (index #) is not a number
               if not IsNumeric(cmd_args(0))then
                    ParseCommandLine = false
                    exit function
              end if
              
               nIndex = CInt(cmd_args(0))
               sTarget = "."
               sUserName = ""
               sPassword = ""
               ParseCommandLine = true
               Exit Function                       
          Case 4
               ' Check if the first arg (index #) is not a number or if sClass/sPropertyName is and bail
               if not IsNumeric(cmd_args(0)) then
                    ParseCommandLine = false
                    exit function
              end if
               nIndex = CInt(cmd_args(0))
               sTarget = cmd_args(1)
               sUserName = cmd_args(2)
               sPassword = cmd_args(3)
               ParseCommandLine = true
               Exit Function       
     End Select
     
     'Return failure
     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 & " <AdapterIndex> [Target Username Password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    " & AdapterIndexString & VbNewLine &_
                         "                   " & AdapterGetIndexString & 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
          Wscript.Quit 0
     End If

End function
