I am trying to create some new DHCP scopes with PowerShell but I'm stuck when adding our 3 DNS servers to Option 3. I have a CSV file with all of the information and the DNS fields that I've tried are:
DNS - 1.1.1.1, 2.2.2.2, 3.3.3.3
DNS1 - 1.1.1.1
DNS2 - 2.2.2.2
DNS3 - 3.3.3.3
Import-Csv "C:\temp\DHCP.csv" | % {
Add-DhcpServerv4Scope -StartRange $_.StartRange -EndRange $_.EndRange -SubnetMask $_.SubnetMask -Name $_.ScopeName -cn $_.DHCPServer -Description $_.Description -LeaseDuration $_.LeaseDuration -State Active -WhatIf
If ($_.Router -notlike "") {
Set-DhcpServerv4OptionValue -cn $_.DHCPServer -ScopeId $_.ScopeID -OptionId 3 $_.Router -WhatIf }
If ($_.TimeServer -notlike "") {
Set-DhcpServerv4OptionValue -cn $_.DHCPServer -ScopeId $_.ScopeID -OptionId 4 $_.TimeServer -WhatIf }
If ($_.DNS -notlike "") {
$DNSString=$_.DNS1 + ", " + $_.DNS2 + ", " + $_.DNS3
#Set-DhcpServerv4OptionValue -cn $_.DHCPServer -ScopeId $_.ScopeID -DnsServer $DNS -WhatIf }
Set-DhcpServerv4OptionValue -cn $_.DHCPServer -ScopeId $_.ScopeID -OptionID 3 $DNSString -WhatIf }
Any idea how to set multiple DNS Servers in a DHCP scope with PowerShell? Any help would be greatly appreciated. Thanks.
Kyle
Three things stand out to me:
Set-DhcpServerv4OptionValue
has a special parameter for setting DNS servers, so you don't need to know the Option ID You want Option 6 for DNS (don't use Option 5). But again, see 2.
Ignore the -OptionID
parameter and use -DnsServer
instead.
This parameter is an array type, so supply the values as an array.
$dnsArray = $_.DNS1,$_.DNS2,$_.DNS3
Set-DhcpServerv4OptionValue -ComputerName $_.DHCPServer -ScopeId $_.ScopeID -DnsServer $dnsArray
Since you're doing this in bulk, you may want to use -Force
, which skips DNS server validation .
I just ran into this same thing... but in my case these scopes were not going to change much, so I didn't need to create a full script to run multiple times, just wanted to run it once to create a bunch of dhcp scopes (about 150) with options all at once. Just setting up the environment at the beginning.
With the help of a friend (because I was banging my head against a wall), this is what we (really he) came up with. Hope it helps someone out there get past the same issue:
For this example, I created a CSV file "dhcpoptions.txt" and my dhcp server name is dhcp01.my.lab
CSV File format:
ScopeId;DnsServer;DnsDomain;Router
1.20.10.0;1.19.10.50,1.19.10.51;my.lab;1.20.10.1
1.20.11.0;1.19.10.50,1.19.10.51;my.lab;1.20.11.1
Since I was basically just running this once, instead of using a script, I just used a single PS command:
import-csv .\dhcpoptions.txt -Delimiter ";" | % {Set-DhcpServerv4OptionValue -cn dhcp01 -ScopeId $_.ScopeId -DnsServer $_.DnsServer.split(',') -DnsDomain $_.DnsDomain -Router $_.Router}
Hope this helps someone out there.
Works fine with :
$dns="192.168.1.1","8.8.8.8",...
Set-DhcpServerv4OptionValue -ScopeId 192.168.1.1 -OptionId 6 -Value $dns
or directly :
Set-DhcpServerv4OptionValue -ScopeId 192.168.1.1 -OptionId 6 -Value
"192.168.1.1","8.8.8.8",...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.