2

I am using Psping to check the latency of IPs with port and export the result CSV without display the header.

I have tried -select skip 1 it seems not working but with errors.


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

Output with header

Please help remove the header

0

1 Answer 1

1

Export-Csv invariably includes a header row (the list of property names of the output objects).

Your only option is to remove that row after the fact, using plain-text processing:

$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

Note the (...) around the Get-Content call, which ensures that the file is read in full before its lines are sent through the pipeline, allowing the input file to be rewritten in the same pipeline.
Do note that there's a slight chance of the file getting corrupted if the write process is interrupted.

Note that Windows PowerShell uses ANSI encoding by default with Set-Content, whereas Export-Csv uses ASCII; use -Encoding as needed. PowerShell Core, fortunately, defaults to BOM-less UTF-8 consistently.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.