3

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?

1
  • You have to put the brace on the same line of foreach-object Commented Dec 1, 2021 at 5:09

1 Answer 1

5

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 
{
  $_
}
Sign up to request clarification or add additional context in comments.

2 Comments

That simple! Odd that it doesn't pick up any of the XML files and doesnt even go into the loop. Anything obvious there?
@Philip It's lame. -include only works with -recurse or a wildcard in -path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.