none
Issue with complex queries

    Question

  • I am trying to use the search-mailbox -searchquery with multiple criteria.

    I have a table with a list of users:

    $users

    usera

    userb

    userc

    $Subject = "String that is the subject"

    $fromuser = "userd@domain.com"

    Trying the following command:

    foreach ($user in $users) {Search-Mailbox -id $user -EstimateResultOnly -SearchQuery "(subject:$Subject )  AND (from:$fromuser)"}

    I would expect the results:

    RunspaceId       : 0fd4ab0d-9f94-41ac-b3b7-56b9e7f1a2cc
    Identity         : /usera
    TargetMailbox    :
    Success          : True
    TargetFolder     :
    ResultItemsCount : 1
    ResultItemsSize  : 8.621 KB (8,828 bytes)

    for each user in $users

    What I get is:

    The property keyword isn't supported.
        + CategoryInfo          : InvalidArgument: (:) [], ParserException
        + FullyQualifiedErrorId : [Server=xxxxxxxxxxxx,RequestId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,TimeStamp=x/xx/xxxx  x:xx:xx PM] [FailureCategory=Cmdlet-ParserException] AD7B1AB3
        + PSComputerName        : xxxxxxxxxxxx.xxxx.xxx

    If I change the searchquery to '(subject:"string that is the subject")  AND (from:username@domain.com)" it works correctly.

    Any assistance will be greatly appreciated.

    Monday, April 16, 2018 6:57 PM

Answers

  • You might try:

    "(subject:`"$Subject`") AND (from:`"$fromuser`")"


    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    • Marked as answer by James Horn Wednesday, April 18, 2018 8:54 PM
    Monday, April 16, 2018 9:32 PM
    Moderator

All replies

  • You might try:

    "(subject:`"$Subject`") AND (from:`"$fromuser`")"


    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    • Marked as answer by James Horn Wednesday, April 18, 2018 8:54 PM
    Monday, April 16, 2018 9:32 PM
    Moderator
  • Hi James,

    Try the following command and check again:

    foreach ($user in $users) {Search-Mailbox -identity $user -EstimateResultOnly -SearchQuery "(subject:'$Subject')AND(from:'$fromuser')"}

    I have tested in my lab, it works as expected, as below:

    Hope it helps,


    Best Regards,
    Niko Cheng


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, April 17, 2018 7:10 AM
    Moderator