Skip to content

Instantly share code, notes, and snippets.

@purwandi
Last active March 16, 2017 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purwandi/6439d4fd637d2847bbf8fec08d3a3f52 to your computer and use it in GitHub Desktop.
Save purwandi/6439d4fd637d2847bbf8fec08d3a3f52 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment