Jump to content

Help with change network metric using WMI methods - Windows 10


Recommended Posts

Hello,

In Windows 10 PowerShell, one can do this to change the metric for a NIC in Windows 10:

Get-NetAdapter | Where-Object -FilterScript {$_.InterfaceAlias -Eq "Ethernet 2"} | Set-NetIPInterface -InterfaceMetric 2

I know I can script the above PowerShell line (and it works!), but I wanted to try something I hadn't done before after looking into jguinch's most excellent Network configuration UDF. I wanted to make use of the SetIPConnectionMetric method in the WMI classes. There is an example VBscript here but this is not for Windows 10. Using AutoIT would also give better control over capturing error return codes than with PowerShell.

But I cannot get my script to work! The return from SetIPConnectionMetric() is 0, which would indicate success. Yet the change does not happen. I also tried WMI methods using .put_ but this fails.

Anyone more experienced than I have ideas to make this work?

#RequireAdmin

_SetNicInterfaceMetric2("Ethernet 2", "2")

Func _SetNicInterfaceMetric2($NIC_NAME, $METRIC)
    Local $s_setIndx = 0
    $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")

    $colNICItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & $NIC_NAME & "'", "WQL")
    If IsObj($colNICItems) Then
        For $objItem In $colNICItems
            $s_nicIndex = $objItem.Index
        Next

        ConsoleWrite("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'" & @CRLF)
        $colNAC = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'", "WQL")

        If IsObj($colNAC) Then
            For $objNetCard In $colNAC
                If $METRIC <> $objNetCard.IPConnectionMetric Then
                    ConsoleWrite("Metric was set to " & $objNetCard.IPConnectionMetric & ". Setting to " & $METRIC & "." & @CRLF)
                    $s_isSet = $objNetCard.SetIPConnectionMetric($METRIC)
                    ConsoleWrite("SetIPConnectionMetric Result = " & $s_isSet & @CRLF)
                Else
                    ConsoleWrite("Metric is already set to " & $METRIC & @CRLF)
                EndIf
            Next
        EndIf
    EndIf
EndFunc   ;==>_SetNicInterfaceMetric2

 

Always carry a towel.

Link to comment
Share on other sites

Link to comment
Share on other sites

Not absolutely. :)

I only wanted to make it work because it seems to work in older versions of Windows and I like to solve problems.

So it seems in Windows 10 this simply does not work with WMI.

I can deal with using PowerShell or netsh. I guess the end result is all that matters.

Thanks for the input!

Always carry a towel.

Link to comment
Share on other sites

Does this work for you? If yes, does "Ethernet 2" exist in the NetConnectionID list,  and does it have the correct Index number associated with it?

#RequireAdmin
;
Opt('MustDeclareVars', 1)
;
Local $_objError = ObjEvent('AutoIt.Error', '_objErrorHandler')
;
_WMI_NetConnectionID()
If @error Then
    MsgBox(16, '', 'Object Error -> Line 15')
EndIf
Exit
;
Func _WMI_NetConnectionID()
    Local $objWMI = ObjGet('Winmgmts:{ImpersonationLevel=Impersonate,AuthenticationLevel=PktPrivacy,(Debug)}!\\.\root\cimv2')
    Local $objItems = $objWMI.ExecQuery('SELECT * FROM Win32_NetworkAdapter')
    If @error Or $objItems.Count = 0 Then
        Return SetError(1)
    EndIf
    ;
    Local $nIndex, $sNetConnectionID, $str = ''
    ;
    For $objItem In $objItems
        $nIndex = $objItem.Index
        $sNetConnectionID = $objItem.NetConnectionID
        ;
        If Not StringLen($sNetConnectionID) Then
            $sNetConnectionID = 'ID Not Found'
        EndIf
        $str &= 'Index #' & $nIndex & ' = ' & $sNetConnectionID & @CRLF
    Next
    MsgBox(0, 'Results', $str)
EndFunc
;
Func _objErrorHandler($_objError)
    $_objError.Clear
    Return SetError(1)
EndFunc
;

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...