'***************************************************************************************************************
'*                                    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_GetSupportedTeamingModes.VBS
' Version           :     1.0
'
'  Purpose :   To validate if the adapter can be teamed and will display supported team modes
'                   
'
'  Arguments : Adapter_GetSupportedTeamingModes.vbs <AdapterIndex>[target username password] [help]
'
'               <AdapterIndex> - Adapter to get the supported team modes 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_GetSupportedTeamingModes.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_GetSupportedTeamingModes.vbs"
     Const SCRIPT_DESCRIPTION = "To validate if the adapter can be teamed and will display supported team modes"

     'Global variables
     dim adapterIndex, internalAdapterindex
     Dim Target, Username, Password
     Dim WbemServices
     Dim sError
     Dim AdapterSet
     
     'Check command line is correct
     If ParseCommandLine(adapterIndex, 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 
     internalAdapterIndex = adapterIndex - 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 (internalAdapterIndex < 0) then
          wscript.echo "Invalid adapter index. Adapter index must be greater than 0"
          wscript.quit 0
     elseif  (internalAdapterIndex >= AdapterSet.AdapterCount) then
          wscript.echo "Adapter index not found: " & adapterIndex
          wscript.quit 0
     end if

     ' Get the specified adapter instance
     dim adapter : set adapter = AdapterSet.GetAdapterInstance(internalAdapterIndex)

     ' Make sure we got an adapter back
     if IsNothing(adapter) then
          wscript.echo "Failed to get adapter instance"
          wscript.quit 0
     end if

     wscript.echo adapterIndex & ") " & adapter.Name & ":" & VbNewLine 

     ' Check if the adapter is capable of teaming
     if AdapterSet.HasCapability(internalAdapterIndex, TEAMING_IS_TEAMABLE) then
          ' Adapter is capable of teaming, tell the user
          wscript.echo "   Adapter currently supports the following team modes: " 
          
          ' Get the supported teaming modes
          dim SupportedTeamModes
          SupportedTeamModes = AdapterSet.GetSupportedTeamModes(internalAdapterIndex)

          ' Check that something was returned
          if not IsNothing(SupportedTeamModes) then 
               ' Loop through each mode that is returned
               dim teamMode 
               for each teamMode in SupportedTeamModes
                    ' Get the friendly text for so display to the screen
                    wscript.echo "      " & AdapterSet.CapabilityString(teamMode)
               next 
          else
               ' Nothing was returned, no team modes are supported.
               ' Should never reach here as it should fail the IS_TEAMABLE capability
               wscript.echo "      No team modes supported"
          end if
     else
          wscript.echo "   Adapter is NOT currently capable of teaming."
     end if

     

     wscript.Quit 0
          

     
'===================================================
' Function       : ParseCommandLine
' Purpose        : Evaluates command line arguments passed to the script and determines If they are correct
' Arguments      :     
'                       
'               AdapterIndex  - Index of the adapter to get the power usage options for
'               sTarget       - the name of a remote computer
'               sUserName     - the user name for remote authentication
'               sPassword     - the password for remote authentication
'
' 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 AdapterIndex, 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 
               ' if the only command line arg is 'help' return false so the script help will be displayed
               If instr(LCase(cmd_args(0)), "help") > 0 Then
                    ParseCommandLine = false
                    Exit Function
               else
                    if IsNumeric(cmd_args(0)) then
                         AdapterIndex = CInt(cmd_args(0))
                         sTarget = "."
                         sUserName = ""
                         sPassword = ""
                         ParseCommandLine = true
                         exit function
                    end if
               End If     
          Case 4
               ' Make sure the argument is a number before proceeding
               if IsNumeric(cmd_args(0)) then
                    AdapterIndex = CInt(cmd_args(0))
                    sTarget = cmd_args(1)
                    sUserName = cmd_args(2)
                    sPassword = cmd_args(3)
                    ParseCommandLine = true
                    exit function
               end if                       
     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
