[英]Powershell ForEach & ForEach-Object with Select
i was trying to loop through a list of file names in $s4a using ForEach and ForEach-Object and display output.我试图使用 ForEach 和 ForEach-Object 遍历 $s4a 中的文件名列表并显示 output。 The only two ways i found that generate output are:我发现生成 output 的唯一两种方法是:
Can someone please let me know why the ones below generate nothing, while the above two work?有人可以让我知道为什么下面的没有产生任何东西,而上面的两个工作吗?
Thanks heaps非常感谢
Essentially, it's because your syntax is incorrect for each of the statements that aren't working.本质上,这是因为您的语法对于每个不起作用的语句都不正确。 In each of the non-working examples, you are not properly referencing the object that you wish the view the 'name' property on.在每个非工作示例中,您都没有正确引用您希望查看“名称”属性的 object。
Select-Object is generally used when accepting an input object from the pipeline but can also identify the object using the -InputObject parameter. Select-Object 通常在从管道接受输入 object 时使用,但也可以使用 -InputObject 参数识别 object。
When calling an object property by $object.propertyName, you don't need a select statement.通过 $object.propertyName 调用 object 属性时,您不需要 select 语句。
Instead of ForEach ($name in $s4a) {Select Name}
, try this: ForEach ($name in $s4a) {$name | Select Name}
而不是ForEach ($name in $s4a) {Select Name}
,试试这个: ForEach ($name in $s4a) {$name | Select Name}
ForEach ($name in $s4a) {$name | Select Name}
Instaed of ForEach ($name in $s4a) {Select $name.Name}
, try this: ForEach ($name in $s4a) {$name.Name}
代替ForEach ($name in $s4a) {Select $name.Name}
,试试这个: ForEach ($name in $s4a) {$name.Name}
Instead of $s4a| ForEach-Object {Select$_.Name}
而不是$s4a| ForEach-Object {Select$_.Name}
$s4a| ForEach-Object {Select$_.Name}
, try this: $s4a| ForEach-Object {$_.Name}
$s4a| ForEach-Object {Select$_.Name}
,试试这个: $s4a| ForEach-Object {$_.Name}
$s4a| ForEach-Object {$_.Name}
Instead of $s4a| ForEach-Object {Select Name}
而不是$s4a| ForEach-Object {Select Name}
$s4a| ForEach-Object {Select Name}
, try this: $s4a| ForEach-Object {$_ | Select Name}
$s4a| ForEach-Object {Select Name}
,试试这个: $s4a| ForEach-Object {$_ | Select Name}
$s4a| ForEach-Object {$_ | Select Name}
Also, please note that there is a difference in how each method returns information.另外,请注意,每种方法返回信息的方式有所不同。 Select statements return modified versions of the original objects that only contain the properties that you are selecting. Select 语句返回仅包含您选择的属性的原始对象的修改版本。 Whereas, calling a property directly returns only the value of that property.而直接调用属性仅返回该属性的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.