How to Execute the NSLOOKUP or the Ping-a Command using PowerShell

I need to read a text file populated with IP addresses.  Run the NSLOOKUP or Ping - a command against each IP address within the text file and return the hostname associated with each IP to a text file.

Any pre-existing scripts or examples that executes either of these commands would be appreciated.

Lipotech
lipotechSys EngAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mauro CazabonnetSenior .NET Software EngineerCommented:
Source IP file

ippaddresses.csv

ipaddress
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5

$ping = New-Object System.Net.NetworkInformation.Ping  

$sourcefile = "C:\test\ipaddresses.csv"
$outfile = "C:\test\hostnameout.csv"
Import-Csv $sourcefile | ForEach {
    $ip   = $_.ipaddress
    $Res = $ping.send($ip)    
    if ($Res.Status -eq "Success")  
    {  
        $hostname = ([system.net.dns]::GetHostByAddress($ip)).hostname    
        $result = $ip + "," + $hostname + " = Success"  
        Write-Host $result
        $result | Out-File $outfile -Encoding ASCII 
    }  
}  

Open in new window

lipotechSys EngAuthor Commented:
I ran the script with the following simple modifications:

$ping = New-Object System.Net.NetworkInformation.Ping  

$sourcefile = "C:\unkiptxt\OPList.txt"
$outfile = "C:\unkiptxt\hostnameout.csv"
Import-Csv $sourcefile | ForEach {
    $ip   = $_.ipaddress
    $Res = $ping.send($ip)    
    if ($Res.Status -eq "Success")  
    {  
        $hostname = ([system.net.dns]::GetHostByAddress($ip)).hostname    
        $result = $ip + "," + $hostname + " = Success"  
        Write-Host $result
        $result | Out-File $outfile -Encoding ASCII
    }  
}  

I am getting the following error:

PS C:\scripts2\unkiptxt> C:\scripts2\unkiptxt\IpNameResolution.ps1
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
At C:\scripts2\unkiptxt\IpNameResolution.ps1:7 char:22
+     $Res = $ping.send <<<< ($ip)    
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

It seems simple enough. Any idea where I am going wrong??

Lipotech
Mauro CazabonnetSenior .NET Software EngineerCommented:
your source file should be a csv
and have header 'ipaddress'

followed by the list of ips

Please post your input file
Guy LidbetterCommented:
Simple change if your source is a TXT file with a list of IP's:

$ping = New-Object System.Net.NetworkInformation.Ping  

$sourcefile = "C:\unkiptxt\OPList.txt"
$outfile = "C:\unkiptxt\hostnameout.csv"
Get-Content $sourcefile | ForEach {
    $ip   = $_
    $Res = $ping.send($ip)    
    if ($Res.Status -eq "Success")  
    {  
        $hostname = ([system.net.dns]::GetHostByAddress($ip)).hostname    
        $result = $ip + "," + $hostname + " = Success"  
        Write-Host $result
        $result | Out-File $outfile -Encoding ASCII 
    }  
}  

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lipotechSys EngAuthor Commented:
Testing this version of the code now.  Please give me until next Tuesday to test.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.