0

I have a requirement to find DNS registered hostname of a given IP via PowerShell. Could someone please tell me how can I store computer name alone (“mysystem” in below example) to a variable from below output.

$Hostname = [System.Net.Dns]::GetHostEntry($ipAddress)

HostName                 Aliases         AddressList                                                 
--------                 -------         -----------                                                 
mysystem.mycountry.company.com         {}              {fe80::50d6:7029:f655:f955%11, 

2 Answers 2

1

Simple. In PowerShell, the return results are always objects. The column headings in the return results (or labels, of the return results are in list format) are properties of the object that's returned. So instead of assigning the whole object to $Hostname, just assign the object's HostName property:

$Hostname = ([System.Net.Dns]::GetHostEntry($ipAddress)).HostName
Sign up to request clarification or add additional context in comments.

Comments

1

Take just the HostName property and split it at dots:

$Hostname = ([Net.Dns]::GetHostEntry($ipAddress).HostName -split '\.')[0]

1 Comment

Ah, yes...I overlooked the part about wanting just the first part, not the whole FQDN. So this answer is more complete.

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.