PowerShell: Ping test script (sequential) with CSV output

This is a script that sequentially tests the specified hosts and writes the output to a CSV file. Unlike the parallel ping script I posted a few days ago, this one writes the output of all the tests the the same file, which whilst not as efficient whilst doing the tests, makes producing graphs in Excel from the results much easier.

Here’s the script:

$Computers = "192.168.0.1","rcmtech.co.uk","bbc.co.uk","virginmedia.com"
$SleepTime = 5
$CSVFile = "$env:UserProfile\Desktop\PingTest.csv"
while($true){
    $Now = Get-Date
    $ResultObject = New-Object PSObject
    $ResultObject | Add-Member NoteProperty Time $Now
    $i = 1
    foreach($Computer in $Computers){
        Write-Progress -Activity "Ping Test" -Status "Pinging $Computer" -PercentComplete ($i/$Computers.Count*100)
        $TestResult = Test-Connection $Computer -Count 1 -ErrorAction SilentlyContinue
        if($TestResult.ResponseTime -eq $null){
            $ResponseTime = -1
        } else {
            $ResponseTime = $TestResult.ResponseTime
        }
        $ResultObject | Add-Member NoteProperty $Computer $ResponseTime
        $i++
    }
    Write-Progress -Activity "Ping Test" -Status "Write results to file" -PercentComplete 100
    $ResultObject | Format-Table -AutoSize
    Export-Csv -InputObject $ResultObject -Path $CSVFile -Append -NoTypeInformation
    for($DelayTime=0; $DelayTime -lt $SleepTime; $DelayTime++){
        Write-Progress -Activity "Ping Test" -Status "Sleeping" -SecondsRemaining ($SleepTime-$DelayTime)
        Start-Sleep 1
    }
}
This entry was posted in PowerShell and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s