8 | foreach {
New-Object PSObject -Prop @{
Address ="192.168.1.$_";
Status = (Test-Connection "192.168.1.$_" -Quiet -Count 1);
try {HostName=[System.Net.Dns]::GetHostEntry("192.168.1.$_").HostName} catch {HostName="UNKNOWN"}
}
} | Format-Table -Auto
I want to resolve the hostnames using [System.Net.Dns]::GetHostEntry()
. I am using Test-Connection
to get the connection status.
My problem is that when a host name cannot be resolved, the command returns an error. I need to store "UNKNOWN " in the HostName
property for that particular member, is there any construct that i can use to achieve this?
Put the Try/Catch after you start defining HostName.