{"id":4860,"date":"2023-09-05T15:08:14","date_gmt":"2023-09-05T13:08:14","guid":{"rendered":"https:\/\/leeryanrs.com\/?p=4860"},"modified":"2023-09-05T15:08:14","modified_gmt":"2023-09-05T13:08:14","slug":"remotely-change-all-dns-ips-on-windows-servers-in-ad","status":"publish","type":"post","link":"https:\/\/leeryanrs.com\/?p=4860","title":{"rendered":"Remotely change all DNS IP&#8217;s on Windows Servers in AD"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># Define the new DNS server IP addresses\n$dnsServers = \"8.8.8.8\", \"1.1.1.1\", \"8.8.4.4\"\n\n# Define the OU where you want to change DNS servers\n$OU = \"OU=server,DC=domain,DC=local\"  # Replace with your actual OU path\n\n# Create an array to store results\n$results = @()\n\n# Get all computer objects in the specified OU\n$computers = Get-ADComputer -Filter {Name -like \"AZ-EIW-DC01*\"} -SearchBase $OU\n\n# Loop through each computer object\nforeach ($computer in $computers) {\n    $computerName = $computer.Name\n    $computerDN = $computer.DistinguishedName\n\n    try {\n        # Set the DNS server IP addresses on the remote computer\n        Invoke-Command -ComputerName $computerName -ScriptBlock {\n            $networkAdapter = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }\n            Set-DnsClientServerAddress -InterfaceAlias $networkAdapter.Name -ServerAddresses $using:dnsServers -ErrorAction Stop\n        }\n\n        # Record the success in the results array\n        $results += &#91;PSCustomObject]@{\n            ComputerName = $computerName\n            Status = \"Success\"\n        }\n    }\n    catch {\n        # If an error occurs, record the failure in the results array\n        $results += &#91;PSCustomObject]@{\n            ComputerName = $computerName\n            Status = \"Error: $_\"\n        }\n    }\n}\n\n# Export the results to a CSV file\n$results | Export-Csv -Path \"C:\\Temp\\DNSChangeResults.csv\" -NoTypeInformation\n\n# Display the results on the console\n$results<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4860","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/4860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4860"}],"version-history":[{"count":0,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/4860\/revisions"}],"wp:attachment":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}