Updating DNS records via PowerShell

Welcome Forums General PowerShell Q&A Updating DNS records via PowerShell

Viewing 4 reply threads
  • Author
    Posts
    • #278163
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      Hi All,

      I am new to PowerShell, but have written some basic scripts over the years.  I mainly work in Unix/Linux so am very familiar with shell, perl, python, etc scripting.  I am looking to write some PowerShell scripts to update DNS records, mainly updating IP addresses.

      PS Version
      ——-
      5.1.14393.3471

      In searching around I have found a few examples but so far I have not succeeded.  I’m trying to do something simple like this:

      Which errors out with

      Can anyone shed some light on how this can be done?  Any guidance is greatly appreciated..

      Thanks,

      HB

       

    • #278166
      Participant
      Topics: 1
      Replies: 95
      Points: 435
      Helping Hand
      Rank: Contributor

      $new and $old are references that point to the same object. When you update one, the other is updated. So $old now contains the new IP address when $new is updated and cannot be found. You need to clone the object to have a copy with a difference reference.

    • #278169
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      @AdminOfThings45 – Thank you for the quick reply, that does the trick.

      I am reading that updating the PTR is not automatic.  So in general, it seems it would be best to use the block to update IP followed by a block to update the PTR?  Is that correct?

      Thanks again,

      HB

    • #278187
      Participant
      Topics: 1
      Replies: 95
      Points: 435
      Helping Hand
      Rank: Contributor

      You are correct about the PTR record. By default, when you add a new A record, it will create the PTR record if it can. When performing a change, you will have to do both. I have found that deleting records and then adding new records made that process simpler. It may not make sense for you to do that in every case though.

    • #280854
      Participant
      Topics: 0
      Replies: 5
      Points: 21
      Rank: Member

      PowerShell allows you to not only manage your DNS records from the command line but also to take those commands and put them into a script to automate all kinds of time-consuming tasks.

      Following are some different queries to update DNS records using powershell

      I’ll use the cmdlet to query that DNS zone on the domain controller called DC.

      Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local

      Now that I can read various DNS records, perhaps I’d like to modify a static record. One of our server names has changed and I need to be sure its DNS record is updated to reflect that.

      Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -RRType A

      we’ll need to get two identical objects representing a DNS record. In this case, I’m pulling a DNS record for my MySQL server.

      $new = $old = Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL

      After I have the two objects, I’ll then change the IPV4 address on the new object to represent the IP address it has changed to. Unfortunately, it’s not quite as easy as simply setting a string. The IPV4Address property requires a type of System.Net.IPAddress in order to successfully make the change.

      $new.RecordData.IPv4Address = [System.Net.IPAddress]::parse(‘192.168.0.254’)
      1
      $new.RecordData.IPv4Address = [System.Net.IPAddress]::parse(‘192.168.0.254’)
      After the IP address is changed on the $new object, I can then use Set-DNSServerResourceRecord to force PowerShell to update the record on the server itself.

      Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName mylab.local -ComputerName dc
      1
      Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName mylab.local -ComputerName dc
      Finally, if I’d like to remove the record, the process is much simpler. I can simply pipe the results of Get-DNSServerResourceRecord directly to Remove-DNSServerResourceRecord.

      Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL | Remove-DNSServerResourceRecord –ZoneName mylab.local –ComputerName DC
      1
      Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL | Remove-DNSS

      I hope this helps!

      Ben Martin

Viewing 4 reply threads
  • You must be logged in to reply to this topic.