【发布时间】:2021-06-01 10:48:56
【问题描述】:
我正在寻找替换多个文本文件中的一串文本。所有文件中的字符串都相同,但文件名不同。所有测试文件都在同一个文件夹中。本质上,我正在更新多个用户的登录脚本中的共享路径。我不想手动执行此操作,因为有数百个文件。我想使用get-content -replace 来完成此操作。
这是我目前尝试过的代码。
set pwd $filepath
for each($file in $filepath)
{ (get-content $file) -replace $find, $replace
}
$filepath 是远程服务器上共享驱动器的路径。$find 是要替换的字符串。
执行get-content cmdlt 时出现访问被拒绝错误。$replace 是替换字符串。
【问题讨论】:
-
好吧,你能告诉我们你的尝试吗?(:
-
请向我们展示您已经尝试过的内容,以便我们为代码提供帮助。还有一些例子会有所帮助
-
$old = 'new who' $new = 'new(:' $Path = "\\VMServer12\tablestra\" Get-ChildItem -Path $Path -Depth 3 -Filter *.txt | Select-Object -ExpandProperty fullname | foreach { (Get-Content $_).replace($old,$new) | Set-Content $_ } 2>&1 | Out-Null -ErrorAction SilentlyContinue -
感谢所有 cmets。我对代码做了一些更改。现在我的问题是拒绝访问错误。
-
这是我的代码: # 变量声明 $find $replace $filepath #变量赋值 $find = "houfilesvr01" $replace = "aps.local" $filepath = C:\Users\jshabazz\Documents \Area51\logonscripttesting\Testfiles #execution foreach($file in (gci $filepath)) { (get-content $file) -replace $find, $replace |add-content $filepath -force }
标签: powershell replace file-get-contents