1
votes

I am using following coe to replace the string

$folders=Get-ChildItem -Path "C:\temp\Database Scripts"
foreach($folder in $folders)
{
    Write-Host $folder
    $spath=[string]::Concat("C:\temp\Database Scripts\", $folder)
    $subfolders=Get-ChildItem $spath 
    foreach($subfolder in $subfolders )
    {
        if($subfolder -match "Running Scripts")
        {
            $subfolerpath=[string]::Concat($spath,"\",$subfolder,"\*")  
            $files =get-childitem -Path $subfolerpath -include "AVEVAScripts*"
            if($files -ne $null)
            {
                foreach( $file in $files)
                {
                Write-Host $file;
                (Get-Content $file) | ForEach-Object {$_ -replace "DATABASE_USER","fhghjgj" `
                -replace "DATABASE_PASSWORD", "DFGHFHJGJH"  } |Set-Content $file 
                }
            }
        }       
    }
}

But ending up with following error.

Set-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

Please help :)

1
Remove $x at the end of Set-Content . You haven't declared it anywhere. - Frode F.
@Graimer thanks Graimer,That was left by mistake,It worked :) - user1595214
Added it as answer, with an alternative way to solving it using pipelines(untested). - Frode F.
Do not use string concatenation to create filesystem paths. Use Join-Path instead. It's not a solution to your issue, but it makes your code cleaner & safer. - alroc
@alroc Thank u so much,i will use it for sure. - user1595214

1 Answers

3
votes

Remove the $x in the end of Set-Content. $x is never declared.

Also, you could simplify it a lot. Ex:

Get-ChildItem -Filter "Running Scripts" -Path "C:\temp\Database Scripts" -Recurse | ForEach-Object {
    Get-ChildItem -Path $_.FullName -Filter "AVEVAScripts*" -Recurse | ForEach-Object {
        (Get-Content $_.FullName) | ForEach-Object {
            $_ -replace "DATABASE_USER","fhghjgj" -replace "DATABASE_PASSWORD", "DFGHFHJGJH"
        } | Set-Content $_.FullName
    }
}

Or find all files that includes "AVEVAScripts" in it's name, then check if their full path includes "Running Scripts"

Get-ChildItem -Filter "AVEVAScripts*" -Path "C:\temp\Database Scripts" -Recurse | 
Where-Object { $_.FullName -like "*Running Scripts*" } | 
ForEach-Object {
    (Get-Content $_.FullName) | ForEach-Object {
        $_ -replace "DATABASE_USER","fhghjgj" -replace "DATABASE_PASSWORD", "DFGHFHJGJH"
    } | Set-Content $_.FullName
}