Mark Minasi's Tech Forum
Register Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #1 
I have a spreadsheet that has a list of email addresses, phone numbers, and other fields. I need to be able to update users' phone numbers in AD that are in the spreadsheet.  Since the spreadsheet has email addresses I first need to match the email address from the spreadsheet with the email address in Active Directory.

Because the Set-AdUser parameter requires the sAMAccountName, the intermediate step is to get the sAMAccountName and use that has input for the Set-AdUser cmdlet.

My script is below:

$csvFile = Import-CSV "C:\test.csv"

$csvFile | % {
$employeePhoneNumber = $_.employeePhoneNumber
$employeeEmailAddress = $_.employeeEmailAddress

write-host "$employeePhoneNumber | $employeeEmailAddress "

$employeeSAM = Get-ADUser -Filter "mail -eq $($employee.employeeEmailAddress)" -Properties sAMAccountName | select sAMAccountName | select -ExpandProperty sAMAccountName

write-host "$employeeSAM"
}


The error appears at the line below:

$employeeSAM = Get-ADUser -Filter "mail -eq $($employee.employeeEmailAddress)" -Properties sAMAccountName | select sAMAccountName | select -ExpandProperty sAMAccountName

It says Get-ADUser : Error parsing query.

Any suggestions?


0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 243
Reply with quote  #2 
Yes, the -Filter expect a ScriptBlock, not a string. AND $employee is never set. AND in the -Filter syntax the attribute is called EmailAddress.

Try

-Filter {EmailAddress -eq $employeeEmailAddress}

or

-LDAPFilter "(mail=$employeeEmailAddress)"

AND you don't need to specify sAMACccountName, it gets returned anyway

AND, you can select a property and expand it in the same call:

select -ExpandProperty sAMAccountName




__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #3 
Thank you Evgenij! This worked!
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #4 
This worked:  -Filter {EmailAddress -eq $employeeEmailAddress} 




0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.