2017-10-08 44 views
0
$servers = Get-Content "servers.txt" 
$collection = $() 
foreach ($server in $servers) 
{ 

    $status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f G) } 
    if (Test-Connection $server -Count 1 -ea 0 -Quiet) 
    { 
     $status["Results"] = "Online" 
     $test = Test-Connection -ComputerName $server -Count 4 -ErrorAction SilentlyContinue 
     $stat = $test | Measure-Object -Property ResponseTime -Average -Maximum -Minimum 
     $status["Avg"] = $stat.Average 
     $status["Lost"] = [string]$(100*(4-$test.Count)/4) + ' %' 

    } 
    else 
    { 
     $status["Results"] = "Down" 
    } 
    New-Object -TypeName PSObject -Property $status -OutVariable serverStatus 
    $collection += $serverStatus 
} 
$collection | Out-GridView | Export-Csv -LiteralPath .\ServerStatus3.csv -NoTypeInformation 

如何输出为表格?但这样的列是在一个特定的序列
如果我删除“失去”列,它显示为一个表。 See 但是,正如您所看到的,第一列未完全显示。
我需要的最重要的事情是每个集成都能立即显示在屏幕上!格式化集合的输出

+0

'$ collection | Select-Object -Property <以所需顺序在此处插入字段> | Out-GridView | [...]' –

回答

0

如果您正在寻找只是格式化,您可以使用Format-Table-Wrap参数。

但是,如果使用格式表,则会丢失对象结构,结果将只是文本,不能导出为CSV文件。

$ collection | Out-GridView | Export-Csv -LiteralPath。\ ServerStatus3.csv -NoTypeInformation

上述代码行将生成一个空的csv文件。你必须把-Passthru参数设置为Out-GrifView,我建议不要使用Out-GridView,因为它会打破这里的自动化。

0
  1. 可以建立一个对象直接
  2. 你应该存储值没有统一为平均或丢失(统一为打印,存储始终没有统一 - >如果一天你想使用没有必要这个值来重新拆分此字符串)
  3. 不nessary通过数组来传递,输出写你的对象,并使用管道使用

试试这个:

Get-Content "c:\temp\servers.txt" | %{ 

    if (Test-Connection $_ -Count 1 -ea 0 -Quiet) 
    { 
     $test = Test-Connection -ComputerName $_ -Count 4 -ErrorAction SilentlyContinue 

     [pscustomobject]@{ 
     ServerName=$_ 
     Results="Online" 
     TimeStamp=Get-Date 
     Avg=($test | Measure-Object -Property ResponseTime -Average).Average 
     PercentLost=100*(4-$test.Count)/4 
     } 
    } 
    else 
    { 
     [pscustomobject]@{ 
     ServerName=$_ 
     Results="Down" 
     TimeStamp=Get-Date 
     Avg=$null 
     PercentLost=$null 
     } 
    } 

} | export-csv "c:\temp\resultstats.csv" -notype 
+0

感谢您的帮助,但是如何让它将每行连续输出到控制台?如果我把'export-csv''ft -AutoSize'放在最后,然后打印表格,但我想立即看到执行结果。 –

+0

$ result = get_content .......在最后,将$ result打印到屏幕上 – Esperento57