0

So, I have a powershell script pulling data from an API and returning it as a formatted table, though the final product will be a list. The problem is, when I print the table, I have some entries that I do not want in my final output. Here's how I'm building the table:

Write-Output $xml.pnet_message_history_packet_response.imessage | Format-Table -Property @{Label="form_id"; Expression={$_.formdata.form_id}}
, ... other fields... , 
@{Label="Test"; Expression={$_.formdata.im_field.field_number}}

And here's what the output looks like:

form_id        DriverName     TrailerNumber  TrailerHub     DefectFound    TractorDefect  Field7         TractorDefectD TractorRemarks MoreTractorDef Field11        Field13       TrailerDefect Test

-------        ----------     -------------  ----------     -----------    -------------  ------         -------------- -------------- -------------- -------        -------       ------------- ----
125413                                                                                                                                                                                           {5, 6, 12,...

92477                                                                                                                                                                                            1
125413                                                                                                                                                                                           {5, 6, 12,...

52768          regglseton     4022           0              Yes            Yes            Windows        Windows        driver side... No             Windows        Windows                     {2, 3, 4, ...
52768          Robert F       4035           111            No                                                                                                                                   {2, 3, 4, ...
53420          rs8                                                                                                                                                                            {8, 12, 13...

But what I want is to remove all the entries where the form_id is anything BUT 52768. I want to return the same data but only where form_id is 52768. To clarify further, I want the output like this:

form_id        DriverName     TrailerNumber  TrailerHub     DefectFound    TractorDefect  Field7         TractorDefectD TractorRemarks MoreTractorDef Field11        Field13       TrailerDefect Test

-------        ----------     -------------  ----------     -----------    -------------  ------         -------------- -------------- -------------- -------        -------       ------------- ----
52768          regglseton     4022           0              Yes            Yes            Windows        Windows        driver side... No             Windows        Windows                     {2, 3, 4, ...
52768          Robert F       4035           111            No                                                                                                                                   {2, 3, 4, ...

What is the best method to do this? Write the table to a text file and process the file line by line? I was hoping to avoid having to parse another string and was hoping to just narrow down the data printed in the script, but I will of course accept any answer that can help find the best simple solution!

Thank you!

2
  • 1
    Is a simple Where-Object possible to see if form_id -eq "52768"? Commented Oct 4, 2018 at 15:23
  • Yes, that's what I was looking for I just was having trouble figuring out where to put it. Seems silly but I couldn't find it for whatever reason. Commented Oct 4, 2018 at 16:53

1 Answer 1

1

Before piping to Format-Table, try filtering by form_id property using Where-Object:

Write-Output $xml.pnet_message_history_packet_response.imessage | Where-Object {$_.form_id -eq "52768"} | Format-Table

Or using the in operator:

Write-Output $xml.pnet_message_history_packet_response.imessage | Where-Object {$_.form_id -in "52768"} | Format-Table

This should remove the unwanted entries from the table.

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

1 Comment

This is what I was looking for, thank you so much! Answer accepted.

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.