How to Append DNS Servers with Set-DhcpServerv4OptionValue?

0
6
Asked By TechWizard42 On

I'm working with a DHCP scope and I already have the DNS servers set as 10.1.2.2 and 10.1.2.3. However, when I try to add new DNS addresses (10.2.2.3 and 10.2.2.4) using the Set-DhcpServerv4OptionValue command, it completely overwrites the existing ones instead of appending to them. Here's the script I'm using:

$dnsArray = "10.2.2.3","10.2.2.4"
Set-DhcpServerv4OptionValue -ComputerName "dhcp01" -ScopeId "1.1.1.0" -DnsServer $dnsArray

I want to append those new addresses instead of losing the current ones. How can I achieve that?

2 Answers

Answered By CodeNinja88 On

What you're experiencing is expected since the command starts with 'Set', which means it's replacing the current values instead of adding to them. You should first read the existing DNS values, then merge your new addresses into that list, and finally set it again to include everything. Just updating the current value is the way to go!

Answered By DebuggingDiva On

You're correct about needing to get the current values first before adding new ones. Use Get-DhcpServerv4OptionValue to fetch what's currently set, then combine that with your new DNS addresses and use Set-DhcpServerv4OptionValue to save it all back. It sounds more complicated than it is!

ScriptMaster99 -

Got it! I made some changes to my script based on your suggestions. Here’s what I’ve got now:

$computerName = "DHCP01"
$scopeId = "1.2.3.0"
$newDnsServers = "10.1.1.11", "10.2.2.11"

$scopeIds = "1.2.3.0"

foreach ($scopeId in $scopeIds){
$currentOption = Get-DhcpServerv4OptionValue -ComputerName $computerName -ScopeId $scopeId -OptionId 6
$existingDnsServers = @()
if ($currentOption.Value) { $existingDnsServers = $currentOption.Value }
$mergedDnsServers = $existingDnsServers + $newDnsServers | Select-Object -Unique
Set-DhcpServerv4OptionValue -ComputerName $computerName -ScopeId $scopeId -DnsServer $mergedDnsServers -Force
}

Does this look good?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.