获得ADSI -Powershell的广告小组成员
我无法使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如我所见,有两种方式,但是可能有一种更简单的方法来做到这一点。
一种是搜索所有用户的
成员
属性具有组的dickinedname
(这可能是不那么麻烦的方法):另一种方式是使用与相同的方法您在问题中使用:
与上面的相似,但是使用
adsisearcher
,在这种情况下,哪一个会更有效: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 theDistinguishedName
of the group (this might be the less cumbersome approach):The other way around is using the same approach as you're using in your question:
Similar as the one above, but using
adsisearcher
, not sure which one would be more efficient in this case:我在自己的系统上运行了您的代码的修改版本,因此我可以看到搜索字符串的实际外观:
请注意,我让
$ searchKey
来到控制台。当我这样做时,我会看到完整的杰出名称而不是samaccountName
。基于此结果,我更改了代码以寻找该值而不是samaccountName
,然后我看到了(大概)预期的结果:I ran this modified version of your code on my own system, so I could see what the search string actually looked like:
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 justsamAccountName
. Based on this result I changed the code to look for that value instead ofsamAccountName
, and then I saw (presumably) expected results:这对我有用:
This worked for me: