【发布时间】:2015-06-17 00:55:29
【问题描述】:
我前几天发布了这个问题 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
我不知道为什么我在一个域上收到此错误,而在另一个域上却没有。
【问题讨论】:
-
您期望有多少用户?一次枚举太多用户可能会发生这种情况。您可以尝试使用
-ResultPageSize 500和Get-ADUser -
有超过 900,000 个帐户要处理,我正在尝试仅使用具有有效电子邮件地址的帐户并找到重复项然后导出到 csv。
-
那么我会尝试我的第一条评论,看看它是否有帮助。组织中的每个人都有一个电子邮件地址吗?您可以使用
-Filter仅获取具有有效电子邮件地址的帐户,以减少您带回的数据集。 -
这是有道理的,从 Active Directory 获取数据学到了很多东西。谢谢你。我现在正在使用 -Filter { mail - like "*"},它应该只给我带有电子邮件地址的帐户。有一些带有 123 和 .在它们中,所以我将不得不使用 length -gt 3 或其他东西来跳过它们。
-
您不仅可以按
-like "*"过滤,还可以按-like "....*"过滤,这只会返回mail属性至少有4 个符号长的用户。
标签: powershell active-directory