Change DNS setttings of remote PC's

Hi,

I have the following to change DNS settings on remote computers.
I would like to be able to report back on all clients in the clients.txt file that failed to get their settings updated
Thanks

$dnsservers = “192.168.60.4”,“192.168.60.51”
$computers = Get-Content “C:\clients\clients.txt”
foreach ($comp in $computers) {
$adapters = Get-WmiObject -q “select * from win32_networkadapterconfiguration where ipenabled=‘true’” -ComputerName $comp
foreach ($adapter in $adapters)
{
$adapter.setDNSServerSearchOrder($dnsservers)
}
}

Something like this might achieve what you’re after:

$dnsservers = "192.168.60.4","192.168.60.51"
$computers = Get-Content "C:\clients\clients.txt"
foreach ($comp in $computers) {
    Try {
        $adapters = Get-WmiObject -q "select * from win32_networkadapterconfiguration where ipenabled='true'" -ComputerName $comp -ErrorAction Stop
        foreach ($adapter in $adapters) {
            $adapter.setDNSServerSearchOrder($dnsservers)
        }
    } Catch {
        #Write to text file, output object, etc.
    }
}

Jason, the TS stated he wanted to get informed if the settings were not being applied. You’re putting -ErrorAction Stop before the settings get applied, so your solution is no applicable. I’m wondering why the TS can’t use Set-DnsClientServerAddress? With that you can use a try/each construct in the appropriate way.

I’m not sure I follow how the solution isn’t applicable unless you’re referring the potential that the method call could fail. If that’s the case, I might suggest the following:

$dnsservers = "192.168.60.4","192.168.60.51"
$computers = Get-Content "C:\clients\clients.txt"
foreach ($comp in $computers) {
    Try {
        $adapters = Get-WmiObject -q "select * from win32_networkadapterconfiguration where ipenabled='true'" -ComputerName $comp -ErrorAction Stop
        foreach ($adapter in $adapters) {
            $result = $adapter.setDNSServerSearchOrder($dnsservers)
            #SetDNSServerSearchOrder method returns: https://msdn.microsoft.com/en-us/library/aa393295(v=vs.85).aspx
            switch ($result) {
                0       {"DNSServerSearchOrder modified. No reboot required."}
                1       {"DNSServerSearchOrder modified. Reboot required."}
                default {Throw "An error occurred. The error was $result"}
        }
    } Catch {
        #Write to text file, output object, etc.
    }
}

As for Set-DNSClientServerAddress, I would suspect an OS older than 2012 is at work here.

Basically guys what I’ve been tasked to do is update all DNS settings for just over 100 Windows 10 PC’s.
The initial script I pasted seems to work but I am trying to add some extra info that will list all PC’s that did not update. If there is a better or easier way to do this then great.

Cheers

Stuart, can’t you use Set-DnsClientServerAddress?

I don’t know if I can use that? I’m reading what I can but struggling to find a way to do it. I keep going going to websites that lead e to a different idea but am getting nowhere fast. Grrr
Cheers