Cannot figure out syntax
I have the following script where I am trying to use email to perform a search using a filter to perform a quicker query against AD.  I keep getting a

Get-ADuser : The search filter cannot be recognized"

error.  It returns the following from for the value sometime (mail=user1@mail.com)(mail=user2@mail.com)(mail=user3@mail.com) and this is not even the accounts in my CSV file.  it is getting them raw from the -Searchbase I am using.  Anyone have an idea where I am going wrong on my syntax?  Help appreciated, thanks in advance.

#$LdapFilter = '(|'
#$LdapFilter = '('
$file = 'C:\t6\Users_Email.csv'
Import-Csv $file | ForEach-Object {
    $LdapFilter += "(mail=$($_.mail1))"
}

#$LdapFilter += ')'
#write-host $ldapFilter

Get-ADuser -LdapFilter $ldapFilter -Properties Name,employeeID,mail,mailNickName,employeeID | Foreach-Object{


Get-ADuser -Filter * -Server "dc01.mydomain.com" -SearchBase {"ou=users,ou=USwherever,.dc=usamer,dc=company,dc=com"} -Properties Name,employeeID,mail,mailNickName,employeeID | Select name,employeeID,mailNickName | Export-CSV -Path "C:\t6\Employee_Info.csv" -NoTypeInformation -Append

August 21st, 2015 1:06pm

Here. Do it this way.

$file = 'C:\t6\Users_Email.csv'
 Import-Csv $file | 
    ForEach-Object {
        $filter="mail -eq '$($_.mail1)'"
        Get-ADuser -Filter $filter -Properties Name,employeeID,mail,mailNickName,employeeID 
    }
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 1:34pm

Here. Do it this way.

$file = 'C:\t6\Users_Email.csv'
 Import-Csv $file | 
    ForEach-Object {
        $filter += "mail -eq '$($_.mail1)'"
        Get-ADuser -Filter $filter -Properties Name,employeeID,mail,mailNickName,employeeID 
    }
August 21st, 2015 2:02pm

Change += to = so you'll only test on the current item in the loop.
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 2:05pm

Change += to = so you'll only test on the current item in
August 21st, 2015 2:06pm

Sorry - I missed that edit
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 2:07pm

Change += to = so you'll only test on the current item in the

August 21st, 2015 2:16pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics