Link to home
Start Free TrialLog in
Avatar of Patrick G
Patrick G

asked on

DHCP Scope Change by Name or Exclusion (DNS)

Hello Experts!

I'll preface this post by saying I am NOT a PowerShell expert by any stretch of the imagination, but I need to get this script done fairly quickly and would greatly appreciate some help.

Our environment has about 6 DHCP scopes at each site (about 125 sites). Each site has a dhcp server.

I need to write a script to change the DNS servers on all the scopes except for one.

All scope names are uniform, but they all have unique ranges.

Instead of having to plug in all the ScopeID variables for every scope using a different command (apparently, I also can't stack multiple ScopeID's in the same command) using the following:

Set-DhcpServerv4OptionValue -ComputerName "srv1234.blah.net" ,-ScopeID, 123.45.6.0, etc -DnsServer 123.456.789.1, etc

Open in new window


Is there a way to simply use scope names? Obviously, simply substituting -ScopeID for -Name isn't valid, since even though it's listed in the output of format-list, it's not a unique variable.

Or use a command like
Set-DhcpServerv4OptionValue -ComputerName "srv1234.blah.net"  -DnsServer 123.456.789.1

Open in new window


But with an exclusion for the scope I don't want?

Thanks!!
Avatar of kevinhsieh
kevinhsieh
Flag of United States of America image

You have hundreds of scopes. What is causing you to change a DNS server for them?

Pro tip. I don't set DNS on a per scope basis in general. It is set per server.

Second pro tip. Don't ever change your DNS servers. I use the same IP addresses in 2020 as I did in 2005 for my DNS servers. Of course my DNS servers have been replaced many times, but I keep using the same IP addresses. I am also looking at possibly moving them behind a load balancer, but then the load balancer will take the IP address.

I am not a powershell expert either.
Avatar of Patrick G
Patrick G

ASKER

Thank you for your reply, but I honestly don't think the reason we are changing scopes is relevant.

I can and will write separate commands 5x for each server if I have to, but I was hoping there was an easier way that I'm not thinking of that doesn't involve creating a GPO. If there's not, than so be it.

I appreciate your reply, and should you have any suggestions which relate directly to the question or other methods which may accomplish the desired outcome, please feel free to share.



 
You should be able to put your list of servers into a variable holding an array:

About Variables

Then loop this array and run your command for each iteration. Something like:

Set-DhcpServerv4OptionValue -ComputerName $NextComputer -DnsServer 123.456.789.1

Open in new window

Thank you so much! This seems like it may work!!

So, since the only unique factor is the third octet in a given vlan (ex. all data vlan scopes are .0, all phones are .100, etc, with a change in the third octet for the last 2 vlan scopes), can I stick those values in a csv file or something and have it reference each with a location code?

Sorry, again, haven't done a whole lot of scripting until now, so still getting my sea legs.
I'm sure you can do that, though I have never done it myself, but PowerShell can do almost anything, and you should be able to look up the bits and pieces needed - how to read a csv file and do something line by line read.

Also, it should be easy to check out carefully, as you can just print the finished command lines during tests until you can verify 100% that they will be safe to execute.
Thanks again Gustav! I'll do some research and give it a shot. It's not perfect, but I'll take what I can get--plus I learned something I didn't know, which is always a win!


ASKER CERTIFIED SOLUTION
Avatar of Craig Beck
Craig Beck
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, this looks great, and I think this is the perfect solution, except that there are other DHCP servers in our domain that don't need the update applied (it looks like it would just pull all authorized DHCP servers from the DC). Can I create a variable with a list of DHCP servers to apply to?
So, to do what I needed, I ended up running the following:
 
$MyServerList = Get-Content -Path C:\My_SERVER_LIST.txt
foreach ($server in $MyServerList)
{
       Get-DhcpServerv4Scope -ComputerName $server | Where-Object { $_.Name -notlike 'MY_EXCLUDED_VLAN' } | Set-DhcpServerv4OptionValue -ComputerName $server -DnsServer 123.456.789.0, etc
} 

Open in new window


Thank you so much to @some one and @Gustav Brock for setting me on the right path!! I've learned alot about the inner workings of PowerShell, and I look forward to learning more about leveraging this powerful tool to help manage our environment! You all rock :)