Checking if computer is online, then run commands

Welcome Forums General PowerShell Q&A Checking if computer is online, then run commands

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #108071

    Participant
    Points: 0
    Rank: Member

    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
    
    }
    
    }
    
    
  • #108074

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    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.

  • #108079

    Participant
    Points: 0
    Rank: Member

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

  • #108080

    Participant
    Points: 159
    Helping Hand
    Rank: Participant

    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}}
    

The topic ‘Checking if computer is online, then run commands’ is closed to new replies.