Windows 为什么是';测量对象-输入对象$foo';不同于'$foo |测量对象';在地狱里?

Windows 为什么是';测量对象-输入对象$foo';不同于'$foo |测量对象';在地狱里?,windows,powershell,scripting,Windows,Powershell,Scripting,我在一个目录中有六个.txt文件。因此,我创建了一个变量,如下所示: $foo = gci -Name *.txt $foo现在是一个由六个字符串组成的数组。就我而言,我有 PS > $foo Extensions.txt find.txt found_nots.txt output.txt proteins.txt text_files.txt PS > $foo.gettype() IsPublic IsSerial Name

我在一个目录中有六个.txt文件。因此,我创建了一个变量,如下所示:

$foo = gci -Name *.txt
$foo
现在是一个由六个字符串组成的数组。就我而言,我有

PS > $foo
Extensions.txt
find.txt
found_nots.txt
output.txt
proteins.txt
text_files.txt

PS > $foo.gettype()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS > $foo.Count
6
我想测量那个物体,所以我把它传给:

这正是我所期待的。但是,我也可以这样传递
$foo

PS> Measure-Object -InputObject $foo

Count    : 1
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

这不是我所期望的。这里发生了什么事?

PowerShell可能会在这里帮助您。让我们看一下<代码>获取帮助测量对象-Ung/<代码>并查看该参数。< /P>
-InputObject <psobject>
    Specifies the objects to be measured. Enter a variable that contains 
    the objects, or type a command or expression that gets the objects.

    Required?                    false
    Position?                    named
    Default value
    Accept pipeline input?       true (ByValue)
    Accept wildcard characters?  false
-InputObject
指定要测量的对象。输入一个包含
对象,或键入获取对象的命令或表达式。
必修的?假的
位置?命名
默认值
接受管道输入?真(按值)
是否接受通配符?假的
管道输入由值接受,这意味着它对
$foo
的每一行进行计数并进行计数。值得一提的是,引用中的所有和都使用此参数的管道输入。

执行时:

-InputObject <psobject>
    Specifies the objects to be measured. Enter a variable that contains 
    the objects, or type a command or expression that gets the objects.

    Required?                    false
    Position?                    named
    Default value
    Accept pipeline input?       true (ByValue)
    Accept wildcard characters?  false
$foo | measure-object
measure-object -inputobject $foo
PowerShell会自动展开集合/数组,并将每个元素沿管道传递到下一个阶段

执行时:

$foo | measure-object
measure-object -inputobject $foo
cmdlet不会在内部展开集合。如果希望在不让PowerShell自动展开的情况下检查集合,这通常非常有用。顺便说一句,同样的事情也适用于Get成员。如果要查看“集合”上的成员而不是每个单独的元素,请执行以下操作:

get-member -inputobject $foo
在管道情况下模拟这种情况的一种方法是:

,$foo | Get-Member

这将使用一个元素在另一个集合中包装任何foo(在本例中为集合)。当PowerShell自动展开以沿管道发送元素时,唯一的元素是沿管道发送的
$foo

请注意:$foo不是字符串数组。它是一个文件系统数组。试试
$foo[0].getTYpe()
我肯定得到了字符串
$foo=gci-Name*.txt
后接
$foo[0]。gettype()。Name
给出
字符串
但是,如果我这样做
$foo=gci-Filter*.txt
则改为
$foo[0]。gettype()。Name
给出
文件信息
以上内容在PowerShell 2上,以防在其他版本中有所不同。我知道发生了什么
-Name
是一个开关,它告诉
gci
仅返回名称。
*.txt
实际上是
-Path*.txt
谢谢。我不知道这件事,因为我还是有点不对劲。现在,我已经通过
get help about_pipelines
阅读了文档,其中的相关部分是“当您将多个对象通过管道传输到一个命令时,Windows PowerShell将对象一次发送到一个命令。当您使用命令参数时,对象将作为单个数组对象发送。”