Author Posts

July 30, 2015 at 11:38 am

get-dnsserverresourcerecord -computername server -zonename zone.com -RRType A

returns five columns. one of them is "name" and another is "Recorddata." name contains name, and recorddata contains the ip address.

but
get-dnsserverresourcerecord -computername server -zonename zone.com -RRType A | -select name,Recorddata

correctly returns the name in the first column, and "DNSServerResourceRecordA" on every row in the second column.

how do I display just the name and ip address?

July 30, 2015 at 11:47 am

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.

July 31, 2015 at 9:41 am

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?

July 31, 2015 at 10:33 am

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.

Get-ObjectA |
Select-Object -Property Property1,Property2,Property3,@{
n='Property4A";
e={$_.Property4 | Select-Object -ExpandProperty Property4A}
}

That's the basic model.

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?

July 31, 2015 at 3:23 pm

Get-DnsServerResourceRecord -ComputerName server -zonename zone.com -RRType A | select hostname,@{label='IP Address'; expression={$_.recorddata | select -expandproperty ipv4address}} | ft -AutoSize

(after some research to figure out what your 'e' stood for 🙂 )

thank you again. I've actually always wondered how to change the column names anyway, so. two birds, one thread.

January 21, 2016 at 11:16 am

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?

January 21, 2016 at 12:23 pm

Ah, that's what -contains and -in are for.

Assume I have an object $obj, which has a property prop. prop is actually a collection of [string] objects.

... | where { $obj.prop -contains "this" } | ...

Because I'm providing a [string] "this", and that's what $obj.prop contains, it'll work.

January 21, 2016 at 1:33 pm

Don,

I must have missed something.

Get-DnsServerResourceRecord -ComputerName DNSServer-ZoneName zone.com | ? {$_.recorddata -contains "10"}

is not returning anything (every dns record in zone.com starts with 10)

January 21, 2016 at 1:54 pm

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.

January 21, 2016 at 2:21 pm

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:

Get-DnsServerResourceRecord -ComputerName DNSServer -ZoneName zone.com -rrtype a | ? {$_.recorddata -contains '10.10.10.15'}

January 21, 2016 at 2:25 pm

You need a reverse lookup zone ;).

So, do this:

Get-DnsServerResourceRecord -ComputerName DNSServer -ZoneName zone.com -rrtype a | select -expand recorddata | get-member

What's the TypeName?