2

I have a network wherein CNAME entries are created for some servers and not for others. How can I get the details for server names alone by executing Resolve-DnsName script?

If I try

$servername = Resolve-DnsName -Name webserver.999.com
Write-Host $servername.Name

I get 2 results one with webserver.999.com and another with the actual server name post resolving the DNS for servers with CNAME, but only the server name where CNAME entry is not present. I am looking for a solution wherein the command catches the result with only the server name.

2
  • By "host name" do you mean the SMB Windows machine name? Commented Jul 20, 2018 at 22:53
  • Yes, actual name of the server to which the CNAME points at Commented Jul 20, 2018 at 22:55

4 Answers 4

6

Select the result that is an A record:

Resolve-DnsName -Name foo.example.com |
    Where-Object { $_.QueryType -eq 'A' } |
    Select-Object -Expand Name
Sign up to request clarification or add additional context in comments.

3 Comments

I was going to suggestResolve-DnsName -Type A ... instead of Resolve-DnsName ... | Where-Object Type -eq A, but it seems that the former sometimes reports additional records of a different type; e.g., (Resolve-DnsName www.microsoft.com -Type A).Type Do you happen to know why?
@mklement0 Nothing definitive. However, the call needs to resolve the entire chain of CNAMEs to get to the underlying A record, so I can see why it returns multiple results. After all, just responding that "www.microsoft.com" isn't an A record wouldn't be too useful from a name resolution perspective. It would make sense from a DNS management perspective, but there's a different set of cmdlets for that purpose.
Thanks, that makes sense; another thing that's perhaps of interest: it seems that such an indirect lookup of an A record only ever outputs one (as the last), whereas querying a name that directly has an A record can result in multiple A records for large sites (e.g., Resolve-DnsName microsoft.com -Type A).
2

To turn a CNAME into the original hostname:

Resolve-DnsName 'webserver.999.com' -Type CNAME |
    Select-Object -ExpandProperty 'NameHost'

1 Comment

This is not working if "webserver.999.com" doesn't have CNAME and "webserver.999.com" is the actual computer name
0

One more way, which returns original hostname for a CNAME, original A name or just additional A name with the same IP of the server, as long as PTR records are maintained properly and point to the original hostname.

(resolve-dnsname (resolve-dnsname webserver.999.com).ipaddress ptr).namehost

Comments

0

PowerShell code to read CNAME records from a text file and output the original hostnames to another text file. Here's a script to do that:

# Specify the input and output file paths
$inputFilePath = "CNAMEs.txt"
$outputFilePath = "OriginalHostnames.txt"

# Read CNAMEs from the input file
$cnames = Get-Content -Path $inputFilePath

# Initialize an array to store the original hostnames
$originalHostnames = @()

# Iterate through each CNAME and resolve it to get the original hostname
foreach ($cname in $cnames) {
    $originalHostname = (Resolve-DnsName (Resolve-DnsName $cname).IPAddress PTR).NameHost
    $originalHostnames += $originalHostname
}

# Write the original hostnames to the output file
$originalHostnames | Out-File -FilePath $outputFilePath

# Display a message indicating the task is complete
Write-Host "Original hostnames have been written to $outputFilePath"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.