2 ForEach loops with output to SQL Server DB

Welcome Forums General PowerShell Q&A 2 ForEach loops with output to SQL Server DB

Viewing 7 reply threads
  • Author
    Posts
    • #225588
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      I have a butchered version of Dan Belcher’s ‘Active Directory to SQL’

      Which gets a list of ADGroups , Filtered in one loop Then Gets the ADGroupMembers for each group and output to SQL Server database.

      Getting the ADGroups , works , Getting the ADGroupMembers works however when writing to the SQL ServerDatabase it inserts each Group as expected but the members list is always the GroupMembers from the last Group.

      If I have 4 Groups each with varying number of members the output always show 4 groups all with the same member list.

      here is an abridged version of the script

       

      • This topic was modified 8 months, 3 weeks ago by GlynLamb75.
    • #225594
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      Glyn, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

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

      Seems you just need to move your assignment of members inside the groups loop?

    • #225615
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      Not exactly sure what you mean

      do you mean

       

    • #225618
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      Sorry, I reread you post and I think you have solved it.

      Thanks

       

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

      Good. Yeah I think you had everything you needed, just needed to move the $arrayofmembers assignment inside the group loop. I wasn’t sure what the variables at the bottom were, I assumed placeholder for where you write to SQL.

    • #225801
      Participant
      Topics: 17
      Replies: 1958
      Points: 4,020
      Helping Hand
      Rank: Community Hero

      Also, put the output in the PSObject:

    • #225867
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      Thanks for your responses. All fixed and working

Viewing 7 reply threads
  • The topic ‘2 ForEach loops with output to SQL Server DB’ is closed to new replies.