PowerShell 是否要求脚本块的左花括号在同一行上? [英] Does PowerShell require the opening curly brace of a script block to be on the same line?
问题描述
好的,这个脚本的预期输出"是相当明显的,它可以工作.它复制文件:
Okay, the expected "output" of this script is fairly obvious, and it works. It copies files:
Get-ChildItem -Recurse -Directory | ForEach-Object{
if ($_.Name.ToUpper().Contains("INCLUDE_PUBLIC")){
Get-ChildItem -Recurse -File -Include "*.h" -Path $_.FullName | ForEach-Object {Copy-Item ($_.FullName) ("e:\IncTest")}
}
}
但是,如果我将左大括号 {
移动到下一行,我会得到以下输出:
However, if I move the opening brace {
to the next line, I get the following output:
cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]:
我是否被其他一些语法问题欺骗了,或者 ForEach-Object 是否要求它的大括号在同一行上?
Am I getting tricked by some other syntax issue, or does ForEach-Object require its brace to be on the same line?
推荐答案
PS C:\> Get-Help -Name 'ForEach-Object'
SYNTAX
ForEach-Object [-MemberName] <String> [-ArgumentList <Object[]>] [-Confirm] [-InputObject <PSObject>] [-WhatIf] [<CommonParameters>]
ForEach-Object [-Process] <ScriptBlock[]> [-Begin <ScriptBlock>] [-Confirm] [-End <ScriptBlock>] [-InputObject <PSObject>] [-RemainingScripts <ScriptBlock[]>] [-WhatIf] [<CommonParameters>]
通过使用脚本块,您将使用 ForEach-Object
cmdlet 的位置绑定参数.在这种情况下,-Process { }
参数集.如果您转义行尾(本质上,转义换行符),您可以绕过该问题,但这通常是一种不好的做法.
By using a script block, you're using positionally-bound parameters of the ForEach-Object
cmdlet. In this case, the -Process { }
parameter set. If you escape the end of the line (in essence, escaping the newline), you can get around that, but it's generally a bad practice.
这篇关于PowerShell 是否要求脚本块的左花括号在同一行上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!