【问题标题】:Monitor MSMQ time of arrival监控 MSMQ 到达时间
【发布时间】:2020-09-12 00:43:55
【问题描述】:

我正在尝试编写一个脚本来监控服务器上的 MSMQ。我找到了this,它就像一个魅力,但我还需要知道到达的时间。

使用 Get-Member cmdlet 时,我会得到一个属性列表,但似乎没有一个属性能满足我的需要。

有谁知道如何获取到达时间?

/G

【问题讨论】:

  • 由于我还不知道消息本身是如何处理的,所以这只是暗中的一枪。到达时仅在代码中编写日志条目是一种可能的解决方案吗?
  • 我认为这不是必需的,因为如果您检查队列,您可以看到到达时间,因此它存储在某个地方。问题是在哪里。

标签: powershell msmq


【解决方案1】:

终于明白了。

[String]$cName = $Env:COMPUTERNAME
[Reflection.Assembly]::LoadWithPartialName("System.Messaging") | out-null
[System.Messaging.MessageQueue[]]$queues = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine($cName.ToLower())
Foreach ($queue in $queues) {
    $queue.MessageReadPropertyFilter.SetAll()
    try {
        [System.Messaging.Message]$message = $queue.Peek(10)
        Write-Host $queue.QueueName $message.ArrivedTime
    }
    catch {
        #Write-Host "Timeout"
    }
}

诀窍是设置 MessageReadPropertyFilter。可以单独设置每个属性,但现在就可以了。

【讨论】:

    【解决方案2】:
    $qname = "<YourQueueNameHere>"
    
    [Reflection.Assembly]::LoadWithPartialName("System.Messaging") | out-null
    
    $q = new-object System.Messaging.MessageQueue($qname)
    
    $msgs = $q.GetAllMessages()
    
    foreach ( $msg in $msgs )
    {
        Write-Host $msg.ArrivedTime
    }
    

    Messagehere 的文档。 Message.ArrivedTime here 的文档。

    【讨论】:

    • 在 Windows server 2012 中输出为空
    【解决方案3】:

    这对我有用。在我的例子中,脚本会计算过去一小时内收到了多少条消息。

    $QueueArray = "MyQueue1.ERRORS", "MyQueue2.ERRORS";
    
    foreach ($queue in $QueueArray)
    {
        # Create each MSMQ object
        [System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
        $MessageQueue = New-Object System.Messaging.MessageQueue (".\private$\" + $queue)
        
        # Get the amount of the messages in the queue for the past hour.
    
        $MessageQueue.MessageReadPropertyFilter.SetAll()
        $PastHour = (Get-Date).AddHours(-1)
        $countFailedMessages = 0
        foreach ($message in $MessageQueue )
        {
            if ($PastHour -lt $message.ArrivedTime)
            {
                #Write-Host $message.ArrivedTime
                $countFailedMessages++
                #Write-Host $countFailedMessages
            }
        }
        Write-Host $queue `t $countFailedMessages "failed messages for the past hour"
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多