'***************************************************************************************************************
'*                                    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_CreateTeam.VBS
' Version            :     1.0
'
'  Purpose :   Used to create a team.
'                   
'
'  Arguments : Team_CreateTeam.vbs [AdapterList TeamMode TeamName] [target username password] [help]
'
'               AdapterList   - Comma delimeted string of indexes of adapters to be teamed
'               TeamMode      - Mode to create the team with
'               TeamName      - Name to give the team   
'               <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_CreateTeam.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_CreateTeam.vbs"
     Const SCRIPT_DESCRIPTION = "Used to create a team."

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

     ' Loop through each specified adapter and if its valid, add it to the adapter array
     dim currentAdapterIndex
     for each currentAdapterIndex in split(InternalAdapterList,",")
          if ((currentAdapterIndex >= 0) AND (Cint(currentAdapterIndex) < AdapterSet.AdapterCount) )then          
               Push AdapterArray, AdapterSet.GetAdapterInstance(currentAdapterIndex).path
          else
               wscript.echo "Adapter index not found: " & currentAdapterIndex + 1
               wscript.quit 0
          end if
     next

     ' 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

     wscript.echo VbNewLine & "   Attempting to create " & TeamMode & " team with the adapters above..." & VbNewLine

     dim returnValue : returnValue = CreateTeam(WbemServices, AdapterArray, TeamModeInt, TeamName, false, sError)

     if returnValue then
          wscript.echo "   Call to create team was successful."
     else
          wscript.echo "   Call to create team failed." & VbNewLine
          if instr(UCase(sError), "TEAM") > 0 then
               wscript.echo "   " & sError
          end if     
     end if

     wscript.Quit 0
          

     
'===================================================
' Function       : ParseCommandLine
' Purpose        : Evaluates command line arguments passed to the script and determines If they are correct
' Arguments      :     
'                       
'               AdapterList - Comma delimeted string of indexes of adapters to be teamed
'               TeamMode    - Mode to create the team with
'               TeamName    - Name to give the team
'               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 TeamModeInt, byref TeamName, byref sTarget, byref sUserName, byref sPassword)

     Err.Clear
     
     '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 
          case 3
          
               if cmd_args(0) = "" OR cmd_args(1) = "" OR cmd_args(2) = "" then
                    ParseCommandLine = false
                    Exit Function
               End If 

               AdapterList = cmd_args(0) 

               If UCASE(cmd_args(1)) = "AFT" Then
                    TeamModeInt = 0
               Elseif UCASE(cmd_args(1)) = "ALB" Then
                    TeamModeInt = 1
               Elseif UCASE(cmd_args(1)) = "SLA" Then
                    TeamModeInt = 2
               Elseif UCASE(cmd_args(1)) = "8023AD" Then
                    TeamModeInt = 4
               Elseif UCASE(cmd_args(1)) = "SFT" Then
		    ' UBOUND returns the top index so the array needs to be 0 or 1 on the UBOUND
                    if UBOUND(split(AdapterList)) = 0 OR UBOUND(split(AdapterList)) = 1 then
                    	 TeamModeInt = 5
                    else
                         ParseCommandLine = false
                         exit function    
                    end if     
               else
                     ParseCommandLine = false
                     exit function
               End if
               TeamMode = UCASE(cmd_args(1))
               TeamName = cmd_args(2)
               sTarget = "."
               sUserName = ""
               sPassword = ""

               ParseCommandLine = true
               Exit Function
          case 6
               if cmd_args(0) = "" OR cmd_args(1) = "" OR cmd_args(2) = "" then
                    ParseCommandLine = false
                    Exit Function
               End If 
               
               AdapterList = cmd_args(0) 

               If cmd_args(1) = "AFT" Then
                    TeamModeInt = 0
               Elseif cmd_args(1) = "ALB" Then
                    TeamModeInt = 1
               Elseif cmd_args(1) = "SLA" Then
                    TeamModeInt = 2
               Elseif cmd_args(1) = "8023AD" Then
                    TeamModeInt = 4
               Elseif cmd_args(1) = "SFT" Then
		    ' UBOUND returns the top index so the array needs to be 0 or 1 on the UBOUND
                    if UBOUND(split(AdapterList)) = 0 OR UBOUND(split(AdapterList)) = 1 then
                         TeamModeInt = 5
                    else
                         ParseCommandLine = false
                         exit function    
                    end if
               else
                     ParseCommandLine = false
                     exit function
               End if
               
               TeamName = cmd_args(2)
               sTarget = cmd_args(3)
               sUserName = cmd_args(4)
               sPassword = cmd_args(5)

               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 TeamMode TeamName> [target username password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    " & AdapterListString  &_
                         "                   " & AdapterGetIndexString & VbNewLine & VbNewLine &_
                         "    TeamMode     - Team type to create" & VbNewLine &_
                         "                   Valid Team modes: ALB, AFT, SLA, 8023AD, and SFT" & VbNewLine & VbNewLine &_
                         "    TeamName     - Name to apply to the team" & VbNewLine & VbNewLine &_
                         "    Note:  SFT teams can only be created with 2 or fewer adapters." & 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
