我正在使用Psping检查具有端口的IP的延迟,并导出结果CSV而不显示标题。
我试过-select skip 1似乎不起作用,但有错误。
function CheckLatency
{
param([string[]]$servers)
foreach ($server in $servers)
{
$times = [ordered]@{ Server = "$server";TimeStamp = (Get-Date -f "yyyy-MM-dd hh:mm:ss"); Minimum = 0; Maximum = 0; Average = 0; }
$results = & "c:\users\test\desktop\psping.exe" -n 1 $server 2>&1 | select-string "Minimum"
if ($results) {
$results = $results.tostring() -split ","
foreach ($result in $results)
{
$result = ($result -replace "ms","").trim()
$parsed = $result -split " "
switch ($parsed[0])
{
"Minimum" {$times.Minimum = $parsed[2]}
"Maximum" {$times.Maximum = $parsed[2]}
"Average" {$times.Average = $parsed[2]}
}
}
new-object -type PSObject -prop $times
}
}
}
$csvFile = "C:\users\test\desktop\check$(get-date -f yyyy-MM-dd-hhmmss).csv"
CheckLatency 8.8.8.8:443,8.8.8.8:80 | Export-CSV -LiteralPath $csvFile -NoTypeInformation
带标题输出
Export-Csv
始终包含标题行(输出对象的属性名称列表)。
唯一的选择是在使用事实后使用纯文本处理删除该行:
$csvFile = "C:\users\test\desktop\check$(get-date -f yyyy-MM-dd-hhmmss).csv"
# ... your code that calls
# Export-CSV -LiteralPath $csvFile -NoTypeInformation
# Read the resulting file as an array of lines, skip the 1st line,
# and write the result back to the file.
(Get-Content -LiteralPath $csvFile) | Select-Object -Skip 1 | Set-Content -LiteralPath $csvFile
请注意(...)
周围的Get-Content
调用,该调用可确保在通过管道发送其行之前完整读取文件,从而允许在同一管道中重写输入文件。
请注意,如果写入过程中断,则文件损坏的可能性很小。
请注意,默认情况下,Windows PowerShell使用ANSI编码和Set-Content
,而Export-Csv
使用ASCII编码。-Encoding
根据需要使用。幸运的是,PowerShell Core始终默认为无BOM的UTF-8。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句