Change DNS setttings of remote PC's

Welcome Forums General PowerShell Q&A Change DNS setttings of remote PC's

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 year, 8 months ago.

  • Author
    Posts
  • #71302

    Participant
    Points: 0
    Rank: Member

    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)
    }
    }

  • #71309

    Participant
    Points: 0
    Rank: Member

    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.
        }
    }
    
  • #71336

    Participant
    Points: 26
    Rank: Member

    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.

    • #71375

      Participant
      Points: 0
      Rank: Member

      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.

    • #71384

      Participant
      Points: 0
      Rank: Member

      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

  • #71387

    Participant
    Points: 26
    Rank: Member

    Stuart, can't you use Set-DnsClientServerAddress?

  • #71518

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘Change DNS setttings of remote PC's’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort