Home > front end >  ForEach-Object at command pipeline position Problem
ForEach-Object at command pipeline position Problem

Time:12-01

I've got the below Powershell script, and I'm struggling to finish it off.

Get-ChildItem -Path 'C:\temp\xml' -Include '*.xml' |
    ForEach-Object 
    {
        $FileName = $_.Fullname
    $Pattern = "</Date>"  
    $FileOriginal = Get-Content $FileName
    $date = Get-Date
    $DateStr = $date.ToString("yyyyMMdd")

    [String[]] $FileModified = @() 
    Foreach ($Line in $FileOriginal)
    {   
        $FileModified  = $Line
        if ($Line -match $pattern) 
        {
        $FileModified  = "<CDate>$DateStr</CDate>"

        } 
    }
    $FileModified = $FileModified -replace "CUR","PIT"
    $FileModified = $FileModified -replace "Current","Time"
    Set-Content $fileName $FileModified   
    }

When I attempt to run it, I get the following messages:

cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]:

Can anyone see what I'm doing wrong?

CodePudding user response:

Put the curly brace on the same line as the foreach-object:

echo hi | foreach-object {
  $_
}

This would fail, even in a script. It's a cmdlet, not a statement like "if", so it needs its -Process parameter on the same line.

echo hi | foreach-object 
{
  $_
}
  • Related