Jeetcu
Jeetcu

Reputation: 43

Add Members to Distribution Groups DLs

I have below .csv input file. These are the Distribution Groups. Level-4 is member of Level3, Level-3 is member of Level2, Level-2 is member of Level1, Level-1 is member of Level0.

enter image description here

So far, I have tried the below code. Starting to add Level-4 into Level-3. I have marked them for better understanding. However, I am not able to select the object and iterate in correct way using PowerShell.

e.g. First instance of Level-3 DL is 'DL_L3_US1' and it will contain members from Level-4 i.e. DL_L4_US1 and DL_L4_US2. How do I make this work?

$DLlist = Import-Csv C:\tempfile\Book2.csv

$Test = $DLlist | select Level3,Level4 | ? {$_.level3 -notlike $null }

foreach ($x in $DLlist)

{Add-DistributionGroupMember -Identity "$($x.Level3)" -Member "$($x.Level4)"}

Upvotes: 1

Views: 362

Answers (1)

Steven
Steven

Reputation: 7057

So my first answer wasn't correct. I misunderstood the question. Here's a new example:

$Props     = 'Level-0','Level-1','Level-2','Level-3','Level-4'
$GroupDefs = Import-Csv C:\temp\Book2.csv

For( $i = 0; $i -lt $GroupDefs.Count; ++$i )
{   # Loop through the objects that came from the CSV file...
    For( $p = 1; $p -lt $Props.Count; ++$p )
    {   # Loop through the known properties...
        $Prop      = $Props[$p]   # Convenience var
        $GroupProp = $Props[$p-1] # Convenience var

        If( $GroupDefs[$i].$Prop ) {
            # If the property is populated then loop backwards from $i-1, 
            # the group def record just prior to the current one.
            
            $Member = $GroupDefs[$i].$Prop
            :Inner For($r = ($i-1); $r -ge 0; --$r)
            {
                If( $GroupDefs[$r].$GroupProp ) {
                    # This means you hit the first record behind your current
                    # position that has the previous property populated.  Now 
                    # we know the group...
                    
                    $Group = $GroupDefs[$r].$GroupProp
                    Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf
                    Break Inner
                }            
            }
        }
    }
}

By using traditional For Loops we can find values at other positions. So, the way I worked this out is to nest a loop of the known properties in a loop of the group definitions. When I find a property that has a value, I then loop backward from the current position in $GroupDefs until I find the previous property populated. By that point, I've managed to find both the group and the member, so I can run the command.

Update for Comments:

There is no Dot sourcing in this program. Dot referencing is used. As previously mentioned . is an operator in the sense that the right-hand side will be evaluated before the property is referenced, hence we can use a variable or expression.

Imagine that you are going through the spreadsheet line by line. That is the outer loop of $GroupDefs. You can look at the value of $i as if it's a row#.

Now, for each of those rows, I want to look at each of a known set of property names. So we're going to loop through $Props. If one of those properties has a value, I then want to look at previous rows to find the nearest previous row where the previous property ($Prop[$p-1]) is populated. For example, if Level-2 is populated in Row# 3 I know I have to look back through rows 2, 1, 0 to find the first previous value for property Level-1. That part is the innermost loop, which moves backward through the $GroupDefs array from the current position -1 ($p = ($i-1)) to 0. When the first populated previous value for Level-1 is found I know that's the group name.

$Member is a convenience variable. It's set to $GroupDefs[$i].$Prop because the value of the given property is the member we wish to add to the yet to be determined group.

Note: $GroupDefs.$i returning nothing is expected. At any given moment $i is a number determined by the loop, but it is not the name of a property on the $GroupDefs array or any objects within it. So, it will neither return any property value from the array itself nor will it unroll (enumerate) any properties from the objects contained within.

Note: The value of $Prop will change as you loop through the properties and until a value is found on a property by the given name.

I realize this is confusing, but if you step through the code you will better understand what's happening. First, try to understand what's literally being done. Then the code should make more sense...

Upvotes: 2

Related Questions