【问题标题】:Write-Output -InputObject (,'Test') -NoEnumerate写输出 -InputObject (,'Test') -NoEnumerate
【发布时间】:2019-07-11 18:51:07
【问题描述】:

我认为这是一个仅适用于 PowerShell Windows 5.1 的错误:

当通过命名的-InputObject 参数提供输入时,-NoEnumerate 开关不起作用:

以下函数返回False

Function Test {Write-Output -InputObject (,'Foo') -NoEnumerate} 
(Test) -is [Array]

虽然这个函数返回True:

Function Test {Write-Output (,'Foo') -NoEnumerate} 
(Test) -is [Array]

PowerShell Windows

Name                           Value
----                           -----
PSVersion                      5.1.17134.858
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.858
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

问题没有显示在 PowerShell Core 中。
(两个函数都返回True

Name                           Value
----                           -----
PSVersion                      6.2.0-preview.1
PSEdition                      Core
GitCommitId                    6.2.0-preview.1
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

虽然在Write-Output cmdlet 的描述中有一条关于-NoEnumeration 开关的说明:

注意

此开关仅适用于 PowerShell Core 6.2 及更高版本。 在旧版本的 PowerShell Core 上,集合仍然是 即使使用此开关也会枚举。 PowerShell 中的行为 Core 6.2 与 Windows PowerShell 一致。

(如果我没看错,我知道-NoEnumeration 开关应该只适用于 PowerShell Windows)

我已按照PowerShell GitHub Community 的指导hereWindows PowerShell [UserVoice] 上报告了问题here,但感觉就像一个黑洞......(我之前报告过问题但几乎没有看到任何回应)。

问题:
这确实是一个错误还是我错过了什么?
如果是错误,Windows PowerShell [UserVoice] 仍然是报告此类问题的正确地址吗?

【问题讨论】:

  • 是的,这是一个已知的错误。不,你不能把它修好。它最近在 v6.2 中得到修复
  • @TheIncorrigible1,感谢您的回答。我遇到了这个问题,但无法将它与它对我的看法联系起来:使用(而不是使用)命名的 -InputObject 参数)。
  • 您的问题中引用的文档在撰写本文时确实不正确。已在this GitHub issue 中请求修复它们。

标签: windows powershell switch-statement enumerate


【解决方案1】:

确实,Windows PowerShell 中的一个错误(直到 v5.1,最后一个发布的版本 - 由于该错误不是安全关键,它是 不太可能修复)在传递-NoEnumerate 时仍会导致枚举,尽管只是一个级别。[1]

自(至少)v6.2.3 起,已在 PowerShell [Core] 中修复此错误[2]

为了演示这个问题(下面的所有命令现在都可以在 PowerShell [Core] 中按预期工作):

WinPS> (Write-Output -NoEnumerate -InputObject 1, 2 | Measure-Object).Count
2 # !! Should be *1*, because a *single array* (with 2 elements) was passed.

注意:在上面的命令中省略 Measure-Object 很诱人,但这会扭曲结果:在表达式导致该命令的输出被枚举

有一个解决方法省略-InputObject,即将集合作为位置传递 em> 参数,令人惊讶的是,这会导致参数按原样传递(而在参数绑定期间显式使用-InputObject 枚举参数):

# POSITIONAL argument binding, without -InputObject
WinPS> (Write-Output -NoEnumerate 1, 2 | Measure-Object).Count
1 # OK

请注意,此解决方法不适用于管道输入,尽管这是不寻常的情况,因为大多数命令都可以将集合作为一个整体输出到管道(它们发送它们的元素一个接一个):

WinPS> ( , (1, 2) | Write-Output -NoEnumerate | Measure-Object ).Count
2 # !! Should be *1*: the unwrapped outer array contains a *single [array]* element

注意:外包装数组, (...),使用,的一元形式,array constructor operator总是 如果您想使用表达式通过管道发送集合作为一个整体,这是必需的。

因此,这种单元素包装器数组技术Write-Output -NoEnumerate 本身的简洁和快速的替代方案,因此,实际上,, (1, 2) 本身 em> 是基于管道的解决方法:它将数组操作数 (1, 2)作为一个整体写入(成功)输出流。


[1] Not 使用-NoEnumerateWindows PowerShell 中还表现出一个相关的错误:太多 枚举发生在@ 987654338@ 被显式使用

错误地应用了一级递归:输入集合的元素也被意外枚举了(同样,PowerShell [Core] 现在可以正常工作):

WinPS> (Write-Output -InputObject 1, (2, 3) | Measure-Object).Count
3 # !! This should be *2*, because the single input array has only 2 elements.
  # !! That the 2nd element is itself an array should not matter.

同样,解决方法省略 -InputObject

# POSITIONAL argument binding, without -InputObject
WinPS> (Write-Output 1, (2, 3) | Measure-Object).Count
2 # OK

[2] 但是,从 PowerShell [Core] 7.0.0-rc.2 开始,当您将
-NoEnumerate标量 参数结合使用时,仍然存在错误是位置绑定

PSCore> (Write-Output -NoEnumerate 42).GetType().Name
List`1 #`# !! scalar 42 was unexpectedly wrapped in a *list*

解决方法使用-InputObject明确

# OK thanks to -InputObject
PSCore> (Write-Output -NoEnumerate -InputObject 42).GetType().Name
Int32 # OK

该错误正在this GitHub issue 中进行跟踪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2011-03-22
    相关资源
    最近更新 更多