user770022
user770022

Reputation: 2959

I want this to run a Continous ping and give me the output

Two issues,

I want a constaint ping to the two servers, and output to a .csv file. The script below only runs twice and the output doesn't work. I'm a power newbie so please go easy.

$servers = "server1","server2"
$collection = $()
foreach ($server in $servers)
{
$status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
    $status["Results"] = "Up"    
}
else
{
    $status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}    
$collection | Export-Csv -LiteralPath .\ServerStatus.csv -NoTypeInformation

Upvotes: 0

Views: 1063

Answers (1)

Cole9350
Cole9350

Reputation: 5560

The output works fine. Just run the script and then Invoke-Item ServerStatus.csv

If you want it to run forever just wrap the whole thing in a while loop:

$servers = "server1","server2"
$collection = $()
while(1) {
    foreach ($server in $servers)
    {
        ...
    }    
    $collection | Export-Csv -LiteralPath .\ServerStatus.csv -NoTypeInformation
}

Upvotes: 1

Related Questions