Joe G
asked on
How do I find and match AD accounts with the existing script to include their emails from AD
My original post was solved (https://www.experts-exchange.com/questions/28668675/Scripted-to-find-samid-from-list-of-users-out-of-Active-Directory.html?anchorAnswerId=40847974#a40847974) but now i need to gather the email address from the AD user account when a perfect match comes up. Any help please?
ASKER
didn't work. see below
Exception setting "Email": "Property 'Email' cannot be found on this object; make sure it exists and is settable."
At D:\PSemailfindfile.ps1:18 char:41
+ $Result.Email = $PossibleMatches.mail
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationExceptio n
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "Email": "Property 'Email' cannot be found on this object; make sure it exists and is settable."
At D:\PSemailfindfile.ps1:18 char:41
+ $Result.Email = $PossibleMatches.mail
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationExceptio
+ FullyQualifiedErrorId : ExceptionWhenSetting
Oops, try this:
$UserFile = "C:\Temp\test.txt"
Import-Module ActiveDirectory
Get-Content -Path $UserFile | % {
$Result = "" | Select-Object -Property FirstName, LastName, PerfectMatch, PossibleMatch, Email
$FirstName, $LastName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
$Result.FirstName = $FirstName
$Result.LastName = $LastName
$SamId = $FirstName.SubString(0, 1) + $LastName
If ($PossibleMatches = @(Get-ADUser -Filter "sAMAccountName -like '$($SamId)*'" -Properties mail)) {
$Result.PossibleMatch = ($PossibleMatches | Select-Object -ExpandProperty SamAccountName) -join ", "
If ($PossibleMatches.Count -eq 1) {
$Result.PerfectMatch = $PossibleMatches.SamAccountName
$Result.Email = $PossibleMatches.mail
} Else {
If ($PerfectMatches = @($PossibleMatches | ? {($_.GivenName -eq $FirstName) -And ($_.Surname -eq $LastName)})) {
If ($PerfectMatches.Count -eq 1) {
$Result.PerfectMatch = $PerfectMatches.SamAccountName
$Result.Email = $PossibleMatches.mail
}
}
}
}
$Result | Write-Output
}
ASKER
almost there... when i do the export-csv function i get a "System.Object[]" for the emails. Any way around this?
ASKER
however without the export-csv the onscreen output shows the emails but in a different format from the original script (it shows the rows instead of columns)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that did it thank you!
you're very welcome!
Open in new window