unable to export data from loop

Welcome Forums General PowerShell Q&A unable to export data from loop

Viewing 8 reply threads
  • Author
    Posts
    • #217017
      Participant
      Topics: 6
      Replies: 23
      Points: 35
      Rank: Member

      I want to export the username and what group it was removed from to csv. I can not figure out the proper syntax to do this .

       

      Here is the code .  I have  tried to comment as much as i could. The code removes the users from groups and should add the results to the csv file. i have the write host that is telling me it is doing it but the results are not going to csv. No matter where i put the code or how i do it. Since this is a list it will be multiple users that will have to be in the csv file.

       

       

       

      • This topic was modified 9 months, 3 weeks ago by matthew moore. Reason: make the question clearer
    • #217083
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      Hello Matthew,

      First thing is the way you are assigning the output of everything in the foreach to $data, you may end up with unexpected information. You may not even know it’s happening without digging deeper. What I recommend is collecting each user/group combination that you act on in a new PSObject and then at the end exporting it all to csv at the end.

      Here is the CSV from my test

       

      I hope this helps.

      • This reply was modified 9 months, 3 weeks ago by Doug Maurer.
    • #217095
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      Hi Matthew,

      I’m not sure if my browser messed up or if my answer is being moderated. Just in case i’ll post it again. The way you’re assigning all the output of foreach group to $data, you may end up for more than you bargained for. What I recommend is controlling what is put in the $data variable via a new PSObject that contains each user/group combo that was acted upon. After collecting all the results you can export to CSV.

      Here is the CSV content from my test

      I hope this helps.

    • #217098
      Participant
      Topics: 6
      Replies: 23
      Points: 35
      Rank: Member

      Wow. I have been working on this all morning and your solution is simple. Thank you so much.

      I guess i will have to do some reading on array list and how to use it. I have never seen it done this way before.

       

      Thank you again.

    • #217170
      Participant
      Topics: 6
      Replies: 23
      Points: 35
      Rank: Member

      i have one more quick queston. Since this is new too me I added another property that i wanted – DN. it shows up on the report but i wanted to be in a different order. I guess the OCD in me.

      the report now shows – Group , Username , DN

      I would like to show up as Username , Group, DN

      I tried changing the order as below but that did not help. Thank you in advance. I am not sure what happen to the users reply either. ODD

       

       

      • This reply was modified 9 months, 3 weeks ago by matthew moore. Reason: thanking
      • This reply was modified 9 months, 3 weeks ago by matthew moore.
    • #217185
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      What you need is a ordered hashtable, which I believe didn’t come about until version 3.

      This should do what you want, as long as you’ve populated DisabledUsers.csv with a column of data titled DistinguishedName

       

    • #217191
      Participant
      Topics: 6
      Replies: 23
      Points: 35
      Rank: Member

      Odd. Now it is only showing the last result on the list only. If i have more than one it will not show it. Maybe it is replication. I will wait a bit. Thank you so much. This is helping me automate a lot of stuff.

      • This reply was modified 9 months, 3 weeks ago by matthew moore.
    • #217197
      Participant
      Topics: 6
      Replies: 23
      Points: 35
      Rank: Member

      thanks mate. It was replication. It is worked as expected.

    • #217206
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      No worries, I’ll get you the address where you can automate a portion of your income my way. 😉

Viewing 8 reply threads
  • The topic ‘unable to export data from loop’ is closed to new replies.