【发布时间】:2013-05-20 09:20:19
【问题描述】:
我正在使用以下coe来替换字符串
$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
}
}
}
}
}
但最终出现以下错误。
Set-Content : 输入对象不能绑定到命令的任何参数,因为命令不接受管道输入,或者输入及其属性不匹配任何接受管道输入的参数。
请帮忙:)
【问题讨论】:
-
删除
$x末尾的Set-Content。你还没有在任何地方声明它。 -
@Graimer 感谢 Graimer,这是错误留下的,它起作用了 :)
-
将其添加为答案,并使用另一种使用管道解决它的方法(未经测试)。
-
不要使用字符串连接来创建文件系统路径。请改用
Join-Path。它不能解决您的问题,但可以让您的代码更简洁、更安全。 -
@alroc 非常感谢,我一定会使用它。
标签: powershell