我正在尝试使用PowerShell从MSMQ消息队列中删除特定消息。我知道在PowerShell 4+中引入了几个用于管理MSMQ队列和消息的新cmdlet,但是据我所知,这些cmdlet仍然无法帮助我实现删除特定消息的目标。
我试图做到的,是一个简单的脚本,连接到指定的队列,然后通过在队列中的每个消息循环,会在邮件正文中指定的搜索字符串,然后移动角度任何匹配的消息到另一个队列在同一服务器。我的搜索工作非常好,可以使用Send方法复制任何匹配项:
$source = new-object System.Messaging.MessageQueue($sourcequeuename)
$destination = new-object System.Messaging.MessageQueue($destinationqueuename)
$msgs = $source.GetAllMessages()
foreach ( $msg in $msgs )
{
#Extra logic here - check message body for search string etc...
$destination.Send($msg)
}
但是-复制后,从源队列中删除原始消息的唯一选择似乎是清除整个源队列,这也会删除所有其他消息,这是我不想做的!
$source.Purge()
我以为我正在使用新的(PowerShell 4+)'Move-MsmqMessage'cmdlet ...
Move-MsmqMessage -DestinationQueue $d -InputObject $q -Message $msg
但是,我正在努力使其正常工作-我不清楚是否可以将特定的消息对象传递给它。谁能给我一个使用PowerShell(任何版本)来循环浏览队列中的消息并使用此Move-MsmqMessage cmdlet移动特定消息的有效示例吗?
未经测试,但是您应该能够使用ReceiveById函数删除消息:
foreach ( $msg in $msgs )
{
#Extra logic here - check message body for search string etc...
$source.ReceiveById($msg.Id, [timespan]::FromMinutes(1))
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句