小智 8
这将列出计算机上的所有队列和消息数:
gwmi -class Win32_PerfRawData_MSMQ_MSMQQueue -computerName $computerName |
ft -prop Name, MessagesInQueue
Run Code Online (Sandbox Code Playgroud)
Windows Server 2012/2012 R2 和 Windows 8/8.1 下的 PowerShell 有一堆内置 Cmdlet,可以通过安装Microsoft 消息队列 (MSMQ) 服务器核心功能来使用。
# Get all message queues
Get-MsmqQueue;
# Get all the private message queues.
# Display only the QueueName and MessageCount for each queue.
Get-MsmqQueue -QueueType Private | Format-Table -Property QueueName,MessageCount;
Run Code Online (Sandbox Code Playgroud)
还有许多其他 Cmdlet 可用于队列管理和消息创建。IE
有关 MSMQ Cmdlet 帮助的完整列表,请参阅Windows PowerShell 中的 MSMQ Cmdlet,或者Get-Command -Module MSMQ如果您已经安装了该功能。
所以,我看到了这个:我可以用 C# 和 Powershell 做什么?并转到这里:http://jopinblog.wordpress.com/2008/03/12/counting-messages-in-an-msmq-messagequeue-from-c/
并做了这个
# Add the .NET assembly MSMQ to the environment.
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
# Create a new QueueSizer .NET class help to warp MSMQ calls.
$qsource = @"
public class QueueSizer
{
public static System.Messaging.Message PeekWithoutTimeout(System.Messaging.MessageQueue q, System.Messaging.Cursor cursor, System.Messaging.PeekAction action)
{
System.Messaging.Message ret = null;
try
{
// Peek at the queue, but timeout in one clock tick.
ret = q.Peek(new System.TimeSpan(1), cursor, action);
}
catch (System.Messaging.MessageQueueException mqe)
{
// Trap MSMQ exceptions but only ones relating to timeout. Bubble up any other MSMQ exceptions.
if (!mqe.Message.ToLower().Contains("timeout"))
{
throw;
}
}
return ret;
}
// Main message counting method.
public static int GetMessageCount(string queuepath)
{
// Get a specific MSMQ queue by name.
System.Messaging.MessageQueue q = new System.Messaging.MessageQueue(queuepath);
int count = 0;
// Create a cursor to store the current position in the queue.
System.Messaging.Cursor cursor = q.CreateCursor();
// Have quick peak at the queue.
System.Messaging.Message m = PeekWithoutTimeout(q, cursor, System.Messaging.PeekAction.Current);
if (m != null)
{
count = 1;
// Keep on iterating through the queue and keep count of the number of messages that are found.
while ((m = PeekWithoutTimeout(q, cursor, System.Messaging.PeekAction.Next)) != null)
{
count++;
}
}
// Return the tally.
return count;
}
}
"@
# Add the new QueueSizer class helper to the environment.
Add-Type -TypeDefinition $qsource -ReferencedAssemblies C:\Windows\assembly\GAC_MSIL\System.Messaging\2.0.0.0__b03f5f7f11d50a3a\System.Messaging.dll
# Call the helper and get the message count.
[QueueSizer]::GetMessageCount('mymachine\private$\myqueue');
Run Code Online (Sandbox Code Playgroud)
它奏效了。
| 归档时间: |
|
| 查看次数: |
26416 次 |
| 最近记录: |