获得ADSI -Powershell的广告小组成员

发布于 2025-01-24 04:54:36 字数 367 浏览 0 评论 0原文

我无法使用Active Directory模块在特定的AD组中获取用户的SamAccountName。我该如何使用ADSI做到这一点?

我尝试过:

$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
    $Searcher = [adsisearcher]"(samAccountName=$_)"
    $searcher.FindOne().Properties
}

但是我看到了此消息:

samaccountname搜索过滤器无效。

我该怎么做?

I cannot use the Active Directory Module to get the SamAccountName of the users in a specific AD-group. How can I do this with ADSI?

I've tried:

$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
    $Searcher = [adsisearcher]"(samAccountName=$_)"
    $searcher.FindOne().Properties
}

But I see this message:

The samAccountName search filter is invalid.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2025-01-31 04:54:37

如我所见,有两种方式,但是可能有一种更简单的方法来做到这一点。

一种是搜索所有用户的成员属性具有组的dickinedname(这可能是不那么麻烦的方法):

$group = 'CN=myGroup, OU=myOU, DC=myDomain'
$searcher = [adsisearcher]"(&(objectclass=user)(objectcategory=person)(memberof=$group))"
$members = foreach($member in $searcher.FindAll()) {
    $member.Properties.samaccountname
}

另一种方式是使用与相同的方法您在问题中使用:

$group = 'CN=myGroup, OU=myOU, DC=myDomain'
$adsi = [adsi]"LDAP://$group"
$members = foreach($member in $adsi.member) {
    $isUser = [adsi]"LDAP://$member"
    if('person' -in $isUser.objectclass) {
        $isUser.samaccountname
    }
}

与上面的相似,但是使用adsisearcher,在这种情况下,哪一个会更有效:

$members = foreach($member in $adsi.member) {
    $check = [adsisearcher]"(&(distinguishedname=$member)(objectclass=user)(objectcategory=person))"
    if($isUser = $check.FindOne()) {
        $isUser.Properties.samaccountname
    }
}

There are 2 ways around this as I see it, there might be an easier way of doing it though.

One is to search for all users which's memberOf attribute has the DistinguishedName of the group (this might be the less cumbersome approach):

$group = 'CN=myGroup, OU=myOU, DC=myDomain'
$searcher = [adsisearcher]"(&(objectclass=user)(objectcategory=person)(memberof=$group))"
$members = foreach($member in $searcher.FindAll()) {
    $member.Properties.samaccountname
}

The other way around is using the same approach as you're using in your question:

$group = 'CN=myGroup, OU=myOU, DC=myDomain'
$adsi = [adsi]"LDAP://$group"
$members = foreach($member in $adsi.member) {
    $isUser = [adsi]"LDAP://$member"
    if('person' -in $isUser.objectclass) {
        $isUser.samaccountname
    }
}

Similar as the one above, but using adsisearcher, not sure which one would be more efficient in this case:

$members = foreach($member in $adsi.member) {
    $check = [adsisearcher]"(&(distinguishedname=$member)(objectclass=user)(objectcategory=person))"
    if($isUser = $check.FindOne()) {
        $isUser.Properties.samaccountname
    }
}
四叶草在未来唯美盛开 2025-01-31 04:54:37

我在自己的系统上运行了您的代码的修改版本,因此我可以看到搜索字符串的实际外观:

$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
    $searchKey = "(samAccountName=$_)"
    $searchKey
    $Searcher = [adsisearcher]$searchKey 
    # $searcher.FindOne().Properties
}

请注意,我让$ searchKey来到控制台。当我这样做时,我会看到完整的杰出名称而不是samaccountName。基于此结果,我更改了代码以寻找该值而不是samaccountName,然后我看到了(大概)预期的结果:

$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
    $Searcher = [adsisearcher]"(distinguishedName=$_)"
    $searcher.FindOne().Properties
}

I ran this modified version of your code on my own system, so I could see what the search string actually looked like:

$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
    $searchKey = "(samAccountName=$_)"
    $searchKey
    $Searcher = [adsisearcher]$searchKey 
    # $searcher.FindOne().Properties
}

Note the point where I let $searchKey come to the console. When I do this, I see values with the full distinguished name instead of just samAccountName. Based on this result I changed the code to look for that value instead of samAccountName, and then I saw (presumably) expected results:

$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
    $Searcher = [adsisearcher]"(distinguishedName=$_)"
    $searcher.FindOne().Properties
}
爱冒险 2025-01-31 04:54:37

这对我有用:

$group = 'CN=myGroup, OU=myOU, DC=myDomain'
$adsi = [adsi]"LDAP://$group"
$members = foreach($member in $adsi.member) {
$isUser = [adsi]"LDAP://$member"
if('person' -in $isUser.objectclass) {
$isUser.samaccountname
}
}
$members

This worked for me:

$group = 'CN=myGroup, OU=myOU, DC=myDomain'
$adsi = [adsi]"LDAP://$group"
$members = foreach($member in $adsi.member) {
$isUser = [adsi]"LDAP://$member"
if('person' -in $isUser.objectclass) {
$isUser.samaccountname
}
}
$members
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文