DirectoryServices 过滤器和方法

发布于 2024-12-07 08:33:55 字数 724 浏览 1 评论 0原文

我正在尝试从 AD 获取 OU/DC 中所有用户的列表。

这就是我想到的:

$erroractionpreference = "SilentlyContinue"
function Get-GroupMembers {
    $filter = "(&(objectCategory=person)(objectClass=user))"
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.Filter = $filter
    $colResults = $objSearcher.FindAll()

        foreach ($member in $colResults) { 
            $member
        }
}
get-GroupMembers 

如果我将过滤器更改为

    $filter = "(&(objectCategory=person)(objectClass=user)("OU=Admin Accounts,DC=admin"))

“无返回”。这是为什么?
我还想显示某些值(如果帐户处于活动状态或禁用状态等),但如果我将 $member 传输到 gm,我什么也得不到。

任何帮助将不胜感激。

I'm trying to get a list of all users in an OU/DC from AD.

This is what I came up with:

$erroractionpreference = "SilentlyContinue"
function Get-GroupMembers {
    $filter = "(&(objectCategory=person)(objectClass=user))"
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.Filter = $filter
    $colResults = $objSearcher.FindAll()

        foreach ($member in $colResults) { 
            $member
        }
}
get-GroupMembers 

If I change the filter to

    $filter = "(&(objectCategory=person)(objectClass=user)("OU=Admin Accounts,DC=admin"))

Nothing returns. Why is this?
I would also like to display certain values (if the accounts is active or disabled etc) but if I pipe $member to gm, I get nothing.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(3

陪我终i 2024-12-14 08:33:55

如果要搜索特定的 OU,可以将其设置为搜索器对象的根:

$objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Admin Accounts,DC=admin")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objOU

您还可以控制搜索范围。以下是与您最相关的两个选项:

# Option 1: Return only users in the 'Admin Accounts' OU
$objSearcher.SearchScope = "OneLevel"

# Option 2: Return users in the 'Admin Accounts' OU or any level beneath it
$objSearcher.SearchScope = "SubTree"

除非您覆盖搜索根和范围,否则您将默认对当前域的根执行子树搜索。您可以在此 TechNet 文章中找到更多详细信息:

编辑: 正如 uSlackr 所指出的,您的 DC 分量确实看起来很可疑。我在示例中保留了它,但无论您使用什么方法,不完整/格式错误的基本对象名称都会破坏您的搜索。

If you want to search a specific OU, you can set it as the root of your searcher object:

$objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Admin Accounts,DC=admin")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objOU

You can also control the scope of your search. Here are the two most relevant options for you:

# Option 1: Return only users in the 'Admin Accounts' OU
$objSearcher.SearchScope = "OneLevel"

# Option 2: Return users in the 'Admin Accounts' OU or any level beneath it
$objSearcher.SearchScope = "SubTree"

Unless you override the search root and scope, you'll get the default of performing a SubTree search against the root of your current domain. You can find more detailed information in this TechNet article:

Edit: As uSlackr noted, your DC component does look fishy. I left it intact for my example, but an incomplete/malformed base object name will botch your search regardless of the method you use.

信仰 2024-12-14 08:33:55

直流分量不完整。它应该看起来像这样:

 $filter = "(&(objectCategory=person)(objectClass=user)("OU=Admin Accounts,DC=myco,dc=com"))

但是 dc=admin,dc=com 应该从本示例中的 AD 域名“admin.com”转换而来,这看起来不太正确

。使用 Microsoft AD cmdlet 会更容易。

get-aduser -filter * -searchbase "ou=test,dc=mycom,dc=com"

有关下载和使用 cmdlet 的信息,请访问 TechNet

The DC component is not complete. It should look something like this:

 $filter = "(&(objectCategory=person)(objectClass=user)("OU=Admin Accounts,DC=myco,dc=com"))

but the dc=admin,dc=com should translate from the AD domainname in this example "admin.com" which doesn't seem right

This is much easier with the Microsoft AD cmdlets.

get-aduser -filter * -searchbase "ou=test,dc=mycom,dc=com"

Information on downloading and using the cmdlets is available on TechNet

近箐 2024-12-14 08:33:55

您可以尝试使用适用于 ActiveDirectory 的 Quest ActiveRolesManagement Shell,可从此处下载:

http://www.quest.com/downloads/

该软件包是免费软件,是通过 powershell 进行 Active Directory 管理的更准确的 cmdlet。

You can try use the Quest ActiveRolesManagement Shell for ActiveDirectory downloadable from here:

http://www.quest.com/downloads/

The package is freeware and is the more accurate cmdlets for Active Directory management from powershell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文