Powershell:检查 AD 中的重复电子邮件
Powershell: Checking for duplicate email in AD
背景:
我正在尝试制作一个脚本来查看新用户的电子邮件 ($email
) 是否与现有的相同(这会导致错误)。我对对象有很好的补救性理解,所以这就是我到目前为止所拥有的(是的,它很丑):
$email = "smithj@company.com"
$mailcheck = Get-ADUser -filter * -Properties * | ForEach-Object {$_.mail}
$mailcheck | ForEach-Object {if ($email -eq $_.mail){"$email = $($_.mail) - Matching email"}else{"$email = $($_.mail) - No duplicate email"}}
问题 1:
该脚本与电子邮件不匹配。当我在 AD 中有匹配的电子邮件时,它无法识别它。
问题2:当只执行第二行时,索引不能正常工作。虽然它看起来像是一个连续的电子邮件列表,但如果用户根本没有电子邮件(空白),它实际上可能是这样的:
smithj@company.com
johnsonj@company.com
robertsr@company.com
doej@company.com
所以 $mailcheck[0]
returns smithj@company.com
而 $mailcheck[1]
returns 空白,尽管列表实际上看起来像这样:
smithj@company.com
johnsonj@company.com
robertsr@company.com
doej@company.com
结论:我真的只需要解决问题 1,但问题 2 激起了我的好奇心。谢谢。
您上面的做法确实效率低下。 -Properties *
将 return 每个 属性 用户,某些属性在处理能力方面是昂贵的 return。只使用你需要的属性。默认情况下 return 未指定参数的属性不需要使用 -Properties
指定,仅需额外的非默认属性。 -Filter *
还将匹配任何字段的任何值,有效地 returning 每个 ADUser
,进一步增加脚本执行所需的资源,因为您现在必须处理每个用户查找与该电子邮件匹配的任何帐户。
既然已经解决了,这里有一个更有效的方法来实现您的要求:
# Set the email address to search for
$emailAddress = 'box@domain.tld'
# Get all users where the email address matches what is set above
# Force it as an array so you can treat it like one even if only
# one or zero users are returned
$adUsers = @( Get-ADUser -Filter "EmailAddress -eq '${emailAddress}'" )
# Make sure no accounts were returned
# If there are, throw an error with the number of users and who they are
if( $adUsers ) {
throw "Found $($adUsers.Count) users matching EmailAddress ${emailAddress}: $($adUsers.SamAccountName -join ', ')"
}
通过使用过滤器只匹配特定的电子邮件地址,Powershell 不需要收集系统中的每个 AD 用户,或者遍历所有用户来查找特定的电子邮件地址。这将需要很长时间来检查,尤其是在较大的环境中,而根据电子邮件地址(或任何其他 属性)过滤 returned 对象会导致更快的操作和更少的数据筛选.
然后您可以检查 $adUsers
是否包含任何内容(除 0
之外的任何内容的数组计数计算为 $True
,您也可以使用 if( $adUsers.Count -gt 0 )
作为条件) , 如果是这样,像我上面那样抛出一个包含更多信息的错误。
评论问题更新:
为了回答您在评论中的其他问题,"I didn't know what object to compare $email
to"、EmailAddress
和 Mail
看起来都是有效的属性,但我不知道它们之间的区别。在我的环境中,Mail
和 EmailAddress
都填充了我的电子邮件地址,但一直使用 EmailAddress
并且没有 运行 使用它的问题。也许一个已被弃用,另一个是新的或什么的,但我不太确定。
还有还有一个 属性也叫proxyAddresses
,初步研究表明EmailAddress
和Mail
和它有关系,但我不是很了解。它没有填充在我的 ADUser
对象上,所以我不能用它四处寻找。
背景:
我正在尝试制作一个脚本来查看新用户的电子邮件 ($email
) 是否与现有的相同(这会导致错误)。我对对象有很好的补救性理解,所以这就是我到目前为止所拥有的(是的,它很丑):
$email = "smithj@company.com"
$mailcheck = Get-ADUser -filter * -Properties * | ForEach-Object {$_.mail}
$mailcheck | ForEach-Object {if ($email -eq $_.mail){"$email = $($_.mail) - Matching email"}else{"$email = $($_.mail) - No duplicate email"}}
问题 1: 该脚本与电子邮件不匹配。当我在 AD 中有匹配的电子邮件时,它无法识别它。
问题2:当只执行第二行时,索引不能正常工作。虽然它看起来像是一个连续的电子邮件列表,但如果用户根本没有电子邮件(空白),它实际上可能是这样的:
smithj@company.com
johnsonj@company.com
robertsr@company.com
doej@company.com
所以 $mailcheck[0]
returns smithj@company.com
而 $mailcheck[1]
returns 空白,尽管列表实际上看起来像这样:
smithj@company.com
johnsonj@company.com
robertsr@company.com
doej@company.com
结论:我真的只需要解决问题 1,但问题 2 激起了我的好奇心。谢谢。
您上面的做法确实效率低下。 -Properties *
将 return 每个 属性 用户,某些属性在处理能力方面是昂贵的 return。只使用你需要的属性。默认情况下 return 未指定参数的属性不需要使用 -Properties
指定,仅需额外的非默认属性。 -Filter *
还将匹配任何字段的任何值,有效地 returning 每个 ADUser
,进一步增加脚本执行所需的资源,因为您现在必须处理每个用户查找与该电子邮件匹配的任何帐户。
既然已经解决了,这里有一个更有效的方法来实现您的要求:
# Set the email address to search for
$emailAddress = 'box@domain.tld'
# Get all users where the email address matches what is set above
# Force it as an array so you can treat it like one even if only
# one or zero users are returned
$adUsers = @( Get-ADUser -Filter "EmailAddress -eq '${emailAddress}'" )
# Make sure no accounts were returned
# If there are, throw an error with the number of users and who they are
if( $adUsers ) {
throw "Found $($adUsers.Count) users matching EmailAddress ${emailAddress}: $($adUsers.SamAccountName -join ', ')"
}
通过使用过滤器只匹配特定的电子邮件地址,Powershell 不需要收集系统中的每个 AD 用户,或者遍历所有用户来查找特定的电子邮件地址。这将需要很长时间来检查,尤其是在较大的环境中,而根据电子邮件地址(或任何其他 属性)过滤 returned 对象会导致更快的操作和更少的数据筛选.
然后您可以检查 $adUsers
是否包含任何内容(除 0
之外的任何内容的数组计数计算为 $True
,您也可以使用 if( $adUsers.Count -gt 0 )
作为条件) , 如果是这样,像我上面那样抛出一个包含更多信息的错误。
评论问题更新:
为了回答您在评论中的其他问题,"I didn't know what object to compare $email
to"、EmailAddress
和 Mail
看起来都是有效的属性,但我不知道它们之间的区别。在我的环境中,Mail
和 EmailAddress
都填充了我的电子邮件地址,但一直使用 EmailAddress
并且没有 运行 使用它的问题。也许一个已被弃用,另一个是新的或什么的,但我不太确定。
还有还有一个 属性也叫proxyAddresses
,初步研究表明EmailAddress
和Mail
和它有关系,但我不是很了解。它没有填充在我的 ADUser
对象上,所以我不能用它四处寻找。