pulling computer names from a network by IP address using a CSV file
powershell
Solution
UPDATE: Added a Trim() call to remove leading or trailing spaces if exist.
Remove the quotes and call the value property:
$foo = import-csv C:\test\test\test.csv
[System.Net.dns]::GetHostbyAddress($foo[0].Value.Trim())
To resolve all address:
import-csv C:\test\test\test.csv |
foreach-object { [System.Net.dns]::GetHostbyAddress($_.Value.Trim()) }
You can use the -as type operator to pass on only valid addresses:
import-csv C:\test\test\test.csv |
where-object {$_.Value.Trim() -as [ipaddress]} |
foreach-object { [System.Net.dns]::GetHostbyAddress($_.Value.Trim()) }
Problem
I have been trying to figure out for a few days now why my code is not working. pretty much I have a list of IP addresses in a CSV file. from this list of IP addresses I want to obtain the computer name. When I execute no matter what I get Exception calling "GetHostByAddress" with "1" argument(s): "An invalid IP address was specified." At line:3 char:35 + [System.Net.dns]::GetHostbyAddress <<<< ("$foo[0]") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException Here is an example of the CSV File and the code used: CSV File Value 192.168.0.11 192.168.0.12 192.168.0.13 192.168.0.14 The code being used: ``` $foo = import-csv C:\test\test\test.csv [System.Net.dns]::GetHostbyAddress("$foo[0]") ```