How to convert results to properly display in columns?

Welcome Forums General PowerShell Q&A How to convert results to properly display in columns?

Viewing 5 reply threads
  • Author
    Posts
    • #184904
      Participant
      Topics: 4
      Replies: 9
      Points: 72
      Rank: Member

      Hello,
      I'm writing script that should read servers addressess from file and run ping command against each of them. Next I want to export data to csv file, but for results are correctly displayed only in list format.

      $results = @()
      $servers = (Get-Content .\servers.txt) -notmatch '^#'
      $servers | ForEach-Object { 
      $server = $_
      Write-Verbose "Pobrałem adres $_"
      $ping = Get-CimInstance -Query "select * from win32_pingstatus where Address='$_'" | select-object -Expandproperty ResponseTime
      $object = New-Object psobject -Property @{
      $server = $ping
      }
      $results += $object
      }
      $results | fl

      And I got result like below:

      google.com : 17
      onet.pl : 98
      gmail.com : 14
      

      When I display results as a table I got only one column:

      google.com
      ----------
              17
      

      How can I change that?

    • #184913
      Participant
      Topics: 8
      Replies: 1274
      Points: 1,033
      Helping Hand
      Rank: Community Hero

      There are a ton of scripts that check basic connectivity and do tests like a basic ping, so you can always do a search and find a ton of examples, but here is a revised example of what you are attempting:

      $servers = 'google.com','onet.pl','gmail.com'
      
      $results = foreach ($server in $servers) {
      
          [pscustomobject]@{
              Server = $server
              Response = Get-CimInstance -Query "select * from win32_pingstatus where Address='$server'" | select-object -Expandproperty ResponseTime
          }
      
      }
      
      $results
      
      • #184919
        Participant
        Topics: 4
        Replies: 9
        Points: 72
        Rank: Member

        Thank you Rob for reply, but this is not quite what I want.

        Result from your code is like below:

        Server     Response
        ------     --------
        google.com       67
        onet.pl          11
        gmail.com       119

        And my goal is to have result like below:

        google.com   onet.pl  gmail.com 
                67        11       119
        
    • #184922
      Participant
      Topics: 41
      Replies: 166
      Points: 571
      Helping Hand
      Rank: Major Contributor

      Reading the text file ....

      
      $results = @()
      (Get-Content c:\temp\servers.txt) -notmatch '^#' | ForEach-Object {
      $server = $_
      $results += [pscustomobject]@{
      Server = $server
      Response = Get-CimInstance -Query "select * from win32_pingstatus where Address='$server'" | select-object -Expandproperty ResponseTime
      }
      }
      $results
      
      
    • #184925
      Participant
      Topics: 41
      Replies: 166
      Points: 571
      Helping Hand
      Rank: Major Contributor

      There was a previous post about transposing columns to rows

      https://powershell.org/forums/topic/transpose-columns-to-rows/

    • #184991
      Participant
      Topics: 8
      Replies: 1274
      Points: 1,033
      Helping Hand
      Rank: Community Hero

      It's breaking the way that Powershell wants to store information because you're breaking filtering (e.g. $results | Where {$_.Server -eq 'google.com'}), but if you want to just format it pretty...

      $servers = 'google.com','onet.pl','gmail.com'
      
      $props = @{}
      foreach ($server in $servers) {
          $props.Add($server, (Get-CimInstance -Query "select * from win32_pingstatus where Address='$server'" | select-object -Expandproperty ResponseTime))
      }
      
      $results = New-Object -TypeName PSObject -Property $props
      
      $results
      
    • #185018
      Participant
      Topics: 4
      Replies: 9
      Points: 72
      Rank: Member

      Rob thank you. I think this is what I wanted 🙂

      Now I can run this script in loop for an hour, save ping results to csv file and create chart from it.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.