Search Domain Computers for DNS Settings, make changes depending on what is set

Hey all,

To start, I am definitely newer to powershell and have been enjoying learning this so far. Unfortunately that has been a rough road since I only use it when I need to, but I’m trying to use it more and more frequently! I am attempting to scan an OU of computers for their DNS information. Then, based on that information, I want to change their DNS settings across the board. I started with this script:

$computers = Get-Content "C:\computers.txt"

foreach ($computer in $computers){

Invoke-Command -ComputerName $computer -ScriptBlock { Get-DnsClientServerAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue } -ErrorAction SilentlyContinue

}

Some questions on what I would like to do:

  1. I saw somewhere else that instead of doing the Get-Content to get the information, that I can invoke Get-ADComputer from within this script. How would I do that?
    1. The Get-ADComputer cmdlet is what generated this text file, however there were a varying number of spaces after each of the computer names... anyway to ensure this is made without spaces so I don't have to manually manipulate this list?
  2. I need to be able to send computers the commands to update their DNS servers. We do NOT use DHCP, and instead preferred a Static Address setup for various reasons. When running Get-DnsClientServerAddress, there are a few things that may interfere with remotely providing each computer DNS information, which I need to work around that somehow, such as:
    1. Computers have different "InterfaceAlias" settings, such as Ethernet, Ethernet 2, etc. Most computers appear to have both, some may use Ethernet, some may use Ethernet 2.
    2. Each computer has a different Interface Index, so we wouldn't be able to solely search machines for that.
    3. All existing computers that do have DNS information all appear to have the same primary DNS server. Perhaps we could somehow filter for ONLY interface and machine names that have that specific server IP address set?
    4. Once we narrow that down, how would I either change or remove a secondary address if set? There are a few I want to change, and the majority do not have a secondary configured.
Thanks!

First and foremost I’d recommend for you to start by learning the very basics of Powershell from scratch. That would save you from a lot of mistakes, errors and wasted time.

You could take a little time and watch this video course: Getting Started with PowerShell 3.0
or read at least the first chapters of this book: Windows PowerShell™ 4: TFM.

It is beyond the scope and the possibilities of a forum to teach you Powershell iteratively.

Powershell cmdlets return objects. What you see on the console are string representatives of these objects. You don’t have to deal with text and spaces in pure Powershell. :wink:

Most of the time you can pipe the output from one cmdlet to the next cmdlet. You should learn how to read the help for the cmdlets you’re about to use to learn how to use them and to learn if they do accept pipeline input and how they do.

Other cmdlets can take arrays of objects instead of single objects. Just like Invoke-Command. :wink: You could create an array of computer objects returned by Get-ADComputer with the attribute Name and provide it to Invoke-Command. There you can access the attribute by using the dot notation. Here is an example:

$ComputerList = Get-ADComputer -SearchBase 'Path to the desired OU' -Filter "Name -like 'StartWithSomething*'"
Invoke-Command -ComputerName $ComputerList.Name -ScriptBlock {'do something meaningful ... '}

For the second part of your question. You can use the cmdlet Get-DNSClient or Get-NetAdapter and filter the output by the conditions you need to determine the desired interface and then use the cmdlet Set-NetAdapter to set your desired settings. You can even use Powershell to figure out what cmdlets could fit your needs with the cmdlet Get-Command … here is an example:

Get-Command -Noun *adapter* -Verb set

You should always read the complete help including the examples for the cmdlets you’re about to use to learn how to use them and to learn what they are capable of.