locked
How to update static DNS IPs for servers in specific OU in Active Directory RRS feed

  • Question

  • I am trying to update static DNS IP addresses for all my servers in specific OU in Active Directory. I need help putting the GetComputer command with the Set-DNSClientServerAddress command so that I can only update the handful of servers I need to from old DNS IPs to new. 
    Monday, July 15, 2019 3:13 PM

All replies

  • What have you attempted so far?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, July 15, 2019 3:16 PM
  • This is the closest thing I have found that may do what I am looking for, but I am not sure where to 1. specify the path to my OU, 2. where to specifyold DNS IPs, and 3. where to specify the 3 new IPs. 

    I have a handful of servers sitting under "OU=Servers,OU=ADM,OU=USA,DC=wrbts,DC=ads,DC=company,DC=com"

    That I want to change IPs from 192.168.180.203, 172.19.3.114 to 10.99.24.191, 10.99.24.192, 10.95.32.63

    Param([String[]]$NewDNS,[String]$OldDNS,[String]$OU)

    #requires -Version 2

    #Adds active directory modules to the current session
    Import-Module -Name ActiveDirectory

    #region Custom DNS Configure Report

    $ScriptPath = ($myinvocation.mycommand.Path).Replace($myinvocation.mycommand.Name,"")
    $key = $(Get-Date -format "MMddhhmmss")

    #It creates a report file in current script position
    New-Item "$ScriptPath\report_$key.html" -ItemType file | Out-Null

    $HTML=@"
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <style> BODY{font-family:Verdana; background-color:lightblue;}
    TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;} 
    TH{font-size:1.2em; border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color:lightskyblue} 
    TD{border-width: 2px;padding: 2px;border-style: solid;border-color: black;align=right}
    </style>
    </head><body>
    <H1>DNS Configuration</H1>
    <table>
    <colgroup>
    <col/>
    <col/>
    </colgroup>
    <tr bgcolor=yellow><th>Computer Name</th><th>Status</th><th>Current_DNS_Address</th><th>Previous_DNS_Address</th></tr>
    "@
    Add-Content -Value $HTML -path "$ScriptPath\report_$key.html"
    #endregion


    #region Main Function

    #Get full distinguished name
    $DistName = Get-ADOrganizationalUnit -Filter 'Name -like $OU' | ForEach-Object{$_.DistinguishedName}
    #Retrieve all computer objects in the OU
    $Servers = Get-ADComputer -SearchBase "$DistName" -Filter "*" | ForEach-Object {$_.Name}


    foreach($Server in $Servers)
    {
    #Connecting test
    $PingResult = Test-Connection -ComputerName $Server -Count 1 -Quiet
    if($PingResult)
    {
    #Use the Windows PowerShell to monitor for errors.
    try
    {
    <#If the computer we are querying is a DHCP client and the DNS servers setting were 
    assigned by a DHCP server option, then do not modify.#>
    $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Server -ErrorAction Stop | `
    Where-Object {$_.IPEnabled -eq $TRUE -and $_.DHCPEnabled -eq $False}

    foreach($NIC in $NICs) 
    {
    $PreDNSInfo = $NIC | Foreach-Object {if($_.DNSServerSearchOrder -match $OldDNS ){$_.DNSServerSearchOrder}}
    $PreDNSInfo = $PreDNSInfo -join ","

    #Set up DNS information
    if($NIC.DNSServerSearchOrder -match $OldDNS )
    {
    $DNSServers = $NewDNS
    $NIC.SetDNSServerSearchOrder($DNSServers) | Out-Null
    $NIC.SetDynamicDNSRegistration("TRUE") | Out-Null
    #Get the DNS information

    Write-Host "$Server Setting Successed!" -ForegroundColor Green
    Add-Content -Value "<tr bgcolor=#F0F8FF><td align=left>$server</td><td align=center>Setting Successed</td><td align=center>$($NewDNS -join ",")</td><td align=center>$PreDNSInfo</td></tr>" -Path "$ScriptPath\report_$key.html"
    }
    }
    }
    #When an error occurs within the Try block, triggers an exception.
    catch
    {
    Write-Warning "$Server Setting Failed! $Error[0]"
    Add-Content -Value "<tr bgcolor=#F0F8FF><td align=left>$server</td><td align=center>Setting Failed</td><td align=center> </td><td align=center> </td></tr>" -Path "$ScriptPath\report_$key.html"
    }
    }
    else
    {
    Write-Host "$Server Failed to connect!" -ForegroundColor Yellow
    Add-Content -Value "<tr bgcolor=#F0F8FF><td align=left>$server</td><td align=center>Failed to connect</td><td align=center> </td><td align=center> </td></tr>" -Path "$ScriptPath\report_$key.html"
    }
    }
    #endregion

    #Modify configure report
    Add-Content -Value '</table>' -Path "$ScriptPath\report_$key.html"
    Add-Content -Value '</body></html>' -Path "$ScriptPath\report_$key.html"
    Add-Content -Value "<p>---------- $(get-date) ----------</p>" -Path "$ScriptPath\report_$key.html"

    Monday, July 15, 2019 3:35 PM
  • There has to be something more simple than this that Points to my specific OU and tells those servers to update existing DNS IPs to new ones. 
    Monday, July 15, 2019 3:36 PM
  • Since you do not have a full understanding of PowerShell, I would start off small first, then expand. Read the section "How to change DNS address using PowerShell" from https://pureinfotech.com/change-dns-windows-10/

    Once you get that working for a single server, you can expand it to query AD then pipe over to those cmdlets.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, July 15, 2019 5:29 PM
  • Hi,

    Thanks for your question.

    1. create the csv file contains the computer information you want to change the dns address.

    For example:

    2. Import the CSV file and change the ip address remotely by powershell.

    $cred= Get-Credential
    $computers=Import-Csv "c:\temp\computerinfor.csv"
    foreach($computer in $computers)
    {
      Invoke-Command -ComputerName $computer.computername -ScriptBlock {Set-DnsClientServerAddress -InterfaceIndex $computer.interfaceindex -ServerAddresses $computer.serveraddresses} -Credential $cred
    }

    Best regards,

    Lee


    Just do it.

    Tuesday, July 16, 2019 7:47 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee



    Just do it.

    Wednesday, July 31, 2019 7:56 AM