Share via


Set DNS to automatic across multiple NIC's

Question

Sunday, October 12, 2014 3:03 PM

I have a client with 100(ish) computers. Whoever their IT firm was before us has manually set DNS on most of these computers. I would like to set all NICs on all computers to dynamic, and pull the DNS server address from DHCP server. The problem is I can't use netsh because it would require the network adapter name. There are multiple NICs on the computers, some are named Ethernet, some are named Local Area Connection, ect. 

Is there a way to script this with powershell? I know I can use

Set-DnsClientServerAddress –InterfaceIndex ? -ResetServerAddressesbut I need the interface index, which will be different across various computers. Can I pipe the output of get-netadapter (which shows if index) and input all of them?Any help is appreciated. Thank you!

All replies (8)

Monday, October 13, 2014 2:32 PM ✅Answered

I have a client with 100(ish) computers. Whoever their IT firm was before us has manually set DNS on most of these computers. I would like to set all NICs on all computers to dynamic, and pull the DNS server address from DHCP server. The problem is I can't use netsh because it would require the network adapter name. There are multiple NICs on the computers, some are named Ethernet, some are named Local Area Connection, ect. 

Is there a way to script this with powershell? I know I can use

Set-DnsClientServerAddress –InterfaceIndex ? -ResetServerAddressesbut I need the interface index, which will be different across various computers. Can I pipe the output of get-netadapter (which shows if index) and input all of them?Any help is appreciated. Thank you!

Yes.  Kind of like this:

Get-NetAdapter | Set-DnsClientServerAddress -ResetServerAddresses

I had issues with that in a virtual machine, but it worked fine on a physical machine.

Get-NetAdapter outputs a CIM instance of each adapter, which you can directly feed to Set-DnsClientServerAddress.  You could also use a foreach-object to iterate through the collection and specify the index or alias, but that is less elegant.


Sunday, October 12, 2014 8:04 PM

Whatever method you will use, you will need interface names. You can get names from simple ipconfig /all command that is run locally. I used remote exec function from Sysinternals to retrieve this information. To get info from log output regular expressions will do the job. When you have info on nic names, then you can change NIC setting by any method (PS, netsh, ...). Alternatively you can enumerate NICs on every computer and assign new properties. Recommended method is based on MAC address reservation.

Rgds

Milos


Monday, October 13, 2014 6:19 AM

Hello,

You can run something like the below, this will create an array of all DNS Client Server Addresses, and loop through the array resetting the server addresses for all interfaces.

$Array = Get-DnsClientServerAddress
foreach($element in $Array)
    {
        Set-DnsClientServerAddress -InterfaceIndex $($element).InterfaceIndex -ResetServerAddresses
    }

Regards

Michael


Monday, October 13, 2014 10:51 PM

I have a client with 100(ish) computers. Whoever their IT firm was before us has manually set DNS on most of these computers. I would like to set all NICs on all computers to dynamic, and pull the DNS server address from DHCP server. The problem is I can't use netsh because it would require the network adapter name. There are multiple NICs on the computers, some are named Ethernet, some are named Local Area Connection, ect. 

Is there a way to script this with powershell? I know I can use

Set-DnsClientServerAddress –InterfaceIndex ? -ResetServerAddressesbut I need the interface index, which will be different across various computers. Can I pipe the output of get-netadapter (which shows if index) and input all of them?Any help is appreciated. Thank you!

Yes.  Kind of like this:

Get-NetAdapter | Set-DnsClientServerAddress -ResetServerAddresses

I had issues with that in a virtual machine, but it worked fine on a physical machine.

Get-NetAdapter outputs a CIM instance of each adapter, which you can directly feed to Set-DnsClientServerAddress.  You could also use a foreach-object to iterate through the collection and specify the index or alias, but that is less elegant.

This does work! Thank you! 


Tuesday, October 14, 2014 12:47 AM

I have a client with 100(ish) computers. Whoever their IT firm was before us has manually set DNS on most of these computers. I would like to set all NICs on all computers to dynamic, and pull the DNS server address from DHCP server. The problem is I can't use netsh because it would require the network adapter name. There are multiple NICs on the computers, some are named Ethernet, some are named Local Area Connection, ect. 

Is there a way to script this with powershell? I know I can use

Set-DnsClientServerAddress –InterfaceIndex ? -ResetServerAddressesbut I need the interface index, which will be different across various computers. Can I pipe the output of get-netadapter (which shows if index) and input all of them?Any help is appreciated. Thank you!

Yes.  Kind of like this:

Get-NetAdapter | Set-DnsClientServerAddress -ResetServerAddresses

I had issues with that in a virtual machine, but it worked fine on a physical machine.

Get-NetAdapter outputs a CIM instance of each adapter, which you can directly feed to Set-DnsClientServerAddress.  You could also use a foreach-object to iterate through the collection and specify the index or alias, but that is less elegant.

It only seams to work on Windows 8.1 computers. Is there a method for Windows 7? 


Tuesday, October 14, 2014 1:02 AM

It only seams to work on Windows 8.1 computers. Is there a method for Windows 7? 

Well you said you wanted to use Set-DnsClientServerAddress.  Try using the wmi interface instead.

Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" |
ForEach-Object { $_.EnableDHCP() }


Tuesday, October 14, 2014 1:10 AM

It only seams to work on Windows 8.1 computers. Is there a method for Windows 7? 

Well you said you wanted to use Set-DnsClientServerAddress.  Try using the wmi interface instead.

Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" |
ForEach-Object { $_.EnableDHCP() }

Yes I apologize. The wmi interface did not work for me either. I'll keep experimenting. Thanks again. 


Tuesday, October 14, 2014 1:13 AM

It only seams to work on Windows 8.1 computers. Is there a method for Windows 7? 

Well you said you wanted to use Set-DnsClientServerAddress.  Try using the wmi interface instead.

Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" |
ForEach-Object { $_.EnableDHCP() }

Yes I apologize. The wmi interface did not work for me either. I'll keep experimenting. Thanks again. 

get-member is your friend then