The default view that gets used when you just run Get-DnsServerResourceRecord does not display actual property names, and does some stuff to create a friendlier display than would otherwise happen by default.
Once you use Select-Object, you dis-engage that default view.
Start by piping the command to Get-Member so you can see the “real” property names. Or, use Select-Object * to see something similar. That’ll help you figure it out. See also, https://powershell.org/kb/commands-default-output-can-lie/.
Part of what can happen sometimes is that a property contains another complex object - like a DNSServerResourceRecordA object. You might need to use Select-Object’s -ExpandProperty parameter to expand just that problematic object so you can see what IT looks like.
thanks Don. I did not know about -expandproperty. get-member isn’t really helpful, but I do see ipv4address when I use -expandproperty recorddata at the end of my original command.
unfortunately I still don’t know how to JUST show hostname and ipv4address in the output.
… | -select hostname -expandproperty recorddata
gets me close, but there’s an extra column called “PSComputername.” the column is empty, but it’s still there. is there a way to select just one of the expanded parts of recorddata?
It can be tricky. What you want is the combined output of two objects. Let’s take these specific ones out of it for a second.
Let’s say I have Object A, and I want Property1, Property2, and Property3. There’s also a Property4, and it’s actually a sub-object, and I want Property4A from it. So that’s four bits of output total.
The first Select-Object is getting the “top level” properties from Object A. I’m then creating a “custom property” (also called a “calculated property”). In it, I set the output property name (n=) as Property4A, so that’s what’ll show in the output table. I then create an expression (e=) that defines what the custom property will contain.
$_ represents whatever object Select-Object is working with, and so I take its Property4 - which is a sub-object, remember - and pipe it to another Select-Object, extracting the contents of the sub-object’s Property4A, which is what I want.
The resulting output is a single object with four properties, which PowerShell would by default display as a four-column table.
This solves the “the thing I want to display includes sub-objects, and I only need a portion of the info those sub-objects contain.” In your case, RecordData is Property4A… think you can take it from there?
well. now. this is great and all for outputting data. but what if I want to query dns, and for records whose ip address is 10.10.10.15? how do I use an expanded property in a where clause?
So, that’s the trick with -contains, It isn’t a wildcard match. It’s looking to see if $_.recorddata contains EXACTLY “10.” Which, of course, it doesn’t, right?
So there’s no way to do a wildcard match across a collection. You’ll probably have to use a ForEach instead, and enumerate all the RecordData properties individually.
my end goal is to feed a list of IPs into a foreach statement, and have the script output a list of FQDNs from DNS. but at this point i’m just testing with one IP that I know has a dns record.
so if I do this:
Get-DnsServerResourceRecord -ComputerName DNSServer -ZoneName zone.com -rrtype a | ? hostname -like host1 |select hostname, @{label=‘IP Address’;expression={$_.recorddata |select -expandproperty ipv4address}}
the output is hostname ‘host1’ and ip address 10.10.10.15. so obviously there is an A record in my DNS named host1 with that ip address.
but if I try to reverse the process to return ‘host1’ from the ip address 10.10.10.15, nothing is returned from the following:
Hey Don, your contribution has been very helpful. My challenge is a bit more complex than John’s. I need to move all of my Zones to a public DNS provicer that would only accept csv file in the format and order below.
You can do this,
I would suggest to DIY.
Build a CSV and iterate through the records to append the contents into CSV, set the value property based on the RecordType of the incoming object. Give it a try and update here when you get isssues, we are here to help you.
PS: It would be good if you post a new thread based on your attempt.