Powershell v2促进了foreach的价值

时间:2013-04-12 15:02:01

标签: windows powershell

此代码在PowerShell v1中运行,但在PowerShell v2中不运行。它现在提示输入值:

enter image description here

Get-ChildItem -Path $source -Filter "*.journal" | 
 Where-Object { $_.Name -match 'SoundPC\[\d{1,12}-\d{1,12}\].journal' } | Sort-Object -Property CreationTime | ForEach-Object 
{

$sourcefile = $_.Name

}

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

左括号必须与foreach命令位于同一行,目前它在新行上:

Foreach-Object {...

答案 1 :(得分:0)

正如Shay Levy指出的那样,括号必须与ForEach-Object命令位于同一行。

... | ForEach-Object {
    $sourcefile = $_.Name
}

或者,您可以使用行尾的反引号字符`来继续,然后允许您将左括号放在下一行。作为一名C#开发人员,我更喜欢这样,以便我的大括号可以排队。

... | ForEach-Object `
{
    $sourcefile = $_.Name
}