Update settings in network adapter

Hello All,

I am working on a script for my company that updates our VPN TAP adapter with the correct DNS settings and such.
The problem I am running into is the adapter will have a different “Interface Index” from machine to machine. If I don’t select the correct Index, it will update some settings for every adapter on the computer. What is the proper way to check for the adapters “Interface Index” and save it as a variable for use in the script?
Current Script idea:
Get-NetAdapter -InterfaceDescription “TAP-Windows Adapter V9” | Set-DnsClientServerAddress -ServerAddresses XX.XX.XX.XX,XX.XXX.XX.XX | Set-NetIPInterface -InterfaceIndex 18 -InterfaceMetric 1

Anything helps my noobie skills!
Thank you!

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

How to format code on PowerShell.org

If you want to do it on one line, then you don’t need a variable. You can use the PassThru parameter to ‘send’ the adapter down the pipeline to the Set-NetIPInterface command.

Get-NetAdapter -InterfaceDescription 'TAP-Windows Adapter V9' | 
    Set-DnsClientServerAddress -ServerAddresses 'x.x.x.x','x.x.x.x' -PassThru | 
        Set-NetIPInterface -InterfaceMetric 1

To use a variable, you assign it in the usual way:

$index = Get-NetAdapter -InterfaceDescription 'TAP-Windows Adapter V9' | Select-Object -ExpandProperty ifIndex
Set-DnsClientServerAddress -InterfaceIndex $index -ServerAddresses 'x.x.x.x'
Set-NetIPInterface -InterfaceIndex $index -InterfaceMetric 1
1 Like

I will make sure to submit code in this way going forward.

This solution worked for me! It is much appreciated sir!

1 Like