foreach - Issue with nested powershell -


i have code snippet try tro replace strings in files of directory. thought nest foreach in foreach-object, not seem work.

the error is:

invalidargument: (:) [foreach-object], parameterbindingexception


$files = get-childitem $testdir\reference *.* -recurse $replacementmap = @{"fruit::apple" = "newapple";"fruit::banana" = "newbanana"}  foreach ($file in $files)     {     if (get-content $($file.fullname) | select-string -pattern "fruit::")         {         $content = get-content $($file.fullname) | foreach-object                {                 $line = $_                foreach ($entry in $replacementmap.getenumerator())                    {                    $line -replace $($entry.name),$($entry.value)                    }                 }         $content = $content -join "`r`n"         $content | set-content $($file.fullname)      } 

this code worked without

foreach ($entry in $replacementmap.getenumerator())     {     $line -replace $($entry.name),$($entry.value)     } 

part. has clue i'm doing wrong? in advance

you missed curly brace closure , formatting issue on foreach-object. need take care of foreach , foreach-object in different way:

replace existing foreach part this:

foreach ($file in $files) {     if(get-content $($file.fullname) | select-string -pattern "fruit::")     {         $content = get-content $($file.fullname) | %{                     $line = $_                    foreach ($entry in $replacementmap.getenumerator())                    {                     $line -replace $($entry.name),$($entry.value)                    }         }         $content = $content -join "`r`n"         $content | set-content $($file.fullname)     } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -