为什么PowerShell函数无法返回MessageQueue对象? [英] Why can't a PowerShell function return a MessageQueue object?

查看:100
本文介绍了为什么PowerShell函数无法返回MessageQueue对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了函数的以下变体以返回System.Messaging.MessageQueue对象:

I've written the following variations of a function to return a System.Messaging.MessageQueue object:

set-strictmode -version latest
add-type -AssemblyName System.Messaging
$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'

function Get-MsmqQueue1 {
    New-Object "Messaging.MessageQueue" -Args '.\private$\barneytest'
}

function Get-MsmqQueue2 {
    $q = New-Object "Messaging.MessageQueue" -Args '.\private$\barneytest'
    $q
}

function Get-MsmqQueue3 {
    $q = New-Object "Messaging.MessageQueue" -Args '.\private$\barneytest'
    Write-Output $q
}

function Get-MsmqQueue3a {
    $q = New-Object "Messaging.MessageQueue" -Args '.\private$\barneytest'
    if ($q) {
        Write-Debug "Successfully created $($q.QueueName)"
    } else {
        Write-Error "No queue object created"
    }
    Write-Output $q
}

$q = Get-MsmqQueue3a
$q
if ($q) {
    Write-Debug $q.QueueName
} else {
    Write-Error "No queue object returned"
}

它们都不返回对象. PowerShell莫名其妙地吞噬了它.请注意,"3a"版本已进行日志记录,以证明其写入管道的值是不为空,而该函数没有返回任何值.

None of them return an object. It's somehow being swallowed up by PowerShell. Note that the "3a" version has logging to prove that the value it's writing to the pipeline is not null, yet no value is returned from the function.

怎么可能?这是PowerShell错误吗?

How can this be? Is this a PowerShell bug?

非常感谢.

推荐答案

我是个白痴.这不是错误,但很有趣.因为System.Messaging.MessageQueue通过枚举消息来实现System.Collections.IEnumerable,所以我看到的行为是PowerShell实际上是从新创建的队列中读取消息并将它们放入管道中,而不是队列对象本身.当然,因为队列是新的,所以队列是空的,因此没有任何东西在管道中传递.

I'm an idiot. It's not a bug, but it's interesting. Because System.Messaging.MessageQueue implements System.Collections.IEnumerable by enumerating it's messages, the behaviour I was seeing was that PowerShell was actually reading the messages off the newly-created queues and putting them into the pipeline rather than the queue objects themselves. Of course because the queues were new, they were empty, so there was nothing passed on down the pipeline.

我刚刚度过了整个下午的大部分时间,而今晚则花了几个小时来解决这个问题.我不为自己感到骄傲.

I just spent most of this afternoon and some hours this evening working this out. I am not proud of myself.

这篇关于为什么PowerShell函数无法返回MessageQueue对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