简体   繁体   English

ForEach 真的是 ForEach-Object (Powershell) 的别名吗

[英]Is ForEach really an alias of ForEach-Object (Powershell)

ForEach is documented as an alias of ForEach-Object . ForEach被记录为ForEach-Object的别名。 When I use Get-Alias ForEach it tells that it is in alias of ForEach-Object .当我使用Get-Alias ForEach它告诉我它是ForEach-Object别名。

But,但,

  • ForEach-Object Accepts parameters such as Begin Process and End, where ForEach doesn't accept them. ForEach-Object接受Begin Process 和End 等参数,其中ForEach不接受它们。

    • When we call ForEach-Object without any thing it prompts for parameter Process, and on calling ForEach it leaves a nested prompt with >> .当我们在没有任何东西的情况下调用ForEach-Object ,它会提示参数 Process,而在调用ForEach它会留下一个带有>>的嵌套提示。

    • % and ForEach behave same, but ForEach-Object don't. %ForEach行为相同,但ForEach-Object不一样。

Here my questions are:我的问题是:

  • Is ForEach really an alias of ForEach-Object ? ForEach真的是ForEach-Object的别名吗?

  • Which is better, ForEach or ForEach-Object ? ForEachForEach-Object哪个更好?

Please share your thoughts.请分享您的想法。

Thank you!谢谢!

There are two distinct underlying constructs :两种不同的底层结构

  • The ForEach-Object cmdlet ForEach-Object cmdlet

    • This cmdlet has a built-in alias name: foreach , which just so happens to match the name of the distinct foreach statement (see next point).此 cmdlet 有一个内置别名foreach ,恰好与不同的foreach语句的名称相匹配(请参阅下一点)。
  • The foreach loop statement (akin to the lesser used for statement). foreach循环语句(类似于较少使用的for语句)。

What foreach refers to depends on the parsing context - see about_Parsing : foreach指的是什么取决于解析上下文- 请参阅about_Parsing

  • In argument mode (in the context of a command ), foreach is ForEach-Object 's alias.参数模式下(在命令的上下文中), foreachForEach-Object的别名。

  • In expression mode (more strictly: statement mode in this case), foreach is the foreach loop statement .表达式模式下(更严格地说:本例中的语句模式), foreachforeach循环语句


As a cmdlet , ForEach-Object operates on pipeline input .作为cmdletForEach-Object管道输入进行操作

  • Use it process output from other commands in a streaming manner, object by object , as these objects are being received, via the automatic $_ variable .使用它以方式处理来自其他命令的输出,逐个对象,因为这些对象通过自动$_变量被接收。

As a language statement , the foreach loop operates on variables and expressions (which may include output collected from commands).作为语言语句foreach循环对变量和表达式(可能包括从命令收集的输出)进行操作。

  • Use it to process already-collected-in-memory results efficiently, via a self-chosen iterator variable (eg, $num in foreach ($num in 1..3) { ... } );用它来有效地处理已经收集的功能于存储器的结果,通过自选择的迭代变量(例如, $numforeach ($num in 1..3) { ... } ); doing so is noticeably faster than processing via ForEach-Object .这样做明显比通过ForEach-Object处理

  • Note that you cannot send outputs from a foreach statement directly to the pipeline, because PowerShell's grammar doesn't permit it;请注意,您不能将foreach语句的输出直接发送到管道,因为 PowerShell 的语法不允许这样做; for streaming output to the pipeline, wrap a foreach statement in & { ... } .要将输出流式传输到管道,请将foreach语句包装在& { ... } (By contrast, simple expressions (eg, 1..3 ) can be sent directly to the pipeline). (相比之下,简单的表达式(例如, 1..3可以直接发送到管道)。

For more information, a performance comparison and a discussion of the tradeoffs (memory use vs. performance), including the .ForEach() array method , see this answer .有关更多信息、性能比较权衡讨论(内存使用与性能),包括.ForEach()数组方法,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM