PowerShell Search-Mailbox Search Query Troubles

旧时模样 提交于 2019-12-14 04:00:58

问题


Attempting my hand at some PowerShell scripting. I would like to remove Voice Messages from Inbox's after a certain period of time. The code gets all mailboxes within an organization and pipes them to a Search-Mailbox. Then I need to query the mailboxes using the -searchquery option. It does not seem to like how I am placing the Date variable into the query.

$Date = (Get-Date).AddDays(-19)
$Test = $Date.toString("MM/dd/yyyy")
$SB = [scriptblock]::create("Sent: $Test AND Attachment:`"VoiceMessage.wav`"")
Get-Mailbox | Search-Mailbox -TargetMailbox "Example" -TargetFolder "Searching" -SearchQuery $SB -LogOnly -LogLevel Full

Edit 1: PowerShell is stating the following when the script is executed:

Search-Mailbox : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:3 Char:25

Edit Two: Adjusted the Code in my question to the code that works. Thanks for all the help everyone!


回答1:


The -SearchQuery syntax is a little off according to the Message Properties and Search Operators for In-Place eDiscovery(a fairly small subset of Message Properties Indexed by Exchange Search).

Use syntax that matches the property:value format. Values are not case-sensitive, and they can’t have a space after the operator. If there is a space, your intended value will just be full-text searched. For example to: pilarp searches for "pilarp" as a keyword, rather than for messages that were sent to pilarp.

Reducing the quotes that aren't absolutely required will help avoid needing to escape them. It also supports other operators for comparing things like dates. Try this for the -SearchQuery:

"Attachment:VoiceMessage.wav AND Received<=$Date2"

One thing to note on my own tenant Search-Mailbox wouldn't search the attachments at all, despite Attachment being a "supported" key word for New-MailboxSearch. I had to resort to finding words in the body to get desired reults. I did wait a few hours to see if it might be slow indexing, but nothing changed. Perhaps you will have better luck.

On another note this page mentions:

If the mailbox from which you want to delete messages has single item recovery enabled, you must first disable the feature.

That is NOT an absolute requirement. Without turning that off, users may be able to recover the voicemail. Probably not a big deal for your situation.



来源:https://stackoverflow.com/questions/29779876/powershell-search-mailbox-search-query-troubles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!