Ping Log Script

Hi Guys

I have a customer that has problems with Outlook freezing. I thought that this was caused that the Exchange server stopped responding. Then i thought that i might make a small script that pings destinations and writes the output to CSV file. The script are not working as expected as there is some kind of buffer under-run. Any thoughts on how to optimize the script, or if it's even possible would be greatly appreciated :)

[CmdLetBinding()]
    Param(
        $HostNames = "dr.dk"
    )
    BEGIN{
        $output = @()
        $data = @()
        $objects = @()
        [int]$counter = 0
        foreach ($hostname in $HostNames) { $objects += New-Object -TypeName PSObject -Property @{"Hostname"=$hostname;"IP"=([Net.DNS]::GetHostEntry($hostname).AddressList.IpAddressToString)  }}
    }
    PROCESS{
        Write-Host "Press q Key to stop testing"
        while ($true) {
            if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
                Write-Host "Stopping..."
                break
            }
            Write-Verbose "Defining Props"
            $props = @{
                    "Source"=($env:COMPUTERNAME);
                    "Date" = Get-Date -Format d
                    "Time" = Get-Date -Format T
            }
            Write-Verbose "Defining Object"
            $out = New-Object -TypeName psobject -Property $props
            foreach ($object in $objects) {
                $test = try {Test-Connection $object.ip -Count 1 -ErrorAction Stop| select -ExpandProperty ResponseTime } catch  { Write-Output "FAILED"}
                Add-Member -InputObject $out -Name $object.HostName -Value $test -MemberType NoteProperty
            }
            $data += $out
            $counter++
            Start-Sleep -Seconds 1
            if ($counter -eq 60) {
                if (Test-Path "C:\script\PingLog.csv") {
                    $dataold = Import-Csv "C:\script\PingLog.csv" -Delimiter ";"
                    $output += $dataold
                    $output += $data 
                    $output | Export-Csv -Path "C:\script\PingLog.csv" -Encoding "UTF8" -Delimiter ";" -NoTypeInformation
                } else {
                    $data | Export-Csv -Path "C:\script\PingLog.csv" -Encoding "UTF8" -Delimiter ";" -NoTypeInformation
                }
                $counter = 0
            }
        }
    }
    END{
        
        if (Test-Path "C:\script\PingLog.csv") {
            $dataold = Import-Csv "C:\script\PingLog.csv" -Delimiter ";"
            $output += $dataold
            $output += $data 
            $output | Export-Csv -Path "C:\script\PingLog.csv" -Encoding "UTF8" -Delimiter ";" -NoTypeInformation
        } else {
            $data | Export-Csv -Path "C:\script\PingLog.csv" -Encoding "UTF8" -Delimiter ";" -NoTypeInformation
    }

}


May 27th, 2015 9:07pm

What is your question?  What does the script have to do with outlook?

The script looks like a kludge that just stays busy for no reason.  Where did you get this script?

Start by asking a clear question.  What is the problem?  What are the errors?

Free Windows Admin Tool Kit Click here and download it now
May 27th, 2015 10:52pm

What is your question?  What does the script have to do with outlook?

The script looks like a kludge that just stays busy for no reason.  Where did you get this script?

Start by asking a clear question.  What is the problem?  What are the errors?

May 29th, 2015 2:03am

Simple if you ask a simple question:

Test-Connection mypc | Export-CSv Myfile.csv -NoType

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 2:40am

Simple if you ask a simple question:

Test-Connection mypc | Export-CSv Myfile.csv -NoType

May 29th, 2015 3:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics