2016-03-22 195 views
1

当谈到PowerShell时,我是一个完全新手,但一直在研究测试我设置的消息队列(MSMQ)的最佳方法。 到目前为止,我已经能够在PowerShell中运行以下获得所有我的队列,看看有什么存在:MSMQ发布消息

gwmi -class Win32_PerfRawData_MSMQ_MSMQQueue -computerName . | 
ft -prop Name, MessagesInQueue 

这将返回MSMQ队列的名单上有可用。 我现在想要发送一条消息给只有一个返回的队列被命名来测试连接。我的队列被命名为:

<server>\private$\<queuename> 

我曾尝试使用下面的尝试,但没有运气:

$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 

我不能为我的生活弄清楚如何做到这一点。有没有人有任何好的资源或代码来做到这一点?

回答

3

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

您可以使用.NET System.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()