Find out which groups a user belongs to in PowerShell (MEMBEROF)

Source: Internet
Author: User
Tags samaccountname

Recently, a customer needs to see which groups are under the security group for which they serve.

In fact, the security group that he is querying is cross-domain and directly in the case of cross-domain is directly viewing the MemberOf property is empty

Because of the lack of familiarity with PowerShell commands, the following script has been written to solve this problem temporarily, but. Ultra-low efficiency.

I met my colleague today and he finished it with an order. But there are limitations, if interested, we can discuss together.


I'm here to post the code I wrote:

$groups =get-adgroup-filter ' Groupcategory-eq "Security"-and groupscope-eq "domainlocal" | Select Samaccountname-expandproperty sAMAccountName


foreach ($group in $groups)

{

Try

{

$members =get-adgroupmember-identity $group

}

catch [System.Exception]

{

Write-host "The Error Group name is $group"-foregroundcolor Red

}

foreach ($m in $members)

{

if ($m. Samaccountname-like "*dest_sg_name*")

{

Write-host $group

}

}

}


Colleague's code:

Get-adprincipalgroupmembership smtpazure


The limitation is that get-adprincipalgroupmembership currently I test can only get the memberof attribute within the domain. And can not meet the requirements, if there are other ways to get cross-domain memberof properties, please help to achieve together, thank you!

This article is from the "Richier" blog, make sure to keep this source http://richier.blog.51cto.com/1447532/1629757

Find out which groups a user belongs to in PowerShell (MEMBEROF)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.