我正在尝试通过PowerShell搜索AD以根据显示名称获取电子邮件地址。显示名称是我唯一需要引用的内容。
CSV列标题为
DisplayName "John,Doe"
这是我正在运行的脚本:
$arrayDisplayNames = Import-Csv C:\3\names.csv
foreach ($objectDisplayName in $arrayDisplayNames) {
Get-Member -InputObject $objectDisplayName
$objectDisplayName.DisplayName
$stringFirstName = $objectDisplayName.DisplayName.Split(",")[0]
$stringLastName = $objectDisplayName.DisplayName.Split(",")[1]
$objectUserFromDisplay = Get-ADuser $objectDisplayName.DisplayName -Properties EmailAddress, mail
$objectUserFromFirstLast = Get-Aduser -Filter "givenName -like '$stringFirstName' -and sn -like '$stringLastName'" -Properties EmailAddress, mail
}
这是我的错误:
Get-ADuser : Cannot find an object with identity: 'John,Doe' under: 'DC=jungle,DC=com'. At line:10 char:26 + ... omDisplay = Get-ADuser $objectDisplayName.DisplayName -Properties Ema ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (John,Doe:ADUser) [Get-ADUser], ADIdentityNotFoundException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser Get-Aduser : The search filter cannot be recognized At line:12 char:28 + ... FirstLast = Get-Aduser -Filter "givenName -like '$stringFirstName' -a ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
答案 0 :(得分:0)
这条线错了。您正尝试将显示名称作为输入提供给为UserID保留的位置参数。因此,第10行的第一个错误。
$objectUserFromDisplay = Get-ADuser $objectDisplayName.DisplayName -Properties EmailAddress, mail
将其更改为:
$StringDisplayName = $objectDisplayName.DisplayName
$objectUserFromDisplay = Get-ADuser -Filter "Name -like '$StringDisplayName'" -Properties EmailAddress, mail
虽然,我怀疑你会得到任何结果,因为你的DisplayName字段中有一个“,”,这可能不是你的AD保存显示名称的方式。删除此行,或使用从FirstName和LastName组件创建的显示名称,中间有空格。例如:$DisplayName = "$stringFirstName stringLastName"
第12行的第二个错误,我认为是因为对FirstName和LastName进行了不正确的拆分。否则代码是准确的。
更改为:
$stringFirstName = ($objectDisplayName.DisplayName).Split(",")[0]
$stringLastName = ($objectDisplayName.DisplayName).Split(",")[1]
您可能还想将-like
更改为-eq
,因为您希望缩小搜索结果的范围,而不是返回AD可以找到的所有内容。
答案 1 :(得分:0)
根据Rohin的回答,我已经删除了逗号字符并放下了可以在下面使用的脚本,并指出我能够使用Active Directory进行测试并验证它&#39 ; s返回结果。
foreach ($objectDisplayName in $arrayDisplayNames) {
$stringFirstName = ($objectDisplayName.DisplayName).Split(",")[0]
$stringLastName = ($objectDisplayName.DisplayName).Split(",")[1]
# remove , character
$StringDisplayName = $objectDisplayName.DisplayName -replace ",", " "
$objectUserFromDisplay = Get-ADuser -Filter "Name -like '$StringDisplayName'" -Properties EmailAddress, mail
$objectUserFromDisplay|Select-object Name,EmailAddress,mail
}