'***************************************************************************************************************
'*                                    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_Enumerate.VBS
' Version           :     1.0
'
'  Purpose : Print a list of installed teams in the system.
'
'  Arguments : Team_Enumerate.vbs [target username password, help]
'
'              (no arguments) - defaults to local computer
'              <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_Enumerate.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_Enumerate.vbs"
     Const SCRIPT_DESCRIPTION = "Enumerates teams of adapters in a machine"

     'Global variables
     Dim Target, Username, Password
     Dim WbemServices
     Dim Adapters
     Dim sError
     
     'Check command line is correct
     If ParseCommandLine(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
     dim TeamList
     TeamList = EnumerateTeams(WbemServices, sResult)

     if not IsNothing(TeamList)then
          wscript.echo "Installed teams:" & VbNewLine
          dim count 
          for count = 0 to ubound(TeamList)
             Wscript.Echo "   " & Cstr(count + 1) & ") " & TeamList(count).name
          next
     else
          wscript.Echo "No teams found"
     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 Target, byref Username, byref Password)
     
          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 0     ' no arguments defaults to local machine
                    Target = "."
                    UserName = ""
                    Password = ""
                    ParseCommandLine = true
                    exit function
               Case 1     
                    if instr(LCase(cmd_args(0)), "help") >= 0 then
                         ParseCommandLine = false
                         Exit Function
                    end if                         
               Case 3 ' the first argument cannot be 'local'
                    Target = cmd_args(0)
                    UserName = cmd_args(1)
                    Password = cmd_args(2)
                    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 & " [target username password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine &_
                         "    None required" & 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
