Only exporting the the first object to CSV

Welcome Forums General PowerShell Q&A Only exporting the the first object to CSV

Viewing 7 reply threads
  • Author
    Posts
    • #214992
      Participant
      Topics: 3
      Replies: 13
      Points: 71
      Rank: Member
      $sg = Get-AzNetworkSecurityGroup -Name CCBDRSG01 | select -ExpandProperty NetworkInterfaces | select -Property id
      $sg | Export-Csv -Path c:\temp\sg.csv
      $text = Import-Csv -path c:\temp\sg.csv
      $text | ForEach-Object {$_.id.trimstart(“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/”) | Out-File c:\temp\sg2.csv}
      

      it will only export out the first entry in the CSV file.

      the reason I wanted to do this is Get-AzNetworkSecurityGroup does not support trim so i exported the data so I could use trim. If run the command with export it will display all the server interfaces.

      • This topic was modified 1 month, 3 weeks ago by sreed7743.
      • This topic was modified 1 month, 3 weeks ago by kvprasoon. Reason: code formatting
    • #215001
      Participant
      Topics: 24
      Replies: 172
      Points: 557
      Helping Hand
      Rank: Major Contributor

      get-help select-object -full

      read through all of the parameters, and i think you’ll find your answer pretty quickly 🙂

    • #215007
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      @David Schmidtberger Documentation is the -First place I would look too.

    • #215013
      Participant
      Topics: 3
      Replies: 13
      Points: 71
      Rank: Member

      I always look a the get-help before I post and I must be dense (as I’m fairly new to powershell) I just do not see how the answer is select-object when it come to Export-CSV. I believe it has something to do with for-each not running through all objects. Can you give me another push as I have not found the answer.

    • #215016
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      You need to move that curly brace back in a number of characters so that your last line would look like

      $text | ForEach-Object {$_.id.trimstart(“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/”)} | Out-File c:\temp\sg2.csv
      

      The way you had it enclosed the ‘Out-File c:\temp\sg2.csv’ code portion inside the ForEach loop, resulting in each iteration over-writing the one before, and sg2.csv ends up with the last value only.
      Also consider calling it sg2.txt instead of sg2.csv

    • #215055
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      @sreed7743

      Oops, thought you were asking how to only return the first item, not that you are only returning the first item. First, using trim like that is very specific and if anything in the path changed before what you want to get, then it would fail. Here is another method to get the end of a delimited string:

      #current method
      (“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/WhatIWant").trimstart(“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/")
      
      #better method
      (“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/WhatIWant" -split '/')[-1]
      

      As far as the problem at hand, there is no need to export, import and for loop in this scenario. You can use a calculated expression to get the value:

      $sg = Get-AzNetworkSecurityGroup -Name CCBDRSG01 | 
            select -ExpandProperty NetworkInterfaces | 
            select -Property @{Name='id';Expression={($_.id -split '/')[-1]}} 
      
    • #215142
      Participant
      Topics: 3
      Replies: 13
      Points: 71
      Rank: Member

      This work but I want to make sure I understand the part of the command

      @{Name=’id’;Expression={($_.id -split ‘/’)[-1]}}

      (@ = at) than we are looking for id Property named Name Expression this I’m unsure of however I believe it used to pass the pipeline property id and than split after / [-1] tells it to look at the last / that it finds. Is this correct?

    • #215178
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,334
      Helping Hand
      Rank: Community Hero

      Its the calculated property in PowerShell where we can create custom properties just by calculating it from existing property.
      E -> is the expression, literally any code can be put inside the scriptblock, her $_, currentelement in the pipeline and doing the split operation here for constructing the output

      Name -> is the name of the property being calculated, this can be any string.

      more insight here: https://mcpmag.com/articles/2017/01/19/using-powershell-calculated-properties.aspx

      • This reply was modified 1 month, 3 weeks ago by kvprasoon. Reason: post correction
Viewing 7 reply threads
  • You must be logged in to reply to this topic.