Wednesday, December 7, 2011

Powershell + DNSCMD to find all CNAME (Alias) records from DNS

Sometimes a DBA or developer requests what aliases are pointing to a given machine. To quickly facilitate this request for them, I came up with the script below. I thought there would be a way to accomplish this task with NSLOOKUP or DNSCMD, but I couldn't get it working. If you now of a way to get this working with only one of those tools, please post it in the comments.

function Get-DNSAlias
{
Param([string] $computername=$env:computername)
if ($_) {$computername=$_}

echo "Aliases for $computername"
$aliases = dnscmd computername /enumrecords domain.com . /type CNAME
foreach($alias in $aliases){$alias | % {$_.split(" ")[0]} | where {$alias.Contains($computername)}
}
<#
.SYNOPSIS
Gathers DNS CNAME records for the given host
.EXAMPLE
Get-DNSAlias computername
.DESCRIPTION
This command gathers and reports all of the CNAME records for the given computer in the hardcoded domain list
#>
}#end Get-DNSAlias

2 comments:

Roland Ehle said...

Hi,

thank you very much for this article. Unfortunately this will not work for Records in sudomains. If you want to find all CNAME records in a Subdomain, you have to use the DNSSHell module (http://dnsshell.codeplex.com/)

Regards,
Roland

Roland Ehle said...

Hi,

thank you very much for this post. Unfortunately your solution does not work, when subdomains are involved. To find all CNAME records in subdomains you have to use the DNSShell module (http://dnsshell.codeplex.com/)

Regards,
Roland