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

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 =/

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 .

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…

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?

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?

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
    }
}

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
           }
        }
   }

Dave Wyatt,

Thanks! I will modify my code =]

Dave Wyatt,

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