两个问题,
我想对两台服务器进行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
答案 0 :(得分:1)
输出正常。只需运行脚本然后Invoke-Item ServerStatus.csv
如果你想让它永远运行,只需将整个东西包裹在while循环中:
$servers = "server1","server2"
$collection = $()
while(1) {
foreach ($server in $servers)
{
...
}
$collection | Export-Csv -LiteralPath .\ServerStatus.csv -NoTypeInformation
}