Is there a way to make a foreach process only one thing first?

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 2 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #48131
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Hi,

    Is there a way to make a foreach loop to make one thing only for the first value and then ignore for the rest?
    I have a foreach that puts a HTML header for every distribution group that my organization has and then a second foreach to check if there is some members with a specific name on it.
    The way I made first, it shows all DGs and then the specific members... all separated ok.
    Then I've tried to make the headers appear inside the second loop... now it shows only the DGs that has that specific members on it, but the DG name is repeating for each specific member found.

    foreach ($DG in $DistributionGroups){ 
       $Member = $DG.DisplayName
       $HTML += ConvertTo-Html -Head $CSS -PreContent "$Member"
       foreach ($DGMember in $DistributionsGroupsMembers) {
               if((Get-Variable -Name $DG -ValueOnly).DisplayName -contains $DGMember){
                  $HTML += $DGMember
                  }
               }
       }
    

    What I was wondering is if there is a way to make that second and third lines to run one time only on the second foreach.
    I want to show only the DGs that has that specific members using one header only for each DG.
    Sorry if the explanation was a mess =/

    #48133
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You can use the Break statement to exit out of a foreach loop, but honestly, if you only want it to execute once, why bother with the loop? You can just execute those statements once on the first object in $DistributionsGroupsMembers .

    #48139
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Dave Wyatt,

    Thanks for the very fast reply.
    The problem is how to execute those statements once?
    Let's suppose this scenario:

    4 distribution groups in the organization

    List – Administration
    List – DBA
    List – HR
    List – Support

    The specific members that I want are only in the "List – HR" distribution group.
    If I choose to run those statements once in the first object, it won't add any header because what I need is in the third group... right?

    Also, I said it wrong... I need the headers to be inside the if statement on the second loop.
    I only want the header if there is on or more specific members on it...

    #48142
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm sorry, I'm not really following the logic. Don't focus on code concepts for the moment; what are you trying to do? You've got a list of distribution groups, and are converting them to HTML in some way. What should that HTML table look like?

    #48144
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    I'll share an image to explain it better... sorry =p

    https://s31.postimg.org/9pq8spbl7/Imagem.png

    The first two distribution groups doesn't have any member of those I was looking for.
    The "Colaboradores" and "Colaboradores BH" have more than one.
    If I add those statements inside the if on the second loop, I get only the DGs that have those members... but I will get one header for each member too...
    Like this:

    https://s32.postimg.org/s8k86meat/Imagem1.png

    it helps?

    #48147
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I see. While I don't follow your code exactly, here's what I would do in sort of pseudo-code:

    foreach ($group in $groups)
    {
        $filteredMembers = @($group.Members | Where { $someCondition })
    
        if ($filteredMembers.Count -gt 0)
        {
            Write-GroupHeader
            Write-FilteredMembers
        }
    }
    
    #48149
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Here's another approach which more closely matches what you asked for (doing certain statements only once through a loop):

    foreach ($DG in $DistributionGroups){
       $needsHeader = $true
    
       foreach ($DGMember in $DistributionsGroupsMembers) {
               if((Get-Variable -Name $DG -ValueOnly).DisplayName -contains $DGMember){
                  if ($needsHeader) {
                      $Member = $DG.DisplayName
                      $HTML += ConvertTo-Html -Head $CSS -PreContent "$Member"
                      $needsHeader = $false
                  }
    
                  $HTML += $DGMember
               }
            }
       }
    
    #48151
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Dave Wyatt,

    Thanks! I will modify my code =]

    #48182
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Dave Wyatt,

    Thanks a lot once again.
    The second approach worked perfectly!

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.