Share via


Set dhcp optionvalue (dnsserver) ondhcp scope by powershell

Question

Monday, June 1, 2020 9:09 AM

Hello all,

Please does somebody can help me? I would like to set the dhcp optionvalue 06 on many dhcp scope. I run two differnents methods scripts below, but it not work...

1st method

$dnsArray = $_.DNS1,$_.DNS2
import-csv "c:\script\DHCPScopeSicra.csv" -Delimiter ";" | % Set-DhcpServerv4OptionValue -servername $_.DHCPServer -ScopeId $_.ScopeID -DnsServer $dnsArray 

2nd method

import-csv "c:\script\DHCPScopeSicra.csv" -Delimiter ";" | % {Set-DhcpServerv4OptionValue -cn ServernameScopeId $_.ScopeId -DnsServer $_.DnsServer.split(',')} 

Thank you very much for your help by advance.

Mohamed

All replies (2)

Tuesday, June 2, 2020 10:10 AM âś…Answered

Thank you for you answer. I solved my issue the right command line is

import-csv "c:\script\CSVfile" -Delimiter ";" | % {Set-DhcpServerv4OptionValue -cn "dhcpservername" -ScopeId $_.ScopeId -OptionId 6 -value $_.DnsServer.split(',')}

and below the vsc file content:

ScopeID;DNSServer
1.1.1.1;2.2.2.2,33.33.33.3
1.1.1.1;2.2.2.2,33.33.33.3

and it works fine


Monday, June 1, 2020 3:32 PM

Not knowing what's in your CSV isn't helpful. But your first example fails for three reasons.

1. You're using "$_.DNS1" and "$_.DNS2" (presumably from your CSV) before you import the values.
2. You're missing the "{" and "}" that should surround the Foreach-Object block.
3. The cmdlet wants an array of IP Addresses, not strings, for the DnsServer parameter.

Try this:

import-csv "c:\script\DHCPScopeSicra.csv" -Delimiter ";" |
    Foreach-Object{
        $dnsArray = $_.DNS1,$_.DNS2 | Foreach-Object{[IPAddress]$_.Trim()}
        Set-DhcpServerv4OptionValue -servername $_.DHCPServer -ScopeId $_.ScopeID -DnsServer $dnsArray
    }

Your second example fails for two reasons:

1. There is a space in one of your IP Address strings
2. See #3, above.

Try this:

import-csv "c:\script\DHCPScopeSicra.csv" -Delimiter ";" |
    ForEach-Object {
        $ips = $_.DnsServer.split(',').Trim() | Foreach-Object{[IPAddress]$_}
        Set-DhcpServerv4OptionValue -cn ServernameScopeId $_.ScopeId -DnsServer $ips

Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)