$DC = read-host "Please enter a primary DNS"
$Internet = read-host "Please enter secondary DNS"
$dns = "$DC", "$Internet"
对于 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.
对于 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)
由于 DC 出现故障,我从昨天实际为需要为 200 多台计算机设置 DNS 范围的客户端实际编写的脚本中提取了这些内容。
如果你有兴趣,我可以给你整个剧本。该脚本的重点是在域中运行,并在域中设置每台计算机的DNS。
编辑,制作了执行部分的更好版本(PS4+)
如果出现问题,该脚本不应显示任何错误。与以前的 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."