locked
Powershell returns no results RRS feed

  • Question

  • Hi Everyone,

    i got a CSV file which contains the user mail address, i want to use PS to search their corresponding SamAccountName,CN,City,Office. so i wrote the bellowing script, but i cannot get any results, could someone help with me?

    Import-Module ActiveDirectory
    $mails = Import-Csv C:\PS\Mails.csv
    foreach ($mail in $mails) {
        Get-ADUser -Filter {mail -eq "$mail.mail"} -Server "dc001.corp.contoso.com" -Properties * | select samaccountname,cn,city,office
    }

    Monday, February 18, 2019 7:20 AM

Answers

  • The correct format is:

    Get-ADUser -Filter "mail -eq '$($mail.mail)'"


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Monday, February 18, 2019 8:54 AM
    • Marked as answer by Oliver Ko Tuesday, February 19, 2019 5:43 AM
    Monday, February 18, 2019 8:47 AM
  • Please format your code as code using the code posting tool provided on the edit bar of the post editor. Thanks.

    The correct syntax should be this:

    $mails = Import-Csv -Path 'C:\PS\Mails.csv'
    foreach ($mail in $mails) {
        Get-ADUser -Filter "mail -eq '$($mail.mail)'" -Server 'dc001.corp.contoso.com' -Properties CN, City, Office | 
            Select-Object -Property samaccountname, cn, city, office


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, February 18, 2019 8:53 AM
    • Marked as answer by Oliver Ko Tuesday, February 19, 2019 5:44 AM
    Monday, February 18, 2019 8:53 AM

All replies

  • The correct format is:

    Get-ADUser -Filter "mail -eq '$($mail.mail)'"


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Monday, February 18, 2019 8:54 AM
    • Marked as answer by Oliver Ko Tuesday, February 19, 2019 5:43 AM
    Monday, February 18, 2019 8:47 AM
  • Please format your code as code using the code posting tool provided on the edit bar of the post editor. Thanks.

    The correct syntax should be this:

    $mails = Import-Csv -Path 'C:\PS\Mails.csv'
    foreach ($mail in $mails) {
        Get-ADUser -Filter "mail -eq '$($mail.mail)'" -Server 'dc001.corp.contoso.com' -Properties CN, City, Office | 
            Select-Object -Property samaccountname, cn, city, office


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, February 18, 2019 8:53 AM
    • Marked as answer by Oliver Ko Tuesday, February 19, 2019 5:44 AM
    Monday, February 18, 2019 8:53 AM
  • Hi,

    thanks for ur reply, the script is working after i changed something according to ur advise.

    BTW,

    i am new beginner on PS scripting, i get confused about the characters like {} "" '' [], which command that i should use {}? and which i should ""?

    Tuesday, February 19, 2019 4:53 AM
  • thanks a lot
    Tuesday, February 19, 2019 5:04 AM
  • It is best to always use "" with the filter on AD commands.


    \_(ツ)_/

    Tuesday, February 19, 2019 5:15 AM
  • LOL, you are right, i tried the same scripts but with {} in the filter strings, also failed to get any results.
    Tuesday, February 19, 2019 5:43 AM
  • -filter takes a string.  Example 4 in the docs is wrong.

    Tuesday, February 19, 2019 3:20 PM
  • -filter takes a string.  Example 4 in the docs is wrong.

    No.  It can take either but an expandable string is required when variables are used.  Using quotes works on all forms of filters.  Braces do not.

    A filter expression is defined as a script block or a hash in most commands that use filters.  With AD a scriptblovk is evaluated in a scope that cannot always expand the variables.  The string is always evaluated before it is passed to the filter.

    .


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Tuesday, February 19, 2019 3:47 PM
    Tuesday, February 19, 2019 3:30 PM
  • -filter in this case is a string type.  A script block can be casted to a string, but this won't work in all cases.  I think having an official doc example (#4) using a script block causes confusion.  https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps

    Tuesday, February 19, 2019 5:37 PM
  • Well...yes and no.  It is actually either.  The filter method is overloaded to take both.  All of the examples show a scriptblock.

    Normally an SB casts to a string and is just a string of the contents of the SB.  Because of the overload the SB is passed as an SB and executed.  No variable substitution works outside f simple valuetypes.  To use object property references you can to use {} but must use a pure string which forces the string to be evaluated BEFORE it is passed.

    I just always use a string and then it is not necessary to think about it.  In PS1 I believe that only a script block worked because we always had to extract properties to a simple variable to use it and a string would throw errors.  The docs have never quite been in sync with the reality.


    \_(ツ)_/

    Tuesday, February 19, 2019 6:10 PM
  • Well...yes and no.  It is actually either.  The filter method is overloaded to take both.  All of the examples show a scriptblock.

    These look like strings to me.

    Example 1
    PS C:\> Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM"

    Example 2
    PS C:\> Get-ADUser -Filter 'Name -like "*SvcAccount"' | Format-Table Name,SamAccountName -A


    Required Parameters

    -Filter

    Type: String


    I submit that Example 4 is a typo that just happens to work:

    PS C:\> Get-ADUser -Filter {Name -eq "ChewDavid"} -SearchBase "DC=AppNC" -Properties "mail" -Server lds.Fabrikam.com:50000

    • Edited by JS2010 Tuesday, February 19, 2019 6:48 PM
    Tuesday, February 19, 2019 6:44 PM
  • Like I posted, the documentation is wrong and misleading.  It claims strin but all examples of the syntax show a scriptblock.

    From the docs:

    Filter syntax declaration -  <filter>  ::= "{" <FilterComponentList> "}"
    Filter example - Get-ADUser -Filter {EmailAddress -like "*"}

    Those are not strings.


    \_(ツ)_/

    Tuesday, February 19, 2019 7:15 PM
  • They are strings.  That's an error too.  The get-adcomputer doc says the same thing.

    • Edited by JS2010 Tuesday, February 19, 2019 9:55 PM
    Tuesday, February 19, 2019 9:55 PM
  • They are strings.  That's an error too.  The get-adcomputer doc says the same thing.

    What are you saying are a]strngs?

    This is not a string:

     Get-ADUser -Filter {EmailAddress -like "*"}


    \_(ツ)_/

    Tuesday, February 19, 2019 10:15 PM