Batch-Getting IP Addresses
Let's say you have a list of hostnames and need IP addresses for them.
"You have a list of hostnames..."
Very funny. This is probably overkill on what is simply [System.Net.Dns]::GetHostEntry($ComputerName).AddressList. Still, it's useful. Probably.
function Get-IpAddress {
param (
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)][string[]]$ComputerName = @()
);
process {
foreach ($computer in $ComputerName) {
try { $AddressList = [System.Net.Dns]::GetHostEntry($computer).AddressList; }
catch { $AddressList = @('ERROR'); }
foreach ($address in $AddressList) {
1 | Select-Object -Property @{
n = 'ComputerName';
e = { $computer; }
}, @{
n = 'IPAddress';
e = {
if ($address.GetType().Name -ne 'IPAddress') {
'ERROR';
} else {
$address.IpAddressToString;
}
}
}
}
}
}
}