'***************************************************************************************************************
'*                                    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_EnumerateSettings.VBS
' Version           :     1.0
'
'  Purpose :   Enumerates the given Team's settings
'                   
'
'  Arguments : Team_EnumerateSettings.vbs <TeamIndex> [target username password] [help]
'
'               <TeamIndex>  - Team to get the properties 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 Team_EnumerateSettings.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_EnumerateTeamSettings.vbs"
     Const SCRIPT_DESCRIPTION = "This script is used to enumerate all the Advanced Settings on an ANS Team"

     'Global variables
     Dim TeamIndex      'The teamIndex that is displayed to the user
     Dim Target, Username, Password
     Dim WbemServices
     Dim sError
     Dim internalTeamIndex ' internalTeamIndex is teamIndex-1 since the internal team array is 0- indexed
     
     'Check command line is correct
     If ParseCommandLine(TeamIndex, 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    
       
     dim sResult
    
    ' 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
     
     ' Subtract 1 since the user is going to give an index based on 
     ' a 1-indexed array and the adapter array is 0-indexed 
     internalTeamIndex = 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
     
    
     ' Get the team setting
     
    dim TeamSettings : set TeamSettings = GetTeamSettings(WbemServices, TeamList(InternalTeamIndex))
     
     'Check to make sure that something was returned
     if isNothing(TeamSettings) = true then
     	'msgbox "checkpoint2"
        wscript.echo "Failed to get the team settings for the team"
        wscript.quit 0
     end if
     
      ' Display the team
      wscript.Echo Cstr(TeamIndex) & ") " & TeamList(InternalTeamIndex).Caption & " Settings:" & VbNewLine

     if TeamSettings.Count > 0 then
          ' Loop through the settings and print them on the screen
          dim setting
          
          'Loop for each value and check to make sure the value is not null 
          'if the value is null then <empty> is displayed
          
          for each setting in TeamSettings
               
               	if setting.CurrentValue <> "" then
	       		wscript.echo "   " & setting.Caption & " - " & Cstr(setting.CurrentValue)                    
	        else
			wscript.echo "   " & setting.Caption & " - <empty>" 
	        end if     
               
          next
     else
          ' No settings found
          wscript.echo "   No settings found " 
     end if
          
     wscript.Quit 0
     
'===================================================
' Function       : ParseCommandLine
' Purpose        : Evaluates command line arguments passed to the script and determines If they are correct
' Arguments      :     
'                       
'               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 TeamIndex, 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
                   
                   If IsNumeric(cmd_args(0)) then
                 
                         TeamIndex = CInt(cmd_args(0))
                         sTarget = "."
                         sUserName = ""
                         sPassword = ""
                         ParseCommandLine = true
                         exit function
                   End if
                 
           Case 4
              ' Check if the first arg (index #) is not a number or if sClass/sPropertyName is and bail
              if not IsNumeric(cmd_args(0)) then
                    ParseCommandLine = false
                    exit function
              end if
                    TeamIndex = CInt(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 & " <TeamIndex> [target username password] [help]" & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    " & TeamIndexString & VbNewLine &_
                         "                   " & TeamGetIndexString & 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
