Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve HostName from CNAME in PowerShell

Tags:

powershell

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.

like image 675
Rupesh Avatar asked Feb 17 '26 04:02

Rupesh


2 Answers

Select the result that is an A record:

Resolve-DnsName -Name foo.example.com |
    Where-Object { $_.QueryType -eq 'A' } |
    Select-Object -Expand Name
like image 105
Ansgar Wiechers Avatar answered Feb 18 '26 22:02

Ansgar Wiechers


To turn a CNAME into the original hostname:

Resolve-DnsName 'webserver.999.com' -Type CNAME |
    Select-Object -ExpandProperty 'NameHost'
like image 22
TessellatingHeckler Avatar answered Feb 18 '26 22:02

TessellatingHeckler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!