Количество сообщений на MSMQ через Powershell

Для окон при установке пакета, который вы вводите:

python -m pip install [packagename]
9
задан Irwin 18 February 2010 в 04:52
поделиться

4 ответа

Итак, я увидел это: Что я могу делать с 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');

И это сработало.

4
ответ дан 4 December 2019 в 09:12
поделиться

Это работало на меня

[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null

$AlertCount = 39

$queuePath = ".\private$\test.pdpvts.error"

$queue = New-Object System.Messaging.MessageQueue $queuePath


If($queue.GetAllMessages().Length -gt $AlertCount)
{
    Send-MailMessage -To "Me" -From "Alerts" -Subject "Message queue is full" -Credential mycridentials -UseSsl -SmtpServer mail.google.com
}
0
ответ дан 4 December 2019 в 09:12
поделиться

В PowerShell Community Extensions есть набор команд управления MSMQ. Попробуйте эти команды и посмотрите, поможет ли какая-нибудь из них (возможно, Get-MSMQueue):

Clear-MSMQueue
Get-MSMQueue
New-MSMQueue
Receive-MSMQueue
Send-MSMQueue
Test-MSMQueue

Примечание: Попробуйте взять дистрибутив на основе модуля beta 2.0 - только не забудьте "разблокировать" zip перед распаковкой.

1
ответ дан 4 December 2019 в 09:12
поделиться

Решение, предложенное Ирвином, далеко не идеальное.

Существует вызов .GetAllMessages , который вы можете сделать, чтобы сделать это за одну проверку, а не за цикл foreach.

$QueueName = "MycomputerName\MyQueueName" 
$QueuesFromDotNet =  new-object System.Messaging.MessageQueue $QueueName


If($QueuesFromDotNet.GetAllMessages().Length -gt $Curr)
{
    //Do Something
}

.Length указывает количество сообщений в данной очереди.

2
ответ дан 4 December 2019 в 09:12
поделиться
Другие вопросы по тегам:

Похожие вопросы: