PowerShell to ping computers from a csv file

Stefan Funk 21 Reputation points
2022-12-25T16:04:16.953+00:00

I'm using a Powershell script found on the Internet.
It does show me what systems are online and offline if I run it.
Suddenly it stopped working.
Can you may help me with this; please?

Content of this csv input file (hosts.csv):

Host;|
"x.y.z.11;->> Firewall"
"x.y.z.12;->> Switch"
.
.

-------------------------------------------------------------

...and the Powershell script below.

-------------------------------------------------------------

$Systemes = import-csv "C:\hosts.csv" -delimiter ";"
foreach ($Systeme in $Systemes) {
if (test-Connection -Computername $Systeme.Host -Count 1 -Quiet ) {
Write-Host "$System is online!" -ForegroundColor Green
} else
{ Write-Host "$System is offline!" -ForegroundColor Red }
}
Pause

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,066 Reputation points
    2022-12-26T13:44:46.913+00:00

    Change the input file to this.

    Host;Type  
    x.y.z.11;Firewall  
    x.y.z.12;Switch  
    

    And the script to this.

    $Systemes = import-csv "C:\hosts.csv" -delimiter ";"  
    foreach ($System in $Systemes) {  
        if (test-Connection -Computername $System.Host -Count 1 -Quiet ) {  
           "{0} {1} is online!" -f $System.Host, $System.Type | Write-Host -ForegroundColor Green  
        } else  {   
           "{0} {1} is offline!" -f $System.Host, $System.Type | Write-Host -ForegroundColor Red   
        }  
    }  
    Pause  
      
    

3 additional answers

Sort by: Most helpful
  1. MotoX80 32,066 Reputation points
    2022-12-25T17:53:18.627+00:00

    You are using both $System and $Systeme variable names.

    $Systemes = import-csv "C:\hosts.csv" -delimiter ";"  
    foreach ($System in $Systemes) {  
        if (test-Connection -Computername $System.Host -Count 1 -Quiet ) {  
            Write-Host "$($System.Host) is online!" -ForegroundColor Green  
        } else  {   
            Write-Host "$($System.Host) is offline!" -ForegroundColor Red   
        }  
    }  
    Pause  
    

    You also have double quotes around each line in the csv file so that is not getting interpreted correctly. Get rid of the quotes.

    Host;|  
    x.y.z.11;->> Firewall  
    x.y.z.12;->> Switch  
    
     
    

  2. Rich Matheisen 45,096 Reputation points
    2022-12-26T16:18:04.207+00:00

    You could do it like this, too:

    import-csv "C:\hosts.csv" -delimiter ";" |  
        ForEach-Object {  
            $ol = "online"  
            $co = "green"  
            if (-not (test-Connection -Computername $_.Host -Count 1 -Quiet) ) {  
                $ol - "offline"  
                $co = "red"  
            }  
            "{0} {1} is {2}!" -f $_.Host, ($_.Type -replace "\W",""), $ol |   
                Write-Host -ForegroundColor $co  
        }  
    
    0 comments No comments

  3. Stefan Funk 21 Reputation points
    2022-12-27T08:49:15.327+00:00

    Many thanks for your help, MotoX80 and RichMatheisen-8856.

    Missing was the "Type", in the hosts.csv file.
    Works now as before. :-)

    Best regards!

    0 comments No comments