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 4 months, 2 weeks ago.

  • 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?

  • #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
               }
            }
       }
    
    • #48182
      Profile photo of Vandrey Trindade
      Vandrey Trindade
      Participant

      Dave Wyatt,

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

You must be logged in to reply to this topic.