我想要这个运行连续ping并给我输出

时间:2014-02-25 23:16:06

标签: powershell powershell-v2.0

两个问题,

我想对两台服务器进行constaint ping,并输出到.csv文件。 下面的脚本只运行两次,输出不起作用。我是一个新手,所以请放轻松。

$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

1 个答案:

答案 0 :(得分:1)

输出正常。只需运行脚本然后Invoke-Item ServerStatus.csv

如果你想让它永远运行,只需将整个东西包裹在while循环中:

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