Page 1 of 1

Variable issue

Posted: Thu Nov 09, 2017 2:08 am
by barretsmile
Hi,
Probably a very simple resolution.
I would like to get the alias for users whos name starts with A,B or C from the get-mailbox command in Exchange. Then I want to take those users and add them to a group. Generally, Ive done this sort of thing with CSVs but id like to avoid that in this instance.
I can create a variable as below:
$AC = Get-Mailbox -RecipientTypeDetails UserMailbox -OrganizationalUnit $OUMbx | where {$_.DisplayName -Like "[A-C]*"}
When I run $AC, it does indeed list my users as expected.
Then I tried:
Get-ADUser Group-AC | Add-ADGroupMember -Members $AC
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
I always thought I could do $AC.Alias to get the aliases for example but that does not work.
Any ideas?

Re: Variable issue

Posted: Thu Nov 09, 2017 10:20 am
by cody m
My guess would be that since the members parameter accepts a set of user, group, and computer objects, rather than an array of users, you need to loop through each member that is stored in your $AC variable.

Re: Variable issue

Posted: Thu Nov 09, 2017 10:43 am
by jvierra
I already gave you the answer to that question.

Code: Select all

$AC = Get-Mailbox -RecipientTypeDetails UserMailbox -OrganizationalUnit $OUMbx | 
      where {$_.DisplayName -match '^[A-C]'}
You cannot use LIKE to match a RegEx pattern.

Also you cannot add a mailbox t]o an AD Group. Get the ADUser and add that.

See: https://social.technet.microsoft.com/Fo ... powershell