我有以下代码:
function f()
{
begin{$count=0}
process{$count+=10}
end{$count}
}
1..10|f # OK
1..10|%{
begin{$count=0}
process{$count+=10}
end{$count}
} # Error
第一个“f”调用成功,而 %{} 块显示错误:
100
% : The script block cannot be invoked because it contains more than one clause. The Invoke() method can only be used on script blocks that contain a single clause.
At D:\Untitled1.ps1:13 char:7
+ 1..10|%{
+ ~~
+ CategoryInfo : InvalidOperation: (:) [ForEach-Object], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ForEachObjectCommand
但为什么?ForEach-Object
不支持begin
//块process
?end