Help with creating multiple variables and adding info to them

This topic contains 3 replies, has 1 voice, and was last updated by  Vandrey Trindade 2 years, 3 months ago.

  • Author
    Posts
  • #26826

    Vandrey Trindade
    Participant

    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?

  • #26827

    Vandrey Trindade
    Participant

    Fixed it by using this:


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

  • #26828

    Vandrey Trindade
    Participant

    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?

  • #26830

    Vandrey Trindade
    Participant

    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!

You must be logged in to reply to this topic.