'***************************************************************************************************************
'*                                    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              :     Vlan_ModifyVlanInfoOnAdapter.VBS
' Version           :     1.0
'
'  Purpose :   Script used to modify Information of Vlan on Adapter
'                   
'
'  Arguments : Vlan_ModifyVlanInfoOnAdapter.vbs <AdapterIndex VlanID SettingName SettingValue> [target username password] [help]
'
'               <AdapterIndex> - Unique Adapter Identifier
'               <VlanID>    - Unique VLAn Identifier
'               <SettingName>     - VLAN Number or Caption
'               <SettingValue>    - new value for VLANNumber or VLANName 
'               <remote>    - the name of a remote computer; requires the following:
'               <username>  - user name
'               <password>  - password
'
'
'  Instructions for use:
'  =====================
'  1) Execute script by invoking "cscript Vlan_ModifyVlanInfoOnAdapter.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 = "Vlan_ModifyVlanInfoOnAdapter.vbs"
     Const SCRIPT_DESCRIPTION = "Script used to modify Information on an adapter"

     'Global variables
     Dim Target, Username, Password
     Dim WbemServices
     Dim sError
     
     Dim sResult
     Dim AdapterIndex
     Dim InternalAdapterIndex
     Dim SettingName, SettingValue
     Dim VlanID
     
     Dim AdapterSet
     
     'Check command line is correct
     If ParseCommandLine(AdapterIndex,VlanID,SettingName,SettingValue,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 "FAIL. " & 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 Adpater index. Adapter index must be greater than 0"
          wscript.quit 0
     elseif (InternalAdapterIndex > AdapterSet.AdapterCount) then
          wscript.echo "Adapter index not found: " & InternalAdapterIndex
          wscript.quit 0
     end if
     
     dim adapter : set adapter = AdapterSet.GetAdapterInstance(InternalAdapterIndex)

     ' Check that an adapter was returned
     if IsNothing(adapter) then
     
          wscript.echo "Failed to get adapter instance"
          wscript.quit 0
     end if
     

     ' Display the team
      wscript.Echo adapterIndex & ") " & adapter.Name & " "& " VLAN" & VlanID & " : Setting:" & VbNewLine

     if ModifyVlanInfo(WbemServices, adapter.DeviceInstance, VlanID, SettingName,SettingValue, sError) then
          wscript.echo "   Successfully Modified the setting : " & SettingName
     else
          wscript.echo "   " & sError
     end if


     wscript.Quit 0

     
'===================================================
' Function       : ParseCommandLine
' Purpose        : Evaluates command line arguments passed to the script and determines If they are correct
' Arguments      :     
' 
'               AdapterIndex - Unique Adapter Index               
'               VlanID - Unique VLAN Number
'               SettingName - Setting for which the value has to be changed
'               SettingValue - New value that needs to be added             
'               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 AdapterIndex, byref VlanID,byref SettingName,byref SettingValue,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 4
             ' Check if the first arg (index #) is not a number 
             if not IsNumeric(cmd_args(0)) then
                 ParseCommandLine = false
                 Exit Function
             End If
             
             ' Check if the Second arg (index #) is not a number 
             if not IsNumeric(cmd_args(1)) then
                 ParseCommandLine = false
                 Exit Function
             End If
             
             'Check if the third and the forth argumen is not null
             
             if isNothing(cmd_args(2)) or isNothing(cmd_args(3)) then
                 ParseCommandLine = false
                 Exit Function
             End IF
                  
             'Verify the if the modification is to be done for vlannumber or caption only 
               
            if LCase(cmd_args(2)) <> "vlanname" AND LCase(cmd_args(2)) <> "vlannumber" then
                  ParseCommandLine = false
                 Exit Function
            end if
                
            'if the modification is for vlannumber, then verify the given value for the mode is a valid one.
    
            if LCase(cmd_args(2)) = "vlannumber" then
                      
                  'check to verify the settingvalue is valid
          if not IsNumeric(cmd_args(3)) AND LCase(cmd_args(3)) <> "untagged" then
               ParseCommandLine = false
               Exit Function
          end if
                     
                if(LCase(cmd_args(3)) <> "untagged")then     
                     if cmd_args(3) < 1 or cmd_args(3) > 4094 then
                           ParseCommandLine = false
                         exit function
                      end if
                  end if
            end if
               AdapterIndex = CInt(cmd_args(0))
             VlanID = CInt(cmd_args(1))
             SettingName = cmd_args(2)
                 SettingValue = cmd_args(3)
             sTarget = "."
             sUserName = ""
             sPassword = ""
             ParseCommandLine = true
            Exit Function  
                              
        Case 7
            ' Check if the first arg (index #) is not a number 
             if not IsNumeric(cmd_args(0)) then
                 ParseCommandLine = false
                 Exit Function
             End If
             
             ' Check if the Second arg (index #) is not a number 
             if not IsNumeric(cmd_args(1)) then
                 ParseCommandLine = false
                 Exit Function
             End If
             
             'Check if the third and the forth argumen is not null
             
             if isNothing(cmd_args(2)) or isNothing(cmd_args(3)) then
                 ParseCommandLine = false
                 Exit Function
             End IF
             
            'Verify the if the modification is to be done for vlannumber or caption only 
               
            if LCase(cmd_args(2)) <> "vlanname" AND LCase(cmd_args(2)) <> "vlannumber" then
                  ParseCommandLine = false
                 Exit Function
            end if
                
            'if the modification is for vlannumber, then verify the given value for the mode is a valid one.
    
            if LCase(cmd_args(3)) = "vlannumber" then
                      
                  'check to verify the settingvalue is valid
                  if not IsNumeric(cmd_args(3)) or LCase(cmd_args(3)) <> "untagged" then
                      ParseCommandLine = false
                     Exit Function
                 end if
                     
                if(LCase(cmd_args(3)) <> "untagged")then     
                     if cmd_args(3) < 1 or cmd_args(3) > 4094 then
                           ParseCommandLine = false
                         exit function
                      end if
                  end if
            end if
            AdapterIndex = CInt(cmd_args(0))                     
            VlanID = CInt(cmd_args(1))
            SettingName = cmd_args(2)
              SettingValue = cmd_args(3)
            sTarget = cmd_args(4)
            sUserName = cmd_args(5)
            sPassword = cmd_args(6)
            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 VlanID SettingName SettingValue> [target username password] [help]" & VbNewLine & VbNewLine &_
                         "  Required: " & VbNewLine & VbNewLine &_
                         "    " & AdapterIndexString & VbNewLine &_
                         "                   " & AdapterGetIndexString & VbNewLine & VbNewLine &_
                         "    " & VlanIdString & VbNewLine &_
                         "             	   Valid IDs: 1-4094 or 'Untagged'" & VbNewLine & VbNewLine &_
                         "    " & SettingNameString & "         	   ("& "'VLANName' or 'VLANNumber')" & VbNewLine & VbNewLine &_
                         "    " & SettingValueString  & 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
