powershelldnscustomproperty

dnsserverresourcerecord - all expanded properties and zone


Get-DnsServerResourceRecord -ComputerName server -ZoneName zone.com

I want to run this command and get the HostName, RecordType, ZoneName and All RecordData sets.

I have something like so far:

Get-DnsServerResourceRecord zone.com -ComputerName server |
    select hostname, recordType, name,
        @{Name='ARecordData';Expression={$_.RecordData.IPv4Address}},
        @{Name='CNameRecordData';Expression={$_.RecordData.HostnameAlias}}

My issue is two fold.

  1. I need to know how to get the ZoneName into the record set so if I wanted to pass multiple zones I can keep that data separate.
  2. The above example will create different columns for each RecordData. As a DNS record will only have one of these values is there a way to combine them into one column through PowerShell?

Solution

    1. You can add the zone name as a calculated property just like you do with the record data.

      foreach ($zone in $zone_list) {
          Get-DnsServerResourceRecord -ZoneName $zone -ComputerName server |
              Select-Object hostname, @{n='ZoneName';e={$zone}}, recordType, ...
      }
      
    2. Don't create individual properties, instead use a switch statement to select the relevant data depending on the record type in a single property.

      ... | Select-Object hostname ..., @{n='Data';e={
          $rr = $_
          switch ($rr.RecordType) {
              'A'     {$rr.RecordData.IPv4Address}
              'CNAME' {$rr.RecordData.HostnameAlias}
              ...
          }
      }}