0
votes

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
2

2 Answers

1
votes

This Line is wrong. You are trying to give Display Name as input into the positional parameter reserved for UserID. Hence the first error at line 10.

$objectUserFromDisplay = Get-ADuser $objectDisplayName.DisplayName -Properties EmailAddress, mail

Change this to:

$StringDisplayName =  $objectDisplayName.DisplayName
$objectUserFromDisplay = Get-ADuser -Filter "Name -like '$StringDisplayName'" -Properties EmailAddress, mail

Although, I doubt you will get any result from this because your DisplayName field has a "," in it and that is probably not how your AD is saving the display Name. Either delete this line, or use a display name that you create from FirstName and LastName components with a space in between. For Eg: $DisplayName = "$stringFirstName stringLastName"

The second error at Line 12, I think is because of improper splitting done for FirstName and LastName. Otherwise the code is accurate.

Change to this:

$stringFirstName = ($objectDisplayName.DisplayName).Split(",")[0]
$stringLastName = ($objectDisplayName.DisplayName).Split(",")[1]

You might also want to change -like to -eq since you are looking to narrow down your search result than return everything AD could find.

1
votes

Just building on what Rohin's answer was, I've stripped out the comma characters and put the script that can be used below, noting that I was able to test with Active Directory and verify it's returning results.

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
}