Pulling computer names from a network by IP address using a CSV file

I am trying to figure out a few days why my code is not working.
I have a list of IP addresses in a CSV file. from this list of IP addresses I want to get the computer name. When I do what I get

Throw an exception "GetHostByAddress" with argument "1": "Invalid IP address specified." On line: 3 char: 35 + [System.Net.dns] :: GetHostbyAddress <<<("$ Foo [0]") + CategoryInfo: NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId: DotNetMethodException

Here is an example 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

Used code:

$foo = import-csv  C:\test\test\test.csv 
[System.Net.dns]::GetHostbyAddress("$foo[0]")
+3
2

UPDATE: Trim() , .

value:

$foo = import-csv  C:\test\test\test.csv 
[System.Net.dns]::GetHostbyAddress($foo[0].Value.Trim())

:

import-csv  C:\test\test\test.csv | 
foreach-object { [System.Net.dns]::GetHostbyAddress($_.Value.Trim()) }

- :

import-csv  C:\test\test\test.csv | 
where-object {$_.Value.Trim() -as [ipaddress]} | 
foreach-object { [System.Net.dns]::GetHostbyAddress($_.Value.Trim()) }
+7

, :

$foo = get-content C:\test\test\test.csv | ? { $_ -notmatch  "Value" }

[System.Net.dns]::GetHostbyAddress($foo[0])
0

All Articles