Nested ForEach Loops

Welcome Forums General PowerShell Q&A Nested ForEach Loops

Viewing 5 reply threads
  • Author
    Posts
    • #263325
      Von
      Participant
      Topics: 4
      Replies: 15
      Points: 43
      Rank: Member

      Using Powershell to pull data from eDirectory, the data I’m trying to grab looks like:

      cn primary secondary
      123joe ABCD 5678
      123joe ABCD 8998
      123joe ABCD 1648
      123janis DEFG null
      123janis GHEX null

      Where a user ID might have one or more primary values, and zero or more secondary values.

      The code I’m working with allows me to entirely grab a user like 123joe in three rows with all three of his secondary identifiers, but this code won’t grab 123janis at all, who has no secondary identifiers. I’d like to fix the query to pull all IDs, including the ones with no secondary identifiers.

    • #263376
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 980
      Helping Hand
      Rank: Major Contributor

      This will never output data if there are no secondary items because of the ForEach logic on line 23. If $Item.secondary is null then the ForEach will operate 0 times, resulting in line 25 being skipped because that loop doesn’t do anything and therefore producing no data for $CSVoutput on line 28.

      Separating the primary and secondary foreach loops should solve the problem:

      (untested)

      Of course, this isn’t the best methodology.

    • #263397
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      What exactly are you trying to end up with?

    • #263403
      Von
      Participant
      Topics: 4
      Replies: 15
      Points: 43
      Rank: Member

      Trying to end up with three columns in a csv like:

      cn primary secondary
      123joe ABCD 5678
      123joe ABCD 8998
      123joe ABCD 1648
      123janis DEFG
      123janis GHEX

      Basically, for each user ID, grab all the primary values, and if there are any secondary values grab those too.

      But if the user ID doesn’t have a secondary value like 123janis, then just grab the user ID and primary value(s).

      I tried grokkit’s code and it looks like all the data is being dropped into the first column.

    • #263415
      Von
      Participant
      Topics: 4
      Replies: 15
      Points: 43
      Rank: Member

      Getting closer…

      The following code uses If and ElseIf to run a separate query based on whether Item.secondary is null or not null.

      When I use the Write-Host output, it looks correct on screen, but the CSV file ends up with all the data sitting in row 2 of the file, like:

      cn primary secondary
      123joe,ABCD,5678,123joe,ABCD,8998,123joe,ABCD,1648,123janis,DEFG,123janis,GHEX

      Any ideas how to get the $CSVOutput formatted correctly?

       

    • #263651
      Von
      Participant
      Topics: 4
      Replies: 15
      Points: 43
      Rank: Member

      Solved. A working version of the full script is pasted below. Made a modification based on this article (no longer using +=).

      This version includes grabbing two additional columns from eDirectory: firstname $fn and lastname $ln.

      Note: if you want to test in your environment, input your eDirectory ldap path, userID, and password. Also the “primary” and “secondary” attributes probably don’t exist in your eDirectory environment, so you’ll want to modify these accordingly. Also the $Query.Filter above will only grab user IDs beginning with “123” so modify accordingly.

      Your $eDirPath might look something like: ‘LDAP://your IP address/o=something/ou=somethingElse’

      Whereas your $eDirUser credentials might look something like: ‘cn=yourID,ou=something,o=somethingElse’

Viewing 5 reply threads
  • You must be logged in to reply to this topic.