Get-ADUser - Property LoginName not found in PSCustomObject

Good Morning,

I have a question regarding a script I have written, it seems that 'Get-ADUser' does not work correctly when filtering on a property within a PSCustomObject, I get an error stating that the property is not found although it is definitely there as Intellisense picks it up, see the none working script below:

$LANDESKReport = Import-Csv C:\path\to\input.csv

#Cycle through each line in the CSV, for each user create a custom object to output to CSV
$CustomOutput = ForEach ($Row in $LANDESKReport)
{
$User = Get-ADUser -Filter {SAMAccountName -eq $Row.LoginName} -Properties mail -ErrorAction SilentlyContinue #Does not work, returns an error stating the property cannot be found

New-Object PSObject -Property @{
LoginName = $Row.LoginName
EmailAddress = $User.mail
DeviceName = $Row.DeviceName
Name = $Row.Name
Version = $Row.Version
}

$User = ""
}
# ONLY OUTPUT 1 KIND OF OBJECT!!!!
$CustomOutput | Export-Csv -path c:\path\to\output.csv -NoTypeInformation

I can fix this by declaring a variable of SAMAccountName above the line and using that variable in place of $Row.LoginName but why is this required? see working script below:

$LANDESKReport = Import-Csv C:\path\to\input.csv

#Cycle through each line in the CSV, for each user create a custom object to output to CSV
$CustomOutput = ForEach ($Row in $LANDESKReport)
{
$SAMAccountName = $Row.LoginName
$User = Get-ADUser -Filter {SAMAccountName -eq $SAMAccountName} -Properties mail -ErrorAction SilentlyContinue #This works
New-Object PSObject -Property @{
LoginName = $Row.LoginName
EmailAddress = $User.mail
DeviceName = $Row.DeviceName
Name = $Row.Name
Version = $Row.Version
}

$User = ""
}
# ONLY OUTPUT 1 KIND OF OBJECT!!!!
$CustomOutput | Export-Csv -path c:\path\to\output.csv -NoTypeInformation

 

Parents Reply Children
No Data