MSMQ 发布消息

Bat*_*ice 2 powershell msmq

我是 PowerShell 的完全新手,但一直在研究测试我设置的消息队列 (MSMQ) 的最佳方法。到目前为止,我已经能够在 Powershell 中运行以下命令来获取我的所有队列以查看存在的内容:

gwmi -class Win32_PerfRawData_MSMQ_MSMQQueue -computerName . |
ft -prop Name, MessagesInQueue
Run Code Online (Sandbox Code Playgroud)

这将返回我可用的 MSMQ 队列列表。我现在只想向返回的队列之一发布消息以测试连接。我的队列被命名为:

<server>\private$\<queuename>
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用以下方法但没有运气:

$myqueue = '.\Private$\portalemailqueue'
$MyQueue.Send("<<Message>>", "test1")

Method invocation failed because [System.String] does not contain a method named 'Send'.
At line:3 char:1
+ $MyQueue.Send("<<Message>>", "test1")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Run Code Online (Sandbox Code Playgroud)

我一生都无法弄清楚如何做到这一点。有没有人有任何好的资源或代码来做到这一点?

Mat*_*sen 6

正如错误所说,您的$MyQueue对象只是一个字符串,没有什么特别之处。

您可以使用 .NETSystem.Messaging命名空间来处理消息队列实例:

Add-Type -AssemblyName System.Messaging
$MyQueuePath = '.\Private$\portalemailqueue'
$MyQueue = if([System.Messaging.MessageQueue]::Exists($MyQueuePath)) {
    # Open existing queue
    New-Object System.Messaging.MessageQueue $MyQueuePath
} else {
    # Or create it 
    [System.Messaging.MessageQueue]::Create($MyQueuePath)
}
# Now you can call $MyQueue.Send()
Run Code Online (Sandbox Code Playgroud)