Author Posts

June 25, 2015 at 8:19 am

Hi,

I'm trying to create one variable to each distribution list I got in Exchange Online, then, add to that variables the members of each group. So I wrote this code:


$Lists = Get-DistributionGroup | Sort Name

foreach ($List in $Lists){
New-Variable $List
${$List} = Get-DistributionGroupMember -Identity $List
}

The problem is that, when I try to see the content of the variables, they are empty!
I need those variables filled with the members so I can use the results to check with every user on another foreach code...
Any1?

June 25, 2015 at 8:44 am

Fixed it by using this:


New-Variable $List -Value (Get-DistributionGroupMember -Identity $List.DisplayName)

June 25, 2015 at 9:17 am

Ok... Even so I can't resolve what I need...


Write-Host "Getting all Exchange Online users ..."
$Users = Get-Mailbox -RecipientTypeDetails UserMailBox | Sort Name
Write-Host "Getting all distribution groups..."
$Lists = Get-DistributionGroup | Sort Name

Write-Host "Getting all members of " $Lists.Count "distribution groups..."
foreach ($List in $Lists){
New-Variable $List -Value (Get-DistributionGroupMember -Identity $List.DisplayName)
}

Write-Host "Check the user memberships"
foreach ($User in $Users) {
Write-Host "The user" $Usuario "is member of this groups:"
foreach ($List in $Lists){
if (${$List}.Name -contains $User.Name){
Write-Host $List.DisplayName
}
}
}

The problem is that the "if" statement isn't working as I want it to... And it's printing all the distribution groups.
How can I make so the ${$List} changes into ${List – Support} for example?

June 25, 2015 at 9:42 am

Solved using it like this:


foreach ($List in $Lists){
if ((Get-Variable -Name $List -ValueOnly).Name -contains $User.Name){
Write-Host $List.DisplayName
}
}

Thanks anyway!