powershell system.net.dns gethostaddresses error code example

Example 1: powershell system.net.dns gethostaddresses error

cls

import-module activedirectory

$w = "L2-Windows Servers"
$ComputerList = Get-ADComputer -Filter * 

foreach ($Computer in $ComputerList) {
	$IP = $Null
	$ErrorActionPreference = "silentlycontinue"
	$IP = [System.Net.Dns]::GetHostAddresses($Computer.DNSHostName)
	$ErrorActionPreference = "continue"
	If ($IP -ne $Null) {
		Write-Output ("add filter filterlist=" + $w + " srcaddr=any dstaddr=" + $IP + "description=" + $Computer.Name)  | Out-File "c:\dailyimport.nsh" -width 120 -Append
		}
	If ($IP -eq $Null) {
		Write-Output ($Computer.DNSHostName + "Could Not Be Resolved") | Out-File "c:\Error.Log" -width 120 -Append
		}
	}

Example 2: 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