1

My Current Code Is

Param(
  [string]$filePath = "C:\",
  [string]$logFileFind = "error.log",
  [string]$logFileReplace ="ThisHasBeenReplaced.log" 
)


($configFile = Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*.config") } 

It Works fine and gives me list of files i was wondering how i could go through these files and find and replace certain words for when I'm moving though environments and the path wont be the same. I'm very limited in my powershell knowledge and i tried adding this to the end of the script.

ForEach-Object{(Get-Content $configFile) -replace $logFileFind , $logFileReplace | Set-Content $configFile})

This didn't work and i was wondering if there was anyone out there who knew what i could do to make it work.

Thanks in Advance!

3
  • Try to set the -raw parameter on the Get-Content invoke Commented Aug 3, 2015 at 14:12
  • its Saying its invalid data in the Get-Content Commented Aug 3, 2015 at 14:17
  • What version of PowerShell are you running in your environments? Commented Aug 3, 2015 at 14:50

1 Answer 1

1

You always access $configFile in your foreach loop (which is probably an System.Array), not the actual element. Try this:

$configFile | foreach { (get-content $_.FullName -Raw) -replace $logFileFind , $logFileReplace | Set-Content $_.FullName }

Here is a full example:

Get-ChildItem -Recurse -force $filePath -ea 0 |
 where { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*.config") } | 
 foreach { 
   (gc $_.FullName -raw) -replace $logFileFind , $logFileReplace | sc $_.FullName 
 }
Sign up to request clarification or add additional context in comments.

7 Comments

It's Throwing up the error Get-Content : A parameter cannot be found that matches parameter name 'Raw'.
I updated my answer. Please try again. You could also try to omit the -Raw parameter since the error was the access of your file path.
The Error message is Get-Content : Cannot bind argument to parameter 'Path' because it is null. does this mean there is something wrong with my $configFile?
Hm, try to print all paths using "$configFile | select -expand FullName"
I upgraded my answer with a full example which works for me.
|

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.