Set DNS with PowerShell
Asked Answered
P

8

10

What I am trying to accomplish: I am trying to set the DNS address on my machine using powershell.

Here is my code:

$dnsserver = (,"192.168.0.5")
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Invoke-WmiMethod -Name SetDNSServerSearchOrder -ArgumentList (,$dnsserver)

I am using this as reference:

Using Invoke-WmiMethod to set the DNS servers

The problem: When I run the script, nothing changes. If I run the script and restart the machine, nothing happens. I am running the script on my local machine not remotely.

I am only wanting to add 1 DNS address.

Do I perhaps need to run as a different user or do something else special in order for this to work?

Pseudohermaphrodite answered 10/9, 2013 at 16:20 Comment(4)
do you run an elevated powershell console ?Gathers
I do not believe so. Would that entail anything more than a 'cmd > right-click "Run As Admin"'?Pseudohermaphrodite
It think you have not choose the best reference, have a look here blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/…Gathers
@Kayasax And that did it! I ran as Admin and boom. Thanks. If you want to put that in as an answer, I can accept it.Pseudohermaphrodite
T
4

managing network interfaces usualy require administrative rights, so you have to run your script in an elevaled powershell console.

Tamathatamaulipas answered 10/9, 2013 at 17:16 Comment(0)
R
8
$DC = read-host "Please enter a primary DNS"
$Internet = read-host "Please enter secondary DNS"
$dns = "$DC", "$Internet"

For powershell 4:

#I know this one is sloppy, but will get the work done.

Set-DnsClientServerAddress -InterfaceIndex 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 17, 18, 19, 20 -ServerAddresses ($DNS)
#I know this one is sloppy, but will get the work done.

For Powershell 1,2 and 3.

$Interface = Get-WmiObject Win32_NetworkAdapterConfiguration 
Write-Host "$(Get-Date -format T):Registering DNS $dns for $server" -ForegroundColor Green
$Interface.SetDNSServerSearchOrder($dns)

I plucked these off a script I wrote actually yesterday for a client that needed to set the DNS range for some-odd 200 Computers since the DC was failing.

If you are interested I can give you the whole script. Point of the script is to run in a domain, and set every computer' DNS in the domain.

Edit, Made a Better version of the executing part (PS4+)

This script should not show any errors, if it does something went wrong. Unlike previous script for ps4, it would always output several errors.

$ErrorActionPreference = "Continue"

#Set all DNS addresses needed.
write-verbose -Verbose "Set all DNS addresses needed."
$DC = "192.168.103.30"
$Internet = "8.8.8.8" #Google
$WorkRouter = "192.168.12.254"
$HomeRouter = "10.0.0.1"
$Possible = "192.168.1.1"
$Possible2 = "192.168.0.1"

#Combine addresses
write-verbose -Verbose "Combining DNS addresses."
$dns = "$DC", "$Internet", "$WorkRouter", "$HomeRouter", "$Possible", "$Possible2"

#Set network adapter ranges
write-verbose -Verbose "Setting network adapter ranges."

#Get Network adapters
write-Verbose -Verbose "Now checking available network adapters."
$Net = Get-NetAdapter | select ifIndex | ft -a | Out-File -FilePath C:/Netadapter.txt
$Net =  "C:/Netadapter.txt"

#Setting ranges to work with
$Ranges = (Get-Content $Net) -creplace "ifIndex", "" -creplace "-", "" | foreach {$_.Trim()} | Where { $_ } | Sort #| out-file C:/Netadapter.txt

#Execute DNS change
write-Warning -Verbose "Now executing DNS change to all available network adapters."
foreach ($range in $ranges)    {
Set-DnsClientServerAddress -InterfaceIndex $range -ServerAddresses ($DNS)
}

write-verbose -Verbose "DNS Settings have been altered."
Rondo answered 3/10, 2014 at 12:41 Comment(0)
B
8

