How do I loop a variable properly in Powershell? (Exchange)

74 Views Asked by At

I'm trying, but not so good with Powershell. I'm trying to dynamically check and create many Dynamic distribution lists in Exchange. Seeing as we have a relatively high turn-over of staff in this department, I wanted to script the creation of their groups.

Here's what I have, but the contents of each dist group is showing ALL of the staff, not filtering their direct reports. I understand why it's doing this, just not sure how to fix it in the Powershell script:

$users = Get-ADUser -SearchBase "OU=Sales Managers,OU=User Accounts - Canada,DC=somedomain,DC=net" -filter * | Select-object Name,DistinguishedName
ForEach($user in $users)  
    {new-dynamicdistributiongroup -name "$($user.name) - Test Dyn Distribution Group" -OrganizationalUnit "OU=User Accounts - Sales Staff,DC=somedomain,DC=net" -recipientfilter {(recipienttype -eq'usermailbox' -or RecipientType -eq 'MailUser') -and (manager -like $user.DistinguishedName)}}

If I replace the variables user.name and user.distinguishedname with the managers' actual name, the list creates the contents as expected.

Thank you,

Brad

0

There are 0 best solutions below