Link to home
Start Free TrialLog in
Avatar of Joe G
Joe GFlag for United States of America

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?
Avatar of NinjaStyle82
NinjaStyle82
Flag of United States of America image

No idea if this works, but i think it will.
$UserFile = "C:\Temp\test.txt"
Import-Module ActiveDirectory
Get-Content -Path $UserFile | % {
	$Result = "" | Select-Object -Property FirstName, LastName, PerfectMatch, PossibleMatch
	$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
}

Open in new window

Avatar of Joe G

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: (:) [], SetValueInvocationException
    + 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
}

Open in new window

Avatar of Joe G

ASKER

almost there... when i do the export-csv function i get a "System.Object[]" for the emails.  Any way around this?
Avatar of Joe G

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
Avatar of NinjaStyle82
NinjaStyle82
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joe G

ASKER

that did it thank you!
you're very welcome!