'***************************************************************************************************************
'*                                    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              :     Team_ValidateSettingsOnTeams.VBS
' Version           :     1.0
'
'  Purpose :   Validates synchronization of all the settings of the adapters in the team
'                   
'
'  Arguments : Team_ValidateSettingsOnTeams.vbs <AdapterList>[target username password] [help]
'
'               
'               <AdapterList> - Comma delimeted string containing the desired adapters' index
'               <remote>      - the name of a remote computer; requires the following:
'               <username>    - user name
'               <password>    - password
'
'
'  Instructions for use:
'  =====================
'  1) Execute script by invoking "cscript Team_ValidateSettingsOnTeams.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 = "Team_ValidateSettingsOnTeams.vbs"
     Const SCRIPT_DESCRIPTION = "Validates synchronization of all the settings of the adapters in the team"

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

     InternalAdapterList = ShiftIntStringDown(AdapterList)

     dim currentAdapterIndex
     for each currentAdapterIndex in split(InternalAdapterList,",")
          if ((currentAdapterIndex >= 0) AND (Cint(currentAdapterIndex) < AdapterSet.AdapterCount) )then          
               PushObject AdapterArray, AdapterSet.GetAdapterInstance(currentAdapterIndex)
          else
               wscript.echo "Adapter index not found: " & currentAdapterIndex + 1
               wscript.quit 0
          end if
     next

     dim returnValue : set returnValue = ValidateTeamSettings(WbemServices, AdapterArray)

     ' if something was returned, we need to see what the value is
     if not IsNothing(returnValue) then
          ' create a bool to track whether or not there are any warnings
          dim bNoWarning : bNoWarning = true

          ' List out the ports specified by the user
          wscript.echo "Specified ports: "
          dim adapterIndex
          for each adapterIndex in split(InternalAdapterList,",")
               ' no need to check that the index is valid since it is already done above
               wscript.echo "  " & adapterIndex+1 & ") " & AdapterSet.Name(adapterIndex)
          next

          ' Put a space after the last adapter is printed and then the heading
          ' for team warnings
          wscript.echo VbNewLine & "Team creation warnings for the above ports: "

          ' if we get a IPv6 LSO warning, tell the user that IPv6 LSO will be disabled on team
          if (hex(returnValue.ValResult) And LSO_IPv6_WARNING) = LSO_IPv6_WARNING then 
               wscript.echo "  One or more adapters does not support Large Send Offload on IPv6 connections."
               bNoWarning = false
          end if
          
          ' if we get a IPv4 LSO warning, tell the user that IPv4 LSO will be disabled on team
          if (hex(returnValue.ValResult) And LSO_IPv4_WARNING) = LSO_IPv4_WARNING then 
               wscript.echo "  One or more adapters does not support Large Send Offload on IPv4 connections."
               bNoWarning = false
          end if

          ' if we get a LSO disabled warning, tell the user that IPv4 and IPv6 LSO will be disabled on team
          if (hex(returnValue.ValResult) And LSO_DISABLED_WARNING) = LSO_DISABLED_WARNING then 
               wscript.echo "  One or more adapters has Large Send Offload disabled on IPv4 and IPv6 connections. "
               bNoWarning = false
          end if

          ' if there were no warnings, we need to specify that no warnings were found          
          if bNoWarning then
               wscript.echo "  None"
          end if
     else
          wscript.echo "Failed to validate settings for team creation."
     end if



     wscript.Quit 0
          

     
'===================================================
' Function       : ParseCommandLine
' Purpose        : Evaluates command line arguments passed to the script and determines If they are correct
' Arguments      :     
'                       
'               AdapterList   - List of adapter indices seperated by a comma
'               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 AdapterList, 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
               End If                         

               AdapterList = cmd_args(0)
               sTarget = "."
               sUserName = ""
               sPassword = ""
               ParseCommandLine = true
               Exit Function                       
          Case 4
               AdapterList = 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 & " <AdapterList> [target username password] [help]" & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    " & AdapterListString & 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
