;; ;;=====================================================================================----- ;; ;;FUNCTION IPConfig() ;; ;;ACTION Gets/Sets the IP Configuration for a specified interface ;; ;;AUTHOR Glenn Barnas ;; The error code translation was blatently taken from ;; Allen Powell's SetIPOptions UDF (developed by Rich H.) ;; ;;VERSION 1.0 - 2010/09/25 ;; ;;HISTORY 1.0 - 2010/09/25 - Initial Release ;; ;;SYNTAX IPConfig(NICID [, data]) ;; ;;PARAMETERS NICID - REQUIRED - String ;; - The identity of the NIC to get or set. This can be the MAC address ;; or the NetConnectionID. The NetConnectionID is the "name" of the ;; interface. ;; ;; Data - OPTIONAL - Array ;; - An array of configuration parameters. If the is present, the values ;; are set, otherwise an array array of current values is populated from ;; the current configuration and returned. Items marked (CSL) are ;; Comma-Separated Lists. ;; ;; Array Format: ;; 0. DHCP Enable (Bool) ;; 1. IP Address (CSL) ;; 2. Netmask (CSL) ;; 3. Gateway (CSL) ;; 4. WINS Server List (CSL) ;; 5. DNS Server List (CSL) ;; 6. DNS Domain Name ;; 7. DNS Domain Suffix Search Order (CSL) ;; ;;REMARKS When setting values, if DHCP Enable is true, elements 1-6 are ignored ;; as they are generally set from DHCP. The IP Default Route is always ;; cleared when changing settings, as persistent default routes will not ;; otherwise be changed without a restart. ;; ;;RETURNS Array if GET operation, Int if SET - (1=success) ;; ;;DEPENDENCIES WMI ;; ;;TESTED WITH W2K, WXP, W2K3, Vista, W2K8, Win7 ;; ;;EXAMPLES None ; Function IPConfig($_NicId, OPTIONAL $_aSettings) Dim $_oWMIService ; WMI Service pointer Dim $_oItem, $_cItems ; items collection, object pointer Dim $_ExitStatus ; summary of error info Dim $_T ; temp var Dim $_Param ; parameter enumeration var Dim $_Errors, $_Err ; error returned from Execute Dim $_I1, $_I2, $_I3, $_I5, $_I7 ; IP setting values $_Errors=1,3010,64,8256,65,4985,66,5894,67,8407,68,9553,69,3951,70,9552,71,9552,72,5762,73,3730, 74,3730,75,9616,76,3960,77,3,78,266,79,1338,80,9851,81,4985,82,4985,83,4985,84,9851,85,9851, 86,4985,87,4985,88,1707,89,1760,90,1734,91,5,92,2501,93,1760,94,1761,95,2186,96,2186,97,1717, 98,4985,100,4985 If UBound(Split($_NicId, '-')) = 7 $_NicId = Join(Split($_NicId, '-'), ':') ; convert dash-delimited MAC addresses EndIf If UBound(Split($_NicId, ':')) <> 7 ; got an interface name, translate it to the corresponding MAC address $_oWMIService = GetObject('winmgmts:\\.\root\cimv2') $_cItems = $_oWMIService.ExecQuery('Select * from Win32_NetworkAdapter',,48) For Each $_oItem in $_cItems If $_oItem.NetConnectionID = $_NicId $_NicId = $_oItem.MACAddress ; replace the ID with the MAC address EndIf Next $_oWMIService = 0 $_oItem = 0 EndIf ; Get or Set the IP Configuration If VarType($_aSettings) < 8192 ; empty / null array - GET ReDim $_aSettings[9] $_oWMIService = GetObject('winmgmts:\\.\root\cimv2') If @ERROR Exit @ERROR EndIf ; exit now if error $_cItems = $_oWMIService.ExecQuery('Select * from Win32_NetworkAdapterConfiguration where IPEnabled=-1') $_ExitStatus = 2 ; prepare to report not found For Each $_oItem in $_cItems If $_oItem.MACAddress = $_NicId $_ExitStatus = 0 ; found a match ; DHCP Enabled Flag $_aSettings[0] = $_oItem.DHCPEnabled ; IP Address $_aSettings[1] = Join($_oItem.IPAddress, ',') ; Netmask $_aSettings[2] = Join($_oItem.IPSubnet, ',') ; Gateway $_aSettings[3] = Join($_oItem.DefaultIPGateway, ',') ; WINS Server List $_T = IIf($_oItem.WINSSecondaryServer, ',' + $_oItem.WINSSecondaryServer, '') $_aSettings[4] = $_oItem.WINSPrimaryServer + $_T ; DNS Server List $_aSettings[5] = Join($_oItem.DNSServerSearchOrder, ',') ; DNS Domain Name $_aSettings[6] = $_oItem.DNSDomain ; DNS Domain Suffix Search Order $_aSettings[7] = Join($_oItem.DNSDomainSuffixSearchOrder, ',') ; future expansion options... EndIf Next $IPConfig = $_aSettings Exit $_ExitStatus Else ; apply the defined configuration ReDim Preserve $_aSettings[9] ; assure proper format $_oWMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2') If @ERROR ; exit now if error instantiating WMI $IPConfig = 0 Exit @ERROR EndIf $_aSettings[0] = IIf(Val($_aSettings[0]), 'EnableDHCP', '') ; define proper value $_cItems = $_oWMIService.ExecQuery('Select * from Win32_NetworkAdapterConfiguration where IPEnabled=-1') For Each $_oItem in $_cItems If $_oItem.MACAddress = $_NicId If $_aSettings[0] ; DHCP is on, process minimal IP configuration $_aSettings[1] = '' ; IP Address $_aSettings[2] = '' ; Netmask $_aSettings[3] = 'SetGateways()' ; Gateway $_aSettings[4] = 'SetWINSServer("","")' ; WINS Server List $_aSettings[5] = 'SetDNSServerSearchOrder' ; DNS Server List $_aSettings[6] = 'SetDNSDomain' ; DNS Domain Name Else $_I1 = Split($_aSettings[1], ',') ; ip address as array $_I2 = Split($_aSettings[2], ',') ; netmask as array $_I3 = Split($_aSettings[3], ',') ; gateway as array $_I5 = Split($_aSettings[5], ',') ; DNS servers as array $_aSettings[1] = 'EnableStatic(' + Chr(36) + '_I1,' + Chr(36) + '_I2)' $_aSettings[2] = '' $_aSettings[3] = 'SetGateways(' + Chr(36) + '_I3)' If $_aSettings[4] If Not InStr($_aSettings[4], ',') $_aSettings[4] = $_aSettings[4] + ',' EndIf $_aSettings[4] = 'SetWINSServer(' + $_aSettings[4] + ')' EndIf $_aSettings[5] = 'SetDNSServerSearchOrder(' + Chr(36) + '_I5)' $_aSettings[6] = 'SetDNSDomain(' + $_aSettings[6] + ')' EndIf ; simply using the "SetGateways() command does not clear persistent default routes. ; run Route Delete 0.0.0.0 to clear the persistent routes before changing the IP settings Shell '%COMSPEC% /c Route Delete 0.0.0.0 2>NUL: 1>NUL:' ; clear default (persistent) routes For $_Param = 0 to 6 If $_aSettings[$_Param] $_T = Execute(Chr(36) + "_Err = " + Chr(36) + "_oItem." + $_aSettings[$_Param]) If $_Err Exit $_Errors[AScan($_Errors,$_Err)+1] EndIf EndIf Next $IPConfig = 1 EndIf Next ; handle the non adapter-specific setting(s) $_oItem = 0 $_I7 = Split($_aSettings[7], ',') ; completion domain list as array $_oItem = $_oWMIService.Get('Win32_NetworkAdapterConfiguration') $_T = $_oItem.SetDNSSuffixSearchOrder($_I7) If @ERROR Exit @ERROR EndIf EndIf Exit 0 EndFunction