none
Querying GetDnsServerSourceRecord.RecordData / CimInstance#Instance data in PowerShell 3.0

    Question

  • I'm trying to query the RecordData property of a zone I've retrieved using Get-DnsServerResourceRecord. If I query any of the other elements things work fine, so for instance I can run :

    $records=getdnsserverrecourcerecord -zonename "mydomain.com" | where-object {$_.RecordType -eq "A"}
    $records

    and I get all the A records in mydomain.com. However if I try the same thing using $_.RecordData I don't get any records returned.

    I've found that unlike the other properties that are all strings, the RecordData property is a CimInstance#Instance type.

    How do I query the data stored in a property of this type? I've seen mention of some sub-properties in there, for instance HostNameAlias and IPv4Address, but those have been mentioned offhand on the few pages I've found that mention RecordData without any explanation behind them.

    Ultimately I want to be able to search the zone for any records that contain a given hostname or IP address, but from what I've found I guess I may been slightly different queries depending on the type of data I'm searching for. At this stage I can't even work out how to determine the structure of CimInstance#Instance, or how to do the most basic of query against it. Unfortunately almost all mention of CimInstance online seems to relate to connecting to remote machines and WMI calls with the previous versions of PowerShell.

    Thursday, July 25, 2013 8:19 PM

All replies

  • Hi,

    Do you mean that when your run below code, nothing returned:

    get-dnsserverrecourcerecord -zonename "mydomain.com" | where-object {$_.Recorddata -eq "*"}

    Regards,

    Yan Li

    If you have any feedback on our support, please click here .

     


    Cataleya Li
    TechNet Community Support

    Friday, July 26, 2013 7:24 AM
    Moderator
  • Yes that's right, that query returns no results while if I select and run the first part only (excluding the pipe and where-object code) it returns all the records within that zone.
    Friday, July 26, 2013 10:17 AM
  • Hi,

    How about:

    get-dnsserverrecourcerecord -zonename "mydomain.com" |select-object $_.Recorddata

    Regards,

    Yan Li

    If you have any feedback on our support, please click here .


    Cataleya Li
    TechNet Community Support

    Monday, July 29, 2013 3:22 AM
    Moderator
  • If I use :

    get-dnsserverrecourcerecord -zonename "mydomain.com" |select-object $_.Recorddata

    then it effectively does nothing, I get a complete zone output including Hostname, RecordType, Timestamp, TimToLive and RecordData entries. If I instead adjust that slightly to use :

    get-dnsserverrecourcerecord -zonename "mydomain.com" |select-object Recorddata

    I get just the RecordData entries, but rather than displaying the actual values I get a list of the types of data they contain, for instance :

    RecordData
    -------------
    DnsServerResourceRecordA
    DnsServerResourceRecordNS
    DnsServerResourceRecordNS
    DnsServerResourceRecordSoa
    DnsServerResourceRecordMX
    DnsServerResourceRecordMX
    DnsServerResourceRecordA
    DnsServerResourceRecordA

    So in both cases I don't get anything I can filter and search for particular strings from.

    Monday, July 29, 2013 6:38 AM
  • Hi,

    Then maybe we could use below code:

    $records=getdnsserverrecourcerecord -zonename "mydomain.com" | where-object {$_.RecordData -eq "DnsServerResourceRecordA"}
    $records

    What is the output?

    Regards,

    Yan Li

    If you have any feedback on our support, please click here .


    Cataleya Li
    TechNet Community Support

    Tuesday, July 30, 2013 5:31 AM
    Moderator
  • Unfortunately that doesn't return any information either.

    Searching and playing around I've found that if I use :

    getdnsserverrecourcerecord -zonename "mydomain.com" | where-object {$_.RecordData.ipv4address -eq "192.168.0.1"}
    

    it returns all the records which match the where-object query. $_.RecordData.nameserver also returns any NS records, but those are the only types I've been able to find thus far.

    There's some mention online of using get-member to determine all the properties, and one of them being the ipv4address parameter, but when I tried it I just a list of the methods and properties.

    I think if I can determine what the property names are for all the other types (for instance cname, mx record, text etc) then I could potentially use multiple queries to work around being unable to do a single query to get all matching records regardless of type, but I can't work out or find how to get that info.

    Tuesday, July 30, 2013 9:17 PM
  • Bit of an update on this. Finally worked out how to find the available properties. I've gone into full detail about it at http://keithlangmead.blogspot.co.uk/2013/08/querying-dns-recorddata-properties-in.html but the crucial thing is that if you use :

    $records = Get-DnsServerResourceRecord -ZoneName "mydomain.com"
    $records.RecordData | Get-Member
    If it will return the information for all the properties within RecordData. The important thing is that the results will only include properties for record types that exist within the queried zone, eg if the zone doesn't have any MX records then you won't see any property data for querying MX records.

    Tuesday, August 27, 2013 8:26 PM
  • You can try to use something like this;

    $b = Get-DnsServerResourceRecord -ComputerName $srv -ZoneName $zone -Name $i  | select hostname,recorddata
                            foreach ($j in $b)
                                {
                                    write-host ($j.hostname + " - " + $j.recorddata.IPv4Address)

    }

    The recorddata.IPv4Address does the trick. Make sure you fill in the variables in the $b i used (this is just a part of my script...)

    Thursday, June 19, 2014 11:39 AM
  • Here's a better solution:

    ##A Records - Search by IP

    Get-DnsServerResourceRecord -ZoneName $zone -ComputerName $DNSserver -RRType "A" | select HostName,RecordType,Timestamp,TimeToLive,@{Name='RecordData';Expression={$_.RecordData.IPv4Address.ToString()}} | Where {$_.RecordData -match $IPAddress}
    ##A Records - Search by ComputerName
    Get-DnsServerResourceRecord -ZoneName $zone -ComputerName $DNSserver -RRType "A" | select HostName,RecordType,Timestamp,TimeToLive,@{Name='RecordData';Expression={$_.RecordData.IPv4Address.ToString()}} | Where {$_.HostName -match $ComputerName}

    ##CName Records - Search by ComputerName

    Get-DnsServerResourceRecord -ZoneName $zone -ComputerName $DNSserver -RRType "CName" | select HostName,RecordType,Timestamp,TimeToLive,@{Name='RecordData';Expression={$_.RecordData.HostNameAlias.ToString()}} | Where {$_.RecordData -match $ComputerName}

    ##PTR Records - Search by ComputerName

    Get-DnsServerResourceRecord -ZoneName $zone -ComputerName $DNSserver -RRType "PTR" | select HostName,RecordType,Timestamp,TimeToLive,@{Name='RecordData';Expression={$_.RecordData.HostNameAlias.ToString()}} | Where {$_.RecordData -match $ComputerName}
    These will convert the value inside the RecordData instance into a string that you can then manipulate as usual.



    • Edited by JAHernandez Wednesday, August 12, 2015 3:36 PM
    Wednesday, August 12, 2015 3:29 PM