PowerShell Search-邮箱搜索查询问题

PowerShell Search-Mailbox Search Query Troubles

尝试编写一些 PowerShell 脚本。我想在一段时间后从收件箱中删除语音消息。该代码获取组织内的所有邮箱并将它们通过管道传输到搜索邮箱。然后我需要使用 -searchquery 选项查询邮箱。它似乎不喜欢我将 Date 变量放入查询的方式。

$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

编辑 1:PowerShell 在执行脚本时声明如下:

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

编辑二:将我问题中的代码调整为有效的代码。感谢大家的帮助!

根据 Message Properties and Search Operators for In-Place eDiscovery(a fairly small subset of Message Properties Indexed by Exchange Search),-SearchQuery 语法有点偏差。

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.

减少不是绝对必需的引号将有助于避免需要转义它们。它还支持其他运算符来比较日期等内容。 试试这个 -SearchQuery:

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

关于我自己的租户需要注意的一件事 Search-Mailbox 根本不会搜索附件,尽管附件是 New-MailboxSearch 的 "supported" 关键字。我不得不求助于在正文中寻找词语以获得预期的结果。我确实等了几个小时,看看它是否索引速度慢,但没有任何改变。也许你会有更好的运气。

在另一个笔记中 this page 提到:

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

这不是绝对要求。在不关闭它的情况下,用户可以恢复语音邮件。对你的情况来说可能没什么大不了的。