locked
Requesting Distinguished Name; setting result to variable, then get group using variable in -filter. RRS feed

  • Question

  • I am currently working on a script to obtain a list of all groups that a user is a member of, and then output the results to a file. However, I am running into a filter error. If I run the command without the variable by inputting the distinguished name, it will run successfully. But as soon as I change to a variable, it's lost. Can anyone help me to understand where I am going wrong?

    $User = read-host -Prompt "Enter Username" 
    
    $DName = (get-mailbox -id $User).distinguishedname
    
        get-Mailbox $User | fl Name,Distinguishedname
    
            get-group -ResultSize Unlimited -Filter 'Members -eq "'$DName'"'

    My result is:

    Invoke-Command : Cannot bind parameter 'Filter' to the target. Exception setting "Filter": "Invalid filter
    a description of the filter parameter syntax see the command help.
    "Members -eq "" at position 13."
    At C:\Users\*****\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\***.****.com\******om.psm1:3805 char:29
    +             $scriptCmd = { & <<<<  $script:InvokeCommand `
        + CategoryInfo          : WriteError: (:) [Get-Group], ParameterBindingException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetGroup

    Thanks in advance. 

    Thursday, March 1, 2018 4:03 AM

All replies

  • If you simply want to get the group membership of a username, you can do this:

    $User = Read-Host -Prompt "Enter Username" 
    Get-ADPrincipalGroupMembership $user | Select-Object -Property Name

    -Tony

    Thursday, March 1, 2018 6:26 AM
  • I don't think I have that command.  Maybe:

    -Filter { Members -eq "$DName" }


    • Edited by JS2010 Thursday, March 1, 2018 6:47 AM
    Thursday, March 1, 2018 6:47 AM
  • Hi Tony, Thank you. I tried that one, but it gave me an error telling me that it is an unknown cmdlet. I supposed "ADPrincipalGroupMembership" is not an imported command on my exchange. Can I sub it with another command?

    Thursday, March 1, 2018 7:16 AM
  • Hi JS2010, I tried replacing the filter command with yours, but the end result was that blank. It did however return the distinguished name that was requested. But the output of group data was null. 
    Thursday, March 1, 2018 7:18 AM
  • Hi Tony, Thank you. I tried that one, but it gave me an error telling me that it is an unknown cmdlet. I supposed "ADPrincipalGroupMembership" is not an imported command on my exchange. Can I sub it with another command?

    Sorry.  That command requires the Active Directory PowerShell module installed.  You can easily install it on a server (there is no risk to do so):

    On desktops, you can install the Remote Server Administration Tools for Windows 10.


    -Tony

    • Edited by Tony MCP Thursday, March 1, 2018 8:25 AM
    Thursday, March 1, 2018 8:24 AM
  • Hi Tony, Unfortunately, I'm running W7 so I can't install the same pack. But for W7, I do have that feature enabled and installed. I'm still unable to use that cmdlet though. Perhaps its part of the new W10 feature set?

    <Unable to post screnshot due to account verification>

    Thursday, March 1, 2018 10:05 AM
  • On Windows 7 install RSAT (Remote Server Administration Tools). This gives you the AD module cmdlets. Can be downloaded here:

    https://support.microsoft.com/en-us/help/2693643/remote-server-administration-tools-rsat-for-windows-operating-systems


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, March 1, 2018 10:26 AM
  • With online systems this is easier.

    $user='jsmith'
    Get-Group |Where{$user -in $_.Members}


    \_(ツ)_/

    Thursday, March 1, 2018 4:32 PM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

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

    Friday, March 2, 2018 8:52 AM