0

I am trying to read information from a csv file that and use it to add phone numbers to our active directory if the email address is found in the csv file. (I know the EmailAddress attribute is not realiable, but it's all I have to work with based on where this data is being exported from). I have a powershell script of:

Import-Module ActiveDirectory
$csv = Import-Csv c:\users\user1\Desktop\userphones2.csv
foreach ($line in $csv) {
    Get-ADUser -Filter {EmailAddress -eq $line.email} | 
    Set-ADUser -OfficePhone $line.phone
}

When I run this, for each record in the data file I get an error of

Get-ADUser : Property 'email' not found in object of type: 'System.Management.Automation.PSCustomObject'

CSV file is structured this way:

#TYPE System.Data.DataRow
"email","phone"
"user@example.com","8885555555"
"user2@example.com","8885555552"

If I do a Write-Output in the foreach loop instead of trying to get the data, this works. I have found many different syntaxes describing how to put a variable in a line like this for these filters, but nothing has worked. How do I get this filter to work?

SausageBuscuit
  • 1,226
  • 2
  • 20
  • 34

1 Answers1

1

You are using the wrong name for the email property in AD. It's mail not EmailAddress. I tested this in PowerShell and it works for me.

Also you can't use $line.email in a filter PowerShell doesn't know how to handle it. I dont know the technical reason for this but I have never got it to work that way so I always put it in a variable. Try this:

Import-Module ActiveDirectory
$csv = Import-Csv c:\users\user\Desktop\userphones2.csv
foreach ($line in $csv) {
    $email = $line.email
    Get-ADUser -Filter {mail -eq $email} | 
        Set-ADUser -OfficePhone $line.phone
}
Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
Dane Boulton
  • 1,305
  • 1
  • 11
  • 15
  • Comparing against `EmailAddress` works just fine. The problem occurs because the property can't be accessed inside the filter scriptblock. – Ansgar Wiechers Jan 23 '15 at 22:33
  • So I have to use the mail property instead of EmailAddress within the scriptblock? That seemed to work. Thanks guys. – SausageBuscuit Jan 26 '15 at 16:28
  • 1
    As Ansgar said it should work either way. What the issue is is that you cant reference the csv value in the filter so you have to put it in a variable before you check it in the filter. Did that solve your issue? – Dane Boulton Jan 26 '15 at 22:32