1

I am looking to run a DNS lookup for a local server, select both the hostname and IP address and output to a text file.

[System.Net.Dns]::GetHostEntry('server1') | 
    Select-Object 'HostName', 'IPAddressToString' |
    Out-File -Path 'c:\temp\DnsIpAddress.txt'

I can access HostName but cannot select IPAddressToString. I can access IPAddressToString if I save the results to a variable this way:

$result.AddressList.IpAddressToString

Can I use Select-Object to select hostname and IPAddressToString? Or should I do this another way?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

2 Answers2

3

Use a calculated property with Select-Object:

[System.Net.Dns]::GetHostentry('server1') |
  Select-Object HostName,@{Name = 'IPAddress';Expression={$_.AddressList.IPAddressToString}} |
  Out-File -Path 'C:\temp\DnsIpAddress.txt'
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

It may also be worth mentioning that there's a cmdlet Resolve-DnsName that may yield more "PowerShelly" code:

Resolve-DnsName -name www.stackoverflow.com | Select-Object Name,IPAddress
veefu
  • 2,820
  • 1
  • 19
  • 29