因为我们没有在所有系统上可用的活动目录模块,所以我们使用的是ADSI。下面的代码通过使用AdsiSearcher从AD检索用户对象
$ADUser = ([AdsiSearcher]"(samaccountname=$SamAccountName)").FindOne()这将导致找到属性primarygroupid,它表示用户的域主组,通常是数字513。当我们有这个号码时,我们想要找到这个组的distinguishedName。但是,下面的代码是否很好--我想知道是否有更好的filter可以在FindAll()方法之后使用而不是过滤?
$searcher = [adsisearcher]'objectclass=group'
$searcher.PropertiesToLoad.Add('primarygrouptoken')
$searcher.PropertiesToLoad.Add('distinguishedName')
$searcher.FindAll() |
Where-Object { $_.Properties.primarygrouptoken -eq 513}像这样的东西会很棒,但这是不可能的
([adsisearcher]”(&(objectCategory=group)(primaryGroupid=513))”).FindOne()发布于 2019-10-22 17:54:22
primaryGroupToken是一个构造的属性,这意味着它实际上没有在数据库中物化,并且不能使用LDAP进行过滤。
为了构建一个等价的过滤器,我们需要查看它是如何构造的-- Active Directory中的主组令牌始终与组的objectSid属性的RID部分(相对标识符)相同。
因此,如果我们想通过它进行搜索,我们只需使用objectSid进行过滤就可以了:
# Obtain domain SID
$dncDN = ([adsi]"LDAP://RootDSE").defaultNamingContext
$dnc = [adsi]"LDAP://$dncDN"
$domainSID = [System.Security.Principal.SecurityIdentifier]::new($dnc.objectSid.Value, 0)
# Set the group ID we're looking for
$RID = 513
# Search for group by objectSid value:
([adsisearcher]"(&(objectCategory=group)(objectSid=${domainSID}-${RID}))").FindOne()https://stackoverflow.com/questions/58502340
复制相似问题