ADGroup Membership test working partially.

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Chris Henning Chris Henning 7 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #35184
    Profile photo of Chris Henning
    Chris Henning
    Participant

    Attempting to AD User's membership in specified group (true/False). Fails when attempting to run via pipeline. Works when split up in parts. Need another pair of eyes to review Failing code below.


    # Working Code
    $GroupName = (Get-ADGroup "Domain Admins").DistinguishedName
    $User = get-aduser "Username" -Properties memberof
    $user.memberof -contains $GroupName

    # Failing code
    get-aduser "Username" -Properties memberof | select name, @{n="IsMember"; E="($_.memberof -contains {(Get-ADGroup 'Domain Admins').DistinguishedName})"}

    #35185
    Profile photo of Yuan Li
    Yuan Li
    Participant

    You should use {} not ""

    get-aduser "username" -Properties memberof | select name, @{n="IsMember"; E={$_.memberof -contains (Get-ADGroup "Domain Admins").DistinguishedName }}

    #35187
    Profile photo of Matt Howard
    Matt Howard
    Participant

    Another way you could do it:

    (Get-ADUser -Identity 'Username' -Properties memberOf).memberOf -contains (Get-ADGroup -Identity 'Domain Admins').DistinguishedName
    

    This will just output true/false.

    #35245
    Profile photo of Chris Henning
    Chris Henning
    Participant

    Thank you for your quick responses. Tested both ways, both worked great!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.