Author Posts

July 25, 2016 at 1:35 pm

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

July 25, 2016 at 1:37 pm

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 .

July 25, 2016 at 1:44 pm

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

July 25, 2016 at 1:46 pm

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?

July 25, 2016 at 2:00 pm

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?

July 25, 2016 at 2:06 pm

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

July 25, 2016 at 2:10 pm

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

July 25, 2016 at 2:19 pm

Dave Wyatt,

Thanks! I will modify my code =]

July 25, 2016 at 3:54 pm

Dave Wyatt,

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