'***************************************************************************************************************
'*                                    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_SetTeamMemberAdapterPriority.VBS
' Version           :     1.0
'
'  Purpose : Script is used to change the priority of the adapter from secondary to primary
'
'  Arguments : Team_SetTeamMemberAdapterPriority.vbs <AdapterIndex TeamIndex> [target username password, help]
'
'              <AdapterIndex>    - Unique Adapter Identifier 
'              <AdapterPriority> - Priority value
'              <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_SetTeamMemberAdapterPriority.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_SetTeamMemberAdapterPriority.vbs"
     Const SCRIPT_DESCRIPTION = "Changes the adapter priority"

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

     ' get the adapter instance and check if it is a valid adapter instance
     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     
     
     InternalAdapterIndex = AdapterIndex - 1
     
     ' 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
     
     dim PriorityObj
     PriorityObj = SetTeamMemberAdapterPriority(WbemServices,adapter, AdapterPriority, sResult)
     
      wscript.echo CStr(AdapterIndex) & ") " & adapter.Name & "  " & VbNewLine
     
     if PriorityObj = true then
        wscript.echo "   Successfully set Priority to : " & ucase(AdapterPriority)
     else
        wscript.echo "   Failure : " & sResult
     end if
        
     wscript.Quit 0
'===================================================
'  Function : ParseCommandLine
'   Purpose : Evaluates command line arguments passed to the script and determines if they are correct
' Arguments : 
'             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 AdapterIndex, byref AdapterPriority, 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
                    
                    if not IsNumeric(cmd_args(0)) then
                        ParseCommandLine = false
                        Exit Function
                    end if
                    
                    
                    
                    if IsNumeric(cmd_args(1)) then
                        ParseCommandLine = false
                        Exit Function
                    end if
                    
                    
                    
                    if ucase(cmd_args(1)) <> "PRIMARY" and ucase(cmd_args(1)) <> "SECONDARY" then
                        ParseCommandLine = false
                        Exit Function
                    end if
                    
                    AdapterIndex = CInt(cmd_args(0))
                    AdapterPriority = cmd_args(1)
                    sTarget = "."
                    sUserName = ""
                    sPassword = ""
                    ParseCommandLine = true
                    exit function
               Case 5
                    if not IsNumeric(cmd_args(0)) then
                        ParseCommandLine = false
                        Exit Function
                    end if
                    
                    if IsNumeric(cmd_args(1)) then
                        ParseCommandLine = false
                        Exit Function
                    end if
                    
                    if cmd_args(1) <> "PRIMARY" and cmd_args(1) <> "SECONDARY" then
                        ParseCommandLine = false
                        Exit Function
                    end if
                    
                    AdapterIndex = CInt(cmd_args(0))
                    AdapterPriority = 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 & " <AdapterIndex AdapterPriority> [target username password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    " & AdapterIndexString & VbNewLine &_
                         "                   " & AdapterGetIndexString & VbNewLine & VbNewLine &_
                         "    " & SettingNameString  &_
                         "   		   Valid Priority : PRIMARY or SECONDARY" &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
      End If
      
    end function
