PowerShell: Get IP’s From Computer Names

Resolve from Names to IPs:

$names=@(
    'TESTVM001',
    'TESTVM002',
    'TESTVM003'
)
foreach($name in $names){
    $ips = [System.Net.Dns]::GetHostAddresses($name)
    write-host $ips
}

Resolve from IPs to Names:

$computerNames=@(
    'TESTVM001',
    'TESTVM002',
    'TESTVM003'
)
$regexIP = [regex] "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
foreach($computername in $computernames){
    $name = if($computername -match $regexIp){[System.Net.Dns]::GetHostByAddress($computername).hostname}else{$computername}
    write-host $name
}

Leave a Reply

Your email address will not be published. Required fields are marked *