Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and Replace

Status
Not open for further replies.

cumpleby

Technical User
Nov 25, 2011
20
GB
I have a little problem when running this powershell script form a central location. If i run from a central location i keep getting:-

Get-Content : Cannot bind argument to parameter 'Path' because it is null.

and

Set-Content : Cannot bind argument to parameter 'Path' because it is null.

If i run this within the $Dest lcation it works without errors, how can i get this script to run to change files?

$path = "K:\Business Systems\Microsoft Development\Deploy"
$Date = (get-date -uformat '%d%m%Y')
$DateTime = (get-date -uformat '%d/%m/%Y %H:%M:%S')
$Dest = "c:\testing\"
$LogCopy = "C:\testing\code_drop_FileCopy$Date.log"
$LogRename = "C:\testing\code_drop_RenameFiles$Date.log"
$LogRenameEtl = "C:\testing\code_drop_RenameEtl$Date.log"

write-output "$DateTime Logging started" | out-file -append $logrename
$configFiles=get-childitem $Dest *.config -recurse -verbose -force | out-file -width "500" -append $logrename
foreach ($file in $configFiles)
{
(Get-Content $file.PSpath) | out-file -append $logrename

Foreach-Object {$_ -replace "%%DATABASE_SERVER%%", "SERVER"} |
Foreach-Object {$_ -replace "%%DATABASE_CATALOG%%", "DB"} |
Foreach-Object {$_ -replace "%%DATABASE_USER%%", "USER"} |
Foreach-Object {$_ -replace "%%DATABASE_PASSWORD%%", "PASSWORD"} |

Set-Content $file.PSpath -verbose | out-file -append $logrename
Write-host "$DateTime Files Amended" | out-file -append $logrename
write-output "$DateTime Logging Finished" | out-file -append $logrename


Any help would be appreciated.
 
Just an update to this, i have ran this in a powershell window and now what i get is:-

Get-Content : Access to the path ... is Denied

I think this is what is causing the arror above as it cannot get the contents. How can i retreieve the contents of a folder that is in a different location from wher the script is running form?

Thanks
 
I think the problem is your $configFiles=get-childitem... line. The Out-File cmdlet is an "end-of-line" cmdlet, it produces the file but doesn't pass along any objects through the pipeline. I think if you examine $configFiles after that statement you'll find it either empty or actually null since nothing gets assigned to it. Try splitting the line like this and see what happens
Code:
$configFiles = get-childitem $Dest *.config -recurse -verbose -force
$configFiles | out-file -width "500" -append $logrename
foreach ($file in $configFiles)
...
 
Hi, Thanks for the response, i had just found and then your post came through, yeah it's sending the content to log then not passing any of this on to the remainder of the script, hence Null.

Thanks

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top