Checking if computer is online, then run commands

I’m trying to do a simple query off a long list of computers if they’re online. I’m using test-function and it works, but rather than give me the response back per machine it seems to be coming back as the entire group of computers in 1 line, then ending with online.

Not sure exactly what i’m missing but i want to then add certain commands if a machine is online.

If anyone could provide me information on what i’m missing i’d appreciate it.

#Import Computer list from file

$Computers = Get-Content -path 'L:\scripts\complist.csv'

 

 

#Check to see if each computer is online

ForEach ($computer in $Computers) {

if (Test-connection -Computername $computer -BufferSize 16 -Count 2 -Quiet) {

Write-Host $Computers is online

}

}

It’s Write-Host $Computers is online

What’s in $Computers? It’s the entire list of computers. What’s in $Computer? Just one computer name at a time.

You meant to use $Computer, not $Computers.

Dang it! It was that! Geez! Thanks! How simple. lol

Another issue is you are using Get-Content to open a CSV. Assuming you have a CSV that has a column ‘Name’:

"Name"
"Computer1"
"Computer2"

you can also accomplish testing the connection with code like this using Calculated Properties:

$Computers = Import-CSV -Path 'L:\scripts\complist.csv' |
             Select *,
                    @{Name='Online';Expression={Test-connection -Computername $_.Name -BufferSize 16 -Count 2 -Quiet}}