【发布时间】:2012-08-01 08:49:17
【问题描述】:
是否可以从 bat 文件中清除 msmq 队列?
基本上我想制作一个 bat 文件,或者至少是一些快速简单的东西,以便未经培训的员工可以在不知道任何 shell 或管理工具的情况下单击并修复
有人可以帮助我朝正确的方向前进吗?
【问题讨论】:
标签: batch-file msmq
是否可以从 bat 文件中清除 msmq 队列?
基本上我想制作一个 bat 文件,或者至少是一些快速简单的东西,以便未经培训的员工可以在不知道任何 shell 或管理工具的情况下单击并修复
有人可以帮助我朝正确的方向前进吗?
【问题讨论】:
标签: batch-file msmq
用于清除本地机器上所有私有队列的 PowerShell 脚本:
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$MachineName=(get-wmiobject win32_computersystem).name
[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("$MachineName") | % { $_.Purge(); }
如https://stackoverflow.com/a/11793579/1235394 中所述,最简单的执行方法是:
purge-private-msmq-queues.ps1
在同一文件夹中创建脚本文件purge-private-msmq-queues.cmd,内容如下:
powershell -executionpolicy Unrestricted .\purge-private-msmq-queues.ps1
【讨论】:
通过实用程序管理的任务包括:
别忘了powershell,看看PowerShell Community Extensions
更新
打开powershell,逐行写入
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$queueName = '.\Private$\testQueue'
$queue = new-object -TypeName System.Messaging.MessageQueue -ArgumentList $queueName
$queue.Purge()
从 cmd 调用 powershell
从 cmd 调用脚本的最简单方法。
powershell.exe -executionpolicy Unrestricted C:\purgemsmq.ps1
【讨论】:
此代码有效:
[Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
$Name=(get-wmiobject win32_computersystem).name
$QName=(
"FormatName:Direct=OS:$name\System$;DEADXACT",
"FormatName:Direct=OS:$name\System$;DEADLETTER"
)
foreach ($Q in $Qname){
$MessageQueue = New-Object System.Messaging.MessageQueue($Q)
$MSGCount=$($MessageQueue.GetMessageEnumerator2()).count
IF($MSGCount){
$MessageQueue.Purge()
Write-Host "$Q has been purged of $MSGCount messages." -ForegroundColor green
}
Else{
Write-Host "$Q is clean"}
}
【讨论】: