powershell system.net.dns gethostaddresses error
$computers= gc C:\ListvOfDevices.txt
$list = @()
foreach ($computername in $computers)
{
If(Test-Connection $computername -Quiet)
{
write-host "$computername - $IP - UP" -ForegroundColor GREEN
Try
{
$IP = [System.Net.Dns]::GetHostEntry($computername).AddressList | %{$_.IPAddressToString}
$IP | %{$HostName = [System.Net.Dns]::GetHostEntry($_).HostName}
}
Catch
{
write-error "Can't resolve IP or hostname from dns."
}
$compStatus = New-Object PSObject -Property @{
OrgName = $computername
Active = $true
HostName = $HostName
IP = $IP
}
$list += $compStatus
}
else
{
write-host "$computername - $IP - DOWN" -ForegroundColor RED
$IP = $null
$HostName = $null
$compStatus = New-Object PSObject -Property @{
OrgName = $computername
Active = $false
HostName = $null
IP = $null
}
$list += $compStatus
}
}
$list