check ping for multiple servers - powershell

时间:2016-10-20 18:51:24

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

how to check variables in each line of the text area and test them for ping

foreach ($textbox1 in $textbox1) {

    if (test-Connection -ComputerName $textbox1 -Count 2 -Quiet)
    {
        $path = "$([Environment]::GetFolderPath("Desktop"))\ping.txt";
        "$textbox1 is Pinging " | fl > $path; notepad $path;

    }
    else
    {
        $path = "$([Environment]::GetFolderPath("Desktop"))\notping.txt";
        "$textbox1 not pinging" | fl > $path; notepad $path;

    }
}

I want to check for each variable in text area individually and check them each whether it pings or not

1 个答案:

答案 0 :(得分:1)

Based on your comment, I will use an example of getting the content from a text file:

$Servers = gc "(path to txt file, 1 server per line)"

foreach($Server in $Servers){
    if (Test-Connection -ComputerName $Server -Count 2 -Quiet) {    
        Write-Host "$Server is Pinging "    
    } else {
        Write-Host "$Server not pinging"    
    }
}