Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Need help with ForEach Test-Connection give IPaddress or "Failed"

$
0
0

Complete newbie in Powershell. I do VBA in Access and Excel so I thought this would be easy. Not so much.

I have a txt file of computer names.

I would like to generate a csv the displays:

Computername, IP Address, Ping Status

My first attempt was:
Test-Connection -ComputerName (get-content C:\WMI_Tools\Computerlist.txt) -Count 1 -ea 0 | Select-Object Address,IPV4Address

That works great to give me the active computers but it is lacking a ping status

My next few attempts involved foreach, if/else and Out-file but I can't get all the parts correct.

I found this code from Jaap Brasser here on forum.

Get-Content .\computerlist.txt | ForEach-Object {
   if(Test-Connection -ComputerName $_ -Quiet -Count 1) {  
        New-Object -TypeName PSCustomObject -Property @{
           ServerName = $_
           'Ping Status' = 'Ok'
       }
   } else {
       New-Object -TypeName PSCustomObject -Property @{
           ServerName = $_
           'Ping Status' = 'Failed'
      }
    }
} | Export-Csv C:\WMI_Tools\Get_PingResults.csv

 

How would I add IP Address?

Ping status is the first column currently.
Howow do I make it so ServerName is the first column?  

Thank you.

 

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles