gpt4 book ai didi

powershell - Get-ADUser 错误 : Invalid enumeration context

转载 作者:行者123 更新时间:2023-12-02 22:34:53 24 4
gpt4 key购买 nike

我前几天发布了这个问题 Extract e-mail from grouped objects

$OuUser = @{}

$OuUser = Get-AdUser -Properties * -Filter * -SearchBase "domain"

$Duplicates = $OuUser | Select samaccountname, mail,UserPrincipalName |
Group-Object Mail | Where{$_.Count -gt 1}

$Duplicates | Select Name,Count,@{l='Accounts';e={($_.Group|Select -Expand samaccountname) -join ';'}} |
Export-CSV E:\Damo\Duplicates.csv -NoTypeInfo

代码在一个域上运行良好,针对 OU 中的一小组用户进行测试。

在我要测试的域上进行测试时,其中有很多用户,此代码失败。 OU 中有不是电子邮件格式的电子邮件地址。它指向错误的 Get-ADUser。

Get-ADUser : The server has returned the following error: invalid enumeration c
ontext.
At C:\scripts\CountEmailsDup.ps1:4 char:21
+ $OuUser = Get-AdUser <<<< -Properties * -Filter * -SearchBase 'ou=external,o
u=user accounts,dc=bizdir,dc=nzpost,dc=co,dc=nz' -SearchScope OneLevel
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : The server has returned the following error: inv
alid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.Ge
tADUser

我不明白为什么我在一个域上收到此错误,而在另一个域上却没有。

最佳答案

这里最大的问题是您向 Get-ADUser 询问了很多。根据您的评论,您正在吸引超过 900,000 个帐户。最重要的是,您正在提取这些用户的所有属性。那里有一丝疯狂。

虽然我不是很清楚你的错误是什么意思,但我知道每个收到它的人都会返回很多用户,而你显然是。缓解此问题的第一步是使用 Get-ADUser-ResultPageSize。您的里程可能会有所不同,但您需要试验要返回的记录数。 500-1000 通常是一个好的开始。

我永远不会使用 -Properties * 除非我是为一个用户拉动并且想看到一切。我强烈怀疑您是否在函数中使用了所有这些属性。为了效率,将自己限制在你需要的范围内。您显然需要指定 Mail

由于您是基于 mail 属性进行处理,另一件事是将您的结果限制为仅具有填充邮件属性的结果。您可以根据您对

的评论做一些过滤,例如“ ”、“....”(来自 Vesper 的评论)或“ @

There are some email field with 123 and . in them, so I will have to use length -gt 3 or something to skip them.

对此我不确定,而且我没有样本数据来检验理论,但使用管道应该也会有所帮助,而不是保存结果只是为了在管道中使用它们。

Get-AdUser -Properties mail -Filter 'mail -like "*@*"' -SearchBase "domain" -ResultPageSize 1000 | 
Group-Object Mail |
Where{$_.Count -gt 1} |
Select Name,Count,@{l='Accounts';e={($_.Group|Select -Expand samaccountname) -join ';'}} |
Export-CSV E:\Damo\Duplicates.csv -NoTypeInfo

关于powershell - Get-ADUser 错误 : Invalid enumeration context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com