首页 > 解决方案 > 从域本地组中导出用户

问题描述

你能帮我下面的代码吗?
它工作得很好,但我需要samAccountNamenot thedistinguishedName并且仅通过distinguishedName替换samAccountName.

这里是:

$s = "LDAP://" + (Get-ADGroup "Name of Group").DistinguishedName
([ADSI]$s) | select -ExpandProperty member|
             select @{Name='members';Expression={[string]::j}}

谢谢 !

标签: powershellactive-directory

解决方案


如果您希望获得 AD 组的成员资格,这应该可以解决问题:

(Get-ADGroup 'groupName' -Properties member).member.foreach({
    $_.split(',')[0].replace('CN=','')
})

这应该返回每个成员的,如果您已经拥有则samAccountName无需使用[adsi]ActiveDirectory Module

编辑:

所以,我假设samAccountNameName为你持有相同的价值,我的错。所以这就是你如何使用 AD 模块获得你需要的东西:

(Get-ADGroup 'groupName' -Properties member).member.foreach({
    (Get-ADUser $_).samAccountName
})

您还可以使用专门为此设计的 cmdlet:

(Get-ADGroupMember 'groupName').samAccountName

推荐阅读