יום שבת, 4 בפברואר 2012

Network load balancing using Windows 7


Following is the script: (Save with a .vbs extension), I've marked in red the places u need to change so your script will fit your needs.
The 0.8 number means that at 0.8% network utilization, the script will change the adapter's metric and thus lower its priority.

If you just want to change the metric of your network interface from command line,
use this command : (run CMD as admin)

netsh interface ipv4 set interface "wired" metric=5

=====================================================
On Error Resume Next 

strComputer = "."
strMainNIC = "Wired"
strMainNICDevice = "Marvell Yukon 88E8056 PCI-E Gigabit Ethernet Controller"


Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\Root\CIMV2" ) 

Set colItems = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapter Where NetConnectionID = '" & strMainNIC  & "'") 

For Each objItem in colItems 
strMACAddress = objItem.MACAddress 
Wscript.Echo "MACAddress: " & strMACAddress 
Next 

Set colNetCard = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE" ) 

For Each objNetCard in colNetCard 
If objNetCard.MACAddress = strMACAddress Then 
For Each strIPAddress in objNetCard.IPAddress 
Wscript.Echo "Description: " & objNetCard.Description 
Wscript.Echo "IP Address: " & strIPAddress 
Wscript.Echo "IPConnectionMetric: " & objNetCard.IPConnectionMetric 
regValueDataMetric = objNetCard.IPConnectionMetric 
Next 
End If 
Next 

For X = 0 to 1000000000000000000 

Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\Root\CIMV2" ) 
Set colItems = objWMIService.ExecQuery _ 
("select * from Win32_PerfFormattedData_TCPIP_NetworkInterface Where Name = '" & strMainNICDevice  & "'" ) 
For Each objItem in colItems 
bytesTotal = objitem.BytesTotalPersec 
bandwidth = objItem.CurrentBandwidth 
result = FormatNumber(( bytesTotal / bandwidth) * 1000) 
output = "Main NIC utilization : " & result & " % Utilized: " & objitem.BytesTotalPersec & " " & regValueDataMetric & " Total Bandwidth: " & bandwidth 
WScript.Echo output 

NEXT 

If result >= 0.8 Then 
Wscript.Echo "Utilizing Secondary NIC"
If regValueDataMetric = "1" Then 
regValueDataMetric = "50" 
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run  "netsh interface ipv4 set interface """"Wired"""" metric=" & regValueDataMetric, 0, TRUE
Set colItems = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapter Where NetConnectionID = '" & strMainNIC  & "'") 

End if 
End If 

If result <= 0.8 Then 

If regValueDataMetric = "50" Then 
   regValueDataMetric = "1" 

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run  "netsh interface ipv4 set interface """"Wired"""" metric=" & regValueDataMetric, 0, TRUE

Set colItems = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapter Where NetConnectionID = '" & strMainNIC  & "'") 

For Each objItem in colItems 
strMACAddress = objItem.MACAddress 
Wscript.Echo "MACAddress: " & strMACAddress 
Next 

Set colNetCard = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE" ) 

End if 
End If 

WScript.Sleep 1000

Next 
==================================================================