This answer requires Powershell 4.

  1. Run Get-DNSClientServerAddress. In the output, look for the Interface Index of the adapter you want to change. You will need this in step 2.
  2. In an elevated prompt, run Set-DNSClientServerAddress –interfaceIndex ? –ServerAddresses ("8.8.8.8") where ? is the Interface Index of the interface you want to change DNS server address for (8.8.8.8 is Google's DNS - always a good standby, but change this to the address of whatever DNS server you want).
  3. If you want to change the interface back to 'Obtain DNS server address automatically', in an elevated prompt, run Set-DnsClientServerAddress -InterfaceIndex ? -ResetServerAddresses (Thanks to this blog for this tip), again substituting the Interface Index you looked up in step 1 for ?.
Bowyer answered 5/10, 2015 at 16:43 Comment(1)
Steps 2 and 3: Instead of param -interfaceindex you can use Get-NetAdapter -Name "<e.g. vEthernet>" | set-dnsclientserv<...> using pipe to make it better readable.Puduns
B
8

If you have one network adapter you can use Get-NetAdapter | Set-DnsClientServerAddress -ServerAddresses 8.8.8.8,8.8.4.4

If you have many adapters you need to add name, for checking names run Get-NetAdapter

Get-NetAdapter -Name "vEthernet (DockerNAT)" | Set-DnsClientServerAddress -ServerAddresses 8.8.8.8,8.8.4.4

Benedetta answered 8/8, 2018 at 12:59 Comment(1)
You can more serveraddresses comma-separated?Puduns
G
7

using the netsh.exe program to script changes to the network interfaces is a great way to automate configuring them. Changing DNS is simple:

# turn on DHCP assigned DNS servers
netsh int ip set address "Local Area Connection" dhcp

# set a static DNS entry
netsh int ip set dns "Local Area Connection" static 192.168.1.1

A few notes:

  • You would need to change "Local Area Connection" to the name of the connection you are working with. Though this is generally the default - it may just work in your case. The DNS server address would also need to be specific to your scenario.
  • Changing IP information usually requires elevated privileges, so make sure you are running PowerShell with elevated rights - by default Windows Vista and later launch PowerShell without elevating it. You will need to right click on it and choose "Run as admin".
Giliane answered 10/9, 2013 at 18:52 Comment(4)
This is also a great solution, however in my case I will not always know if "Local Area Connection" is the correct value.Pseudohermaphrodite
For what it is worth: You can't rely on it always being index=7 in your WMI query either. Either value should be looked up or configured in advance.Giliane
Also true, good catch. I have since replaced it with '-Filter IPEnabled=TRUE'. (I had been trying a few different variations and at the time of posting that's what i was working with)Pseudohermaphrodite
Can I add a second dns entry?Puduns
T
4

managing network interfaces usualy require administrative rights, so you have to run your script in an elevaled powershell console.

Tamathatamaulipas answered 10/9, 2013 at 17:16 Comment(0)
P
3
$vmDNS1 = "192.0.2.1"
$vmDNS2 = "192.0.2.2"
$dns = "$vmDNS1", "$vmDNS2"

$Interface = Get-WmiObject Win32_NetworkAdapterConfiguration 
Write-Host "$(Get-Date -format T):Registering DNS $dns for $server" -ForegroundColor Green
$Interface.SetDNSServerSearchOrder($dns)

Using this from years. Used it against many datacenters to configure DNS on virtual machines.

Pelagian answered 22/4, 2018 at 16:6 Comment(0)
E
1

This is what I use.. PS 5.1

$ipaddress = <ip>
$Gateway = <gateway IP>
$netadapter = <InterfaceAlias>
$primary = <First DNS record or comma seperated IP's>
$NetScript = @"
 Get-Netadapter -Name $netadapter | New-NetIPAddress -AddressFamily IPv4 -IPAddress $IP -PrefixLength 23 -Type Unicast -DefaultGateway $Gateway
Set-DnsClientServerAddress -InterfaceAlias 'Ethernet0' -ServerAddresses $primary
Disable-NetAdapterBinding -Name "Ethernet0" -ComponentID ms_tcpip6
Disable-NetAdapterBinding -Name "Ethernet0" -DisplayName "QoS Packet Scheduler"
"@
$NetScript = $NetScript.Replace('#netadapter#', $netadapter).Replace('#Ipaddress#', $IP).Replace('#Gateway#', $Gateway)

#Where Ethernet0 = InterfaceAlias

Use this alot in Vmware and HyperV deployments.

Evvy answered 26/6, 2018 at 20:34 Comment(0)
C
0

Set custom DNS server for IPv4 and IPv6 for every 'UP' adapter. (all customizable)

# Quad9 DNS servers
$Ipv4PrimaryDns = '9.9.9.9'
$Ipv4BackupDns = '149.112.112.112'
$Ipv6PrimaryDns = '2620:fe::fe'
$Ipv6BackupDns = '2620:fe::9'

Get-NetAdapter | Where-Object Status -eq 'Up' | ForEach-Object {
    Set-DnsClientServerAddress -InterfaceIndex $_.ifIndex -ServerAddresses $Ipv4PrimaryDns, $Ipv4BackupDns, $Ipv6PrimaryDns, $Ipv6BackupDns
}

Clear-DnsClientCache
Chambermaid answered 17/1 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.