locked
Variable issue RRS feed

  • Question

  • 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?

    Monday, November 6, 2017 11:17 PM

Answers

  • Hi,

    I agree with jrv. Both adding AD users to groups or adding Mailboxes to groups can achieve your requirement. The following script is adding Mailboxes, for your reference, hope it is helpful to you:
    $mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited -OrganizationalUnit $OUMbx
    $mailboxes | Where-Object {$_.DisplayName -match '^[a-c]'} | ForEach-Object {Add-DistributionGroupMember -Identity 'GroupAC' -Member $_}
    $mailboxes | Where-Object {$_.DisplayName -match '^[d-f]'} | ForEach-Object {Add-DistributionGroupMember -Identity 'GroupDF' -Member $_}

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, November 7, 2017 11:07 AM

All replies

  • $AC = Get-Mailbox -RecipientTypeDetails UserMailbox -OrganizationalUnit $OUMbx | where {$_.DisplayName -match '^[A-C]'}

    "like" does not use regular expressions.<o:p></o:p>



    \_(ツ)_/

    Monday, November 6, 2017 11:31 PM
  • Thats fine but my issue wasnt with that part of the script.

    When I run your suggestion followed by Get-ADUser Group-AC | Add-ADGroupMember -Members $AC

    I get:

    Get-ADGroup : Cannot convert 'a list of the users Display Names' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not
     supported.
    At line:1 char:12

    I just need to be able to somehow pull alias from the $ac variable.

    Tuesday, November 7, 2017 6:36 AM
  • You cannot add a mailbox to a group.  You can only add a user object or user identifier to a group.

    Get-AdUser returns users if you specify a user identifier.  You cannot specify a group name.

    Start be reading all of the help for the CmdLets and spend a bit of time learning basic AD.  After that you can ask the question that you need answered.  Right now your question doesn't make much sense.

    help Get-AdGroupMember -online
    help Get-AdUser -online
    help Get-Mailbox -online


    \_(ツ)_/

    Tuesday, November 7, 2017 6:42 AM
  • What I need to do is get mailboxes based on first letter (so A-C, D-F etc) Then I want to add the a-c users to a group (security) called GroupAC, d-f users added to GroupDF etc.
    Tuesday, November 7, 2017 8:49 AM
  • Hi,

    I agree with jrv. Both adding AD users to groups or adding Mailboxes to groups can achieve your requirement. The following script is adding Mailboxes, for your reference, hope it is helpful to you:
    $mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited -OrganizationalUnit $OUMbx
    $mailboxes | Where-Object {$_.DisplayName -match '^[a-c]'} | ForEach-Object {Add-DistributionGroupMember -Identity 'GroupAC' -Member $_}
    $mailboxes | Where-Object {$_.DisplayName -match '^[d-f]'} | ForEach-Object {Add-DistributionGroupMember -Identity 'GroupDF' -Member $_}

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, November 7, 2017 11:07 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script work?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, November 9, 2017 8:14 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, November 13, 2017 8:58 AM
  • Yes, worked a treat. Thanks
    Monday, November 13, 2017 1:15 PM