windows 使用 PowerShell 设置 DNS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18724141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set DNS with PowerShell
提问by nihiser
What I am trying to accomplish:I am trying to set the DNS address on my machine using powershell.
我想要完成的任务:我正在尝试使用 powershell 在我的机器上设置 DNS 地址。
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
使用 Invoke-WmiMethod 设置 DNS 服务器
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.
我只想添加 1 个 DNS 地址。
Do I perhaps need to run as a different user or do something else special in order for this to work?
我是否可能需要以不同的用户身份运行或做一些其他特殊的事情才能使其正常工作?
采纳答案by Lo?c MICHEL
managing network interfaces usualy require administrative rights, so you have to run your script in an elevaled powershell console.
管理网络接口通常需要管理权限,因此您必须在升级的 powershell 控制台中运行您的脚本。
回答by Goyuix
using the netsh.exe
program to script changes to the network interfaces is a great way to automate configuring them. Changing DNS is simple:
使用该netsh.exe
程序编写对网络接口的更改脚本是自动配置它们的好方法。更改 DNS 很简单:
# 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".
- 您需要更改
"Local Area Connection"
为您正在使用的连接的名称。尽管这通常是默认设置 - 它可能仅适用于您的情况。DNS 服务器地址也需要特定于您的方案。 - 更改 IP 信息通常需要提升权限,因此请确保您使用提升的权限运行 PowerShell - 默认情况下 Windows Vista 和稍后启动 PowerShell 而不提升它。您需要右键单击它并选择“以管理员身份运行”。
回答by Remy van Tour
$DC = read-host "Please enter a primary DNS"
$Internet = read-host "Please enter secondary DNS"
$dns = "$DC", "$Internet"
For powershell 4:
对于 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.
对于 Powershell 1,2 和 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.
我从昨天为一个客户端编写的脚本中摘录了这些,因为 DC 出现故障,该客户端需要为一些奇怪的 200 台计算机设置 DNS 范围。
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.
如果你有兴趣,我可以给你完整的脚本。该脚本的要点是在域中运行,并在域中设置每台计算机的 DNS。
Edit, Made a Better version of the executing part (PS4+)
编辑,制作了执行部分的更好版本(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.
如果出现错误,此脚本不应显示任何错误。与之前的 ps4 脚本不同,它总是会输出几个错误。
$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."
回答by Baodad
This answer requires Powershell 4.
这个答案需要 Powershell 4。
- 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. - 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). - 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 blogfor this tip), again substituting the Interface Index you looked up in step 1 for?
.
- 运行
Get-DNSClientServerAddress
。在输出中,查找要更改的适配器的接口索引。您将在第 2 步中用到它。 - 在提升的提示,跑
Set-DNSClientServerAddress –interfaceIndex ? –ServerAddresses ("8.8.8.8")
这里?
是要更改DNS服务器地址的接口的接口索引(8.8.8.8是谷歌的DNS -总是一个不错的待机,但将其更改为任何你想要的DNS服务器地址)。 - 如果您想将接口更改回“自动获取 DNS 服务器地址”,请在提升的提示中运行
Set-DnsClientServerAddress -InterfaceIndex ? -ResetServerAddresses
(感谢此博客提供此提示),再次将您在步骤 1 中查找的接口索引替换为?
。
回答by Bogdan Mozgovyi
If you have one network adapter you can use Get-NetAdapter | Set-DnsClientServerAddress -ServerAddresses 8.8.8.8,8.8.4.4
如果您有一个网络适配器,您可以使用 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
Get-NetAdapter -Name "vEthernet (DockerNAT)" | Set-DnsClientServerAddress -ServerAddresses 8.8.8.8,8.8.4.4
Get-NetAdapter -Name "vEthernet (DockerNAT)" | Set-DnsClientServerAddress -ServerAddresses 8.8.8.8,8.8.4.4
回答by Gajendra D Ambi
$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.
多年来使用这个。将其用于许多数据中心以在虚拟机上配置 DNS。
回答by mikedopp
This is what I use.. PS 5.1
这是我用的.. 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.
在 Vmware 和 HyperV 部署中经常使用它。