最近有个客户需要查看他们服务的Security Group都在哪些组下。

实际上他要查询的这个Security Group是跨域的,直接在跨域的情况下是直接查看memberof属性是空的

由于对powershell命令的不熟悉,写了如下脚本暂时解决了这个问题,但是。。效率超低。

今天和同事碰头,结果他用一条命令搞定了。但是有局限性,如果有兴趣的可以大家一起讨论。


我在这里贴出我写的代码:

$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

        }

    }

}


同事的代码:

Get-ADPrincipalGroupMembership smtpazure


局限性在于, get-adprincipalGroupMembership 目前我测试只能获取域内的memberof属性。并不能达到要求,如果有其他方法可以获取跨域的memberof属性,请大家帮助一起实现,谢谢!