I'm trying to search AD through PowerShell to get email addresses based on display names. The display name is the only thing I have to reference.
The CSV column header is
DisplayName "John,Doe"
Here is the script that I am running:
$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
}
Here's my error:
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