合并文件的内容,而不是一个一个地编辑

Consolidates the contents of the files instead of editing one by one

$file = 'G\*\configs.txt'
$find = '192.168.152.161:8011'
$replace = '{appconfigs.writes}'

(Get-Content $file).replace($find, $replace) | Set-Content $file

当我运行对所有文件一起编辑脚本 剧本将他们联合起来,不一一编辑

我们能做什么?

您正在尝试处理多个文件。 因此,您不能立即使用 Get-Content。您需要将其包装在一个循环中。

这是一个关于如何执行此操作的示例。

$find = '192.168.152.161:8011'
$replace = '{appconfigs.writes}'

foreach ($File in Get-ChildItem -Path 'G\*\configs.txt') {
    $Content = Get-Content -Path $File.FullName -Raw
    #IndexOf to avoid using Set-Content on files that did not change
    if ($Content.IndexOf($find) -gt -1) {
        $Content.replace($find, $replace) | Set-Content $file.FullName
    }
}