'***************************************************************************************************************
'*                                    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_TestSwitchConfiguration.VBS
' Version           :     1.0
'
'  Purpose :   Script used to run the TestSwitchConfiguration method od IANet_TeamOfAdapters 
'                   
'
'  Arguments : Team_TestSwitchConfiguration.vbs <TeamIndex Command> [target username password] [help]
'
'               <TeamIndex>    - Index of the team to enumerate member adapters
'               <Command>      - Command to be executed
'               <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_TestSwitchConfiguration.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_TestSwitchConfiguration.vbs"
     Const SCRIPT_DESCRIPTION = "used to run the TestSwitchConfiguration method on IANet_TeamOfAdapters "

    'Global variables
     Dim TeamIndex, InternalTeamIndex
     Dim Command
     Dim Target, Username, Password
     Dim sResult
     Dim WbemServices
     Dim sError
     
         
     'Check command line is correct
     If ParseCommandLine(TeamIndex,Command,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 

     ' Enumerate teams
     dim TeamList
     TeamList = EnumerateTeams(WbemServices, sResult)

     ' Check that there are teams on the system
     if IsNothing(TeamList)then
          wscript.Echo "No teams found on the system."
          wscript.quit 0
     end if
     
     InternalTeamIndex = CInt(TeamIndex) - 1

     ' Verify the team specified is a valid index, bail if not
     if (InternalTeamIndex < 0) then
          wscript.echo "Invalid team index. Team index must be greater than 0"
          wscript.quit 0
     elseif (InternalTeamIndex > ubound(TeamList)) then
          wscript.echo "Team index not found: " & TeamIndex
          wscript.quit 0
     end if

     ' Display the team
      wscript.Echo Cstr(TeamIndex) & ") " & TeamList(InternalTeamIndex).Caption & " Settings:" & VbNewLine

     ' Call the modifyTeamInfo from the Library script	
    dim ret : set ret = TestSwitchConfiguration (WbemServices, TeamList(InternalTeamIndex), Command, sResult)                         

     if not isNothing(ret)   then
        
            if not isNothing(ret.Status) then    
                wscript.echo "   Status : " & ret.Status
            else
                wscript.echo "   Status : <empty>"
            end if
            
            
            if not isNothing(ret.CauseMessageId) then
	    	if Ubound(ret.CauseMessageId) < 0 then
	    		wscript.echo "   CauseMessageId : <empty>"
	        else
	    		wscript.echo "   CauseMessageId : " & (join(ret.CauseMessageId)) 
	    	end if
	    else
	        wscript.echo "   CauseMessageId : <empty>"
            end if
            
            
            if not isNothing(ret.strCause) then
            	if Ubound(ret.strCause) < 0 then
			wscript.echo "   strCause : <empty>"
	        else
            		wscript.echo "   strCause : " & (join(ret.strCause)) 
                end if
            else
                wscript.echo "   strCause : <empty>"
            end if
            
            if not isNothing(ret.SolutionMessageId) then
            	if ubound(ret.SolutionMessageId) < 0 then
            		wscript.echo "   SolutionMessageId : <empty>"
	        else
                	wscript.echo "   SolutionMessageId : " & (join( ret.SolutionMessageId)) 
                end if
            else
                wscript.echo "   SolutionMessageId : <empty>"
            end if
            
            if not isNothing(ret.strSolution) then
            	if ubound(ret.strSolution) < 0 then
            		wscript.echo "   SolutionMessage : <empty>"
            	else
            	    wscript.echo "   strSolution :  " & (join(ret.strSolution)) 
            	end if
            else
                wscript.echo "   strSolution : <empty>"
            end if
            
         else
            wscript.echo "   " & sResult
         end if
         
    wscript.Quit 0
          
     
'===================================================
'  Function : ParseCommandLine
'   Purpose : Evaluates command line arguments passed to the script and determines if they are correct
' Arguments : 
'
'             TeamIndex - Index of the team to use
'             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 TeamIndex,byref Command, byref sTarget, byref sUsername, byref sPassword)
     
          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 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 2
                              
	                'Check if the first argument is not a number 
	                if not IsNumeric(cmd_args(0)) then
	                      ParseCommandLine = false
	                      exit function
	                end if
	                
	                'check if the second argument is a number
	                if IsNumeric(cmd_args(1)) then
	                      ParseCommandLine = false
	                      exit function
	                end if
	                
	       
	                'Check for the correct command
	                if UCASE(cmd_args(1)) <> "START" and UCASE(cmd_args(1)) <> "CANCEL" and UCASE(cmd_args(1)) <> "RESULTS" then
	                    ParseCommandLine = false
	                    exit function
	                end if
	                
	                TeamIndex = CInt(cmd_args(0))
	                Command = cmd_args(1)
	                sTarget = "."
	                sUserName = ""
	                sPassword = ""
	                ParseCommandLine = true
                    Exit Function  
                              
               Case 5
                    'Check if the first argument is not a number 
	                if not IsNumeric(cmd_args(0)) then
	                      ParseCommandLine = false
	                      exit function
	                end if
	                
	                'check if the second argument is a number
	                if IsNumeric(cmd_args(1)) then
	                      ParseCommandLine = false
	                      exit function
	                end if
	                
	                
	                'Check for the correct command
	                if UCASE(cmd_args(1)) <> "START" and UCASE(cmd_args(1)) <> "CANCEL" and UCASE(cmd_args(1))<> "RESULTS" then
	                    ParseCommandLine = false
	                    exit function
	                end if
	                
	                TeamIndex = CInt(cmd_args(0))
	                Command = cmd_args(1)
	       		sTarget = cmd_args(2)
	                sUserName = cmd_args(3)
	                sPassword = cmd_args(4)
	                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 & " <TeamIndex Command> [target username password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine & VbNewLine &_
                         "    " & TeamIndexString & VbNewLine &_
                         "                   " & TeamGetIndexString & VbNewLine & VbNewLine &_
                         "    Command 	 - START or CANCEL or RESULTS " &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
