Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 :)

share|improve this question
2  
Remove $x at the end of Set-Content . You haven't declared it anywhere. –  Frode F. May 20 '13 at 9:47
    
@Graimer thanks Graimer,That was left by mistake,It worked :) –  user1595214 May 20 '13 at 9:55
    
Added it as answer, with an alternative way to solving it using pipelines(untested). –  Frode F. May 20 '13 at 10:02
    
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 May 20 '13 at 13:29
    
@alroc Thank u so much,i will use it for sure. –  user1595214 May 20 '13 at 17:46

1 Answer 1

up vote 3 down vote accepted

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
}
share|improve this answer
    
Thank you so much for making it so simple,thats exactly what i wanted. –  user1595214 May 20 '13 at 10:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.