管完整系列的对象,而不是数组项一次? [英] Pipe complete array-objects instead of array items one at a time?

查看:193
本文介绍了管完整系列的对象,而不是数组项一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 - 通用描述

如可以在帮助下看到了 about_pipelines 帮助管道)的PowerShell对象发送一个在时间下来pipeline¹。因此,获取进程-Name记事本|停止进程的时间缩短管道发送一个过程。

The problem - Generic description
As can be seen in help for about_pipelines (help pipeline) powershell sends objects one at the time down the pipeline¹. So Get-Process -Name notepad | Stop-Process sends one process at the time down the pipe.

假设我们有一个第三方的cmdlet的(DO-SomeStuff)不能以任何方式修改或变更。如果它传递一个字符串数组DO-SomeStuff执行不同的或者如果它传递一个字符串对象。

Lets say we have a 3rd party CmdLet (Do-SomeStuff) that can't be modified or changed in any way. Do-SomeStuff perform different if it is passed an array of strings or if it is passed a single string object.

DO-SomeStuff仅仅是一个例子,它可以代替的foreach对象选择对象写主机(或任何其他cmdlet的接受管道输入)

Do-SomeStuff is just an example, it could be substituted for ForEach-Object, Select-Object, Write-Host (or any other CmdLet accepting pipeline input)

DO-SomeStuff将在本实施例过程中,在时间阵列一个在各个项目。

Do-SomeStuff will in this example process the individual items in the array one at the time.

$theArray = @("A", "B", "C")
$theArray | Do-SomeStuff

如果我们想整个阵列发送作为一个对象来执行,SomeStuff人们可能会尝试这样的事情

If we want to send the complete array as one object to Do-SomeStuff one might try something like this

@($theArray) | Do-SomeStuff

但是,因为PowerShell中忽略的新单项阵列也不会产生预期的结果。

But it does not produce the expected result since PowerShell "ignores" the new single-item-array.

那么,你如何逼 $ theArray 来传递下来的管道作为一个单一的数组对象,而不是内容项目之一的时候?

So, how do you "force" $theArray to be passed down the pipe as a single array-object instead of the content items one at the time?



问题 - 实际的例子

如图所示的输出低于如果传递一个数组写主机是不同的,或者如果它传递的数组一个在单个项目的时间。


The problem - practical example
As shown below the output of Write-Host is different if passed an array or if it passed the individual items in the array one at the time.

PS C:\> $theArray = @("A", "B", "C")
PS C:\> Write-Host $theArray
A B C
PS C:\> $theArray | foreach{Write-Host $_}
A
B
C
PS C:\> @($theArray) | foreach{Write-Host $_}
A
B
C

你是怎么做到让 $ theArray | {的foreach写主机$ _} 来产生输出为同写主机$ theArray






脚注


  1. 在PowerShell管道处理

字符串数组正常

PS C:\> @("A", "B", "C").GetType().FullName
System.Object[]



字符串数组正常管道来的foreach对象


A normal array of strings piped to Foreach-Object

PS C:\> @("A", "B", "C") | foreach{$_.GetType().FullName}
System.String
System.String
System.String

数组中的每个字符串处理一个由的ForEach对象cmdlet的时间。

Each string in the array is processed one at the time by the ForEach-Object CmdLet.



阵列,这里的内部数组是字符串数组的数组。


An array of arrays, where the "inner" arrays are arrays of strings.

PS C:\> @(@("A", "B", "C"), @("D", "E", "F"), @("G", "H", "I")) | foreach{$_.GetType().FullName}
System.Object[]
System.Object[]
System.Object[]

阵列中的每个阵列处理一个通过给ForEach对象cmdlet时间,和每个子阵列的从输入的内容被作为即使它是一个数组一个对象来处理。

Each array in the array is processed one at the time by the ForEach-Object CmdLet, and the content of each sub-array from the input is handled as one object even though it is an array.

推荐答案

简短的回答::使用一元数组运算符

Short answer: use unary array operator ,:

,$theArray | foreach{Write-Host $_}

龙答:有一件事你应该了解关于 @()运营商:它总是跨preT其作为陈述的内容,即使内容仅仅是一个前pression。考虑这个code:

Long answer: there is one thing you should understand about @() operator: it always interpret its content as statement, even if content is just an expression. Consider this code:

$a='A','B','C'
$b=@($a;)
$c=@($b;)

我想补充声明标记的显式结束; 在这里,虽然PowerShell的允许忽略它。 $ A 是三个元素的数组。什么的结果美元; 语句? $ A 是一个集合,所以收集应列举和各个项目应该由管道传递。因此导致 $一个的; 语句写入管道三大要素。 @($ A;)看到三个要素,而不是原始数组,并从他们创造的数组,所以 $ B 是三个元素的数组。同样的方法 $ C 相同三个元素的数组。所以,当你写 @($集合)创建数组,即复制 $集合的,而不是数组元素单个元素。

I add explicit end of statement mark ; here, although PowerShell allows to omit it. $a is array of three elements. What result of $a; statement? $a is a collection, so collection should be enumerated and each individual item should be passed by pipeline. So result of $a; statement is three elements written to pipeline. @($a;) see that three elements, but not the original array, and create array from them, so $b is array of three elements. Same way $c is array of same three elements. So when you write @($collection) you create array, that copy elements of $collection, instead of array of single element.

这篇关于管完整系列的对象,而不是数组项一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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