Variable issue

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
barretsmile
Posts: 1
Last visit: Mon Dec 04, 2017 2:37 am

Variable issue

Post 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?
cody m

Re: Variable issue

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Variable issue

Post 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
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked