none
Cannot figure out syntax RRS feed

  • Question

  • 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
    }


    Dave

    Friday, August 21, 2015 5:03 PM

Answers

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


    Sorry - I missed that edit. I fixed it.


    \_(ツ)_/


    That seemed to work.  Here is the finished code that works:

    $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 |Select name,mailNickName,mail | Export-CSV -Path 'C:\t6\Exporttest5.csv' -Append -NoTypeInformation
            write-host $Filter
    }

    Thanks for the help.  Runs MUCH faster than the "Foreach ($filters in $filter)" type of syntax.


    Dave

    Friday, August 21, 2015 6:13 PM

All replies

  • 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 
        }


    \_(ツ)_/


    Friday, August 21, 2015 5:31 PM
  • 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 
        }


    \_(ツ)_/

    Same results.  It returns the names but, repeatedly so I end up of a several meg file.  And it does not search for those folks in the file it returns everyone in that OU I designate in my -Searchbase.

    It returns "mail -eq user1@email.com mail -eq user2@email.com mail -eq user3@email.com mail -eq user1@email.com mail -eq user2@email.com mail -eq user3@email.com mail -eq user1@email.com mail -eq user2@email.com mail -eq user3@email.com mail -eq user1@email.com mail -eq user2@email.com mail -eq user3@email.com"

    Not sure why it is returning the names more than once.

    Thanks for looking at this.

    Dave


    Dave

    Friday, August 21, 2015 5:59 PM
  • Change += to = so you'll only test on the current item in the loop.

    Friday, August 21, 2015 6:01 PM
  • Change += to = so you'll only test on the current item in the loop.

    Sorry - I missed that edit. I fixed it.


    \_(ツ)_/


    • Edited by jrv Friday, August 21, 2015 6:03 PM
    Friday, August 21, 2015 6:03 PM
  • Sorry - I missed that edit.


    \_(ツ)_/

    =]

    Happens to all of us.


    Friday, August 21, 2015 6:04 PM
  • Change += to = so you'll only test on the current item in the loop.


    Sorry - I missed that edit. I fixed it.


    \_(ツ)_/


    That seemed to work.  Here is the finished code that works:

    $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 |Select name,mailNickName,mail | Export-CSV -Path 'C:\t6\Exporttest5.csv' -Append -NoTypeInformation
            write-host $Filter
    }

    Thanks for the help.  Runs MUCH faster than the "Foreach ($filters in $filter)" type of syntax.


    Dave

    Friday, August 21, 2015 6:13 PM