formatting and piping outout from external command

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5847

    by veroli at 2013-04-25 06:03:10

    Hello all,
    i am trying to process the output from the netstat command but turn it into a one liner!
    currently i have the following code

    $results = netstat -a | select-object -skip 4
    convertfrom-csv (($results -replace "\s+",",").trim(",")) -header Protocol,Local_Address,Foreign_Address,State | out-gridview

    I am happy with this from an output point of view but how i can turn this into one line? i am not sure how to pipe $results into the second command
    As you can see it takes the output from netstat strips off the top four lines, removes trailing spaces and formats into a csv so it can be nicely output into out-gridview.

    regards
    dan

    by ArtB0514 at 2013-04-25 06:09:32

    This worked for me:
    $results = ((netstat -a | select-object -skip 4) -replace "\s+",",").trim(",") |
    convertfrom-csv -header Protocol,Local_Address,Foreign_Address,State | out-gridview -PassThru

    by veroli at 2013-04-25 06:36:37

    thanks much appreciated, sometimes it just takes another set of eyes doesnt it, i think i was getting messed up with the brackets in my attempts as
    i was building this command from much smaller pieces but couldnt get the last bit done.
    thanks

    Dan

    by eisenbergz at 2013-04-25 07:00:35

    You must be using v3. When I run that line, Art, I get "Method invocation failed because [System.Object[]] doesn't contain a method named 'trim'. ". It sees the results of ((netstat -a | select -skip 4) -replace "\s+",",") as an array object (which doesn't have a Trim method). Also, there is no -PassThru parameter for Out-GridView. Is there anything wrong here besides I'm using v2?

    If I run it without the .trim() part, it works. I just have to add another header name so that all the data shows up.
    (-header Null, Protocol,Local_Address,Foreign_Address,State)

    Or, if I don't fight it and just roll with it, ((netstat -a | select -skip 4) -replace "\s+",",") |
    foreach {$_.trim(",")} | convertfrom-csv -header Protocol,Local_Address,Foreign_Address,State | out-gridview
    works properly.

    Ze'ev

    by ArtB0514 at 2013-04-25 08:22:41

    Yep, you're right about PS 3. Although PS2 won't perform the trim method against the entire array, it will against the individual members. And because Out-GridView doesn't have -PassThru, as you correctly mentioned, In order to save the array, you need to use two lines of code. So, here's how I'd do this in PS2 (not too different from your solution):
    $results = netstat -a | select-object -skip 4 | foreach {($_ -replace "\s+",",").trim(",")} |
    convertfrom-csv -header Protocol,Local_Address,Foreign_Address,State
    $Results | out-gridview

    by rob_ful at 2013-04-25 12:06:49

    You could use this approach for external commands: Try creating a function that will convert the output into an PSCustomObject which you can output to any format.
    #requires -version 3.0
    Function Get-Netstat {
    $data = netstat -a | select-object -skip 4
    $lines = $data | foreach { $_.trim()}
    $lines | foreach { $temp = $_ -split "\s+"
    [PSCustomObject]@{
    Protocol = $Temp[0]
    Local_Address = $Temp[1]
    Foreign_Address = $Temp[2]
    State = $Temp[3]
    }
    }
    }
    < #
    Get-Netstat
    Get-Netstat | Out-GridView
    #>

    by ArtB0514 at 2013-04-25 12:45:15

    Here's the PowerShell 2 version of Rob's approach (tested to work on both V2 and V3):

    #requires -version 2.0
    Function Get-Netstat {
    netstat -a | select-object -skip 4 | foreach {
    $temp = ($_).Trim() -split "\s+"
    New-Object PSObject -Property @{
    Protocol = $Temp[0]
    Local_Address = $Temp[1]
    Foreign_Address = $Temp[2]
    State = $Temp[3]
    }
    }
    }
    $Results = Get-Netstat
    $Results | Out-GridView

You must be logged in to reply to this topic.