Jump to content
ModemJunki

Help with change network metric using WMI methods - Windows 10

Recommended Posts

ModemJunki

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

 

Share this post


Link to post
Share on other sites
ModemJunki

A single bump, then, and then no more. I can make do with the PowerShell but I much prefer native AutoIT.

Anyone?

Share this post


Link to post
Share on other sites
Neutro

Hi,

Why not simply use this command line?

RunWait('netsh interface ipv4 set interface "Ethernet 2" metric=2')

Edited by Neutro

Share this post


Link to post
Share on other sites
ModemJunki

Thanks, Neutro.

Yes, netsh works as well, but I was looking for an WMI solution. It seems that the WMI method is restricted in Windows 10.

Share this post


Link to post
Share on other sites
ModemJunki

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!

Share this post


Link to post
Share on other sites
ripdad

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

Share this post


Link to post
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

  • Similar Content

    • colombeen
      By colombeen
      Hi guys,
      I'm trying to get some information using WMI, from the Win32_EncryptableVolume class.
      I exec my query, filter out the C-drive, but when I need more info using the objects methods, I only get 1 value back and I can't seem to retrieve the other out params that should be there.
      A very minimal version of what I'm trying to do (no error checking etc, very basic). You need to start SciTE as admin or you won't see any results in the console!
      #RequireAdmin $strComputer = @ComputerName $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\CIMV2\Security\MicrosoftVolumeEncryption") $objWMIQuery = $objWMIService.ExecQuery("SELECT * FROM Win32_EncryptableVolume WHERE DriveLetter='C:'", "WQL", 0) For $objDrive In $objWMIQuery ConsoleWrite("> " & $objDrive.GetConversionStatus() & @CRLF) ConsoleWrite("> " & $objDrive.GetConversionStatus().ConversionStatus & @CRLF) ConsoleWrite("> " & $objDrive.GetConversionStatus().EncryptionPercentage & @CRLF) Next The result from the console is : 
      > 0 > > What I'm expecting to get back is : 
      > 0 > 0 > 0 When using powershell I get this (run as admin is required!!!) : 
      PS C:\WINDOWS\system32> (Get-WmiObject -namespace "Root\cimv2\security\MicrosoftVolumeEncryption" -ClassName "Win32_Encryptablevolume" -Filter "DriveLetter='C:'").GetConversionStatus() ... ConversionStatus : 0 EncryptionFlags : 0 EncryptionPercentage : 0 ReturnValue : 0 ... All I seem to be getting is the ReturnValue when I use the method.
      I've tried this on multiple methods, always ending up with the same result
      Anyone here who has experience with this type of thing?
       
      Greetz
      colombeen
    • Daydreamer067
      By Daydreamer067
      Hi
      in windows 10 i create a new desktop.
      I want to run a script in this desktop, for exemple Mouseclick("left",x,y)
      I want the script to work in that desktop while i switch to the primary desktop.
      So far autoit clicks on the active desktop.
      Is there a way to make it run only on the other desktop ?
       
      Thank you for your response.
    • codingislife
      By codingislife
      Hi everyone, 
      I'm new to the autoit world and am trying to do something a little complicated and am having trouble developing a solution. My OS after reimaging comes up with two operating system choices during boot up, I'd like to delete the incorrect OS. I have noticed that the default OS is always the correct OS. My issue is I need a way to find the OS identifier before I can delete. I know that I  can use bcdedit to find this but it changes every time and it needs to be the correct identifier (i.e. not the Default or bootmgr). If someone knows of a way to parse through the output of bcdedit to find the identifier that would be greatly appreciated. Here is an example of what I am trying to do:
      https://scottiestech.info/2016/07/03/remove-the-choose-an-operating-system-message-when-starting-your-computer/
    • PramodR
      By PramodR
      Hello,
      i am new to AUTO IT development , I would like one help to read value from a POP up.
      i am able to read below values using inspect tool , information given below, I tried to create a script to read value but that does not help. any help is appreciated.
      Available Controls
      POPUP window Controls
      Auto Id = "UpdateNow_Btn"
      Name = Update Now
      Invoke - Button Control
      Below Controls i get when i move inspector tool to the text which i need to read. i need to read the text Name . Currently with my code i get only blank text 
      Auto Id = timeOutLb
      ClassName = TextBlock
      LegacyIAccess = GetName
      Name  = Read Value for Name
       
      My code.
       
    • Dreamfire
      By Dreamfire
      Hi,
      Since today, exe's are being flagged as having a trojan by Windows Defender (Fuery.B!cl)
      Version:  3.3.14.3 - SciTE Version 3.7.3



       

×