Search code examples
powershellmicrosoft-exchange

Is it possible to use Variables in ContentMatchQuery, in a NewComplianceSearch?


I am currently trying to use variables within the New-ComplianceSearch -ContentMatchQuery ($Variable), so I can make my content search more versatile rather than static.

However when I view the results the query doesnt read the $Variable as the variable value, but as just a string with the value of $Variable.

$complianceSearchName ='1st_Test'
$complianceSearchDesc = 'Test_1'
$keywordField='"Scree*" OR "PC*" OR "Somethin*"'
$senderField='Mailbox Name 1', 'Mailbox Name 2'

New-ComplianceSearch -Name $complianceSearchName -Description $complianceSearchDesc -ExchangeLocation $senderField -ContentMatchQuery  '($keywordField)(c:c)(sent=2020-02-01..2020-02-03)(received=2020-02-01..2020-02-03)'
Start-ComplianceSearch -Identity $complianceSearchName

Variables work when using the New-ComplianceSearch in the -Name, -Description and -ExchangeLocation, However, when using the $keywordField within the -ContentMatchQuery is where the error occurs.

Any help?


Solution

  • You need to put the query in double quotes to have the variable expansion work.

    $complianceSearchName ='1st_Test'
    $complianceSearchDesc = 'Test_1'
    $keywordField='"Scree*" OR "PC*" OR "Somethin*"'
    $senderField='Mailbox Name 1', 'Mailbox Name 2'
    
    New-ComplianceSearch -Name $complianceSearchName -Description $complianceSearchDesc -ExchangeLocation $senderField -ContentMatchQuery  "($keywordField)(c:c)(sent=2020-02-01..2020-02-03)(received=2020-02-01..2020-02-03)"
    Start-ComplianceSearch -Identity $complianceSearchName
    

    As you can see here variables are not expanded with single quotes.