Thursday, February 14, 2013

Powershell and DNS

Was trying to search for static address in a large subnet that I was working on.

start off with reading - http://gallery.technet.microsoft.com/scriptcenter/DNS-Server-PowerShell-afc2142b

retrieve the records with
$records = Get-DnsServerResourceRecord -ZoneName koolkids.internal -computer kcdc

tried
$records| ? recorddata -like "10.10.*"

got nothing returned... :(

then check the types:
$records | get-member
DistinguishedName         Property   string DistinguishedName {get;}
HostName                  Property   string HostName {get;}
PSComputerName            Property   string PSComputerName {get;}
RecordClass               Property   string RecordClass {get;}
RecordData                Property   CimInstance#Instance RecordData {get;set;}
RecordType                Property   string RecordType {get;}
Timestamp                 Property   CimInstance#DateTime Timestamp {get;}
TimeToLive                Property   CimInstance#DateTime TimeToLive {get;set;}

notice that is was CimInstance#Instance for the RecordData
can't remember how to convert it to string on the fly.
so my colleague suggested 
$records | out-gridview

then do the filtering from there, which work nice.

if after a few coffees I remember how to sort this out. I will update.

4 comments:

Unknown said...

Had the same issue, turns out you need something like:

$record | ? {($_.recorddata).nameserver -match 'something'}

Exaril said...

write-host $record.RecordData.IPv4Address
Using Get-Member on just $record.RecordData gives you lots of things, but one is IPv4Address as a parameter, which you can 'get;'.

Hope this helps.

JustVisiting said...

thanks Paul/Exaril, will try it out.

Keith Langmead said...

If you use :

$records = Get-DnsServerResourceRecord -ZoneName "mydomain.com"
$records.RecordData | Get-Member

on one of the zones in DNS you'll get a list of the properties that available. One thing to note, it only outputs properties for record types that actually exist in the zone you query.

Found a load of them so far and posted about them here http://keithlangmead.blogspot.co.uk/2013/08/querying-dns-recorddata-properties-in.html