配置更新PowerShell

0

的问题

希望有人可以指给我正确的方向。

背景:

环境是Windows10 我需要更新一行内的配置文件为我们的用户,从一个数字IP到一串。 每个装置这是范围限于有可能的多个结构文件内所定义的道路的脚本用不同以前的用户配置文件。 为此,我已经做了脚本作为一个通配更新所有的位于配置有同一行更新,因此是均匀的,并避免了冲突后的部署。 目前的脚本作品的替换价值,但当另外配置捡起通过剧本,他们正在被合并和重新进入每个配置找到。

脚本在其目前的形式

((Get-Content -Path "C:\Program Files\ProgramX\config\brand-protocol-port-*-config.ext" -Raw) -replace 'X.X.X.X','VALUE') |
    Set-Content -Path " C:\Program Files\ProgramX\config\brand-protocol-port-*-config.ext "
Get-Content -Path " C:\Program Files\ProgramX\config\brand-protocol-port-*-config.ext "

结果是,如果有的配置文件为约翰*史密斯和约翰*沃克-脚本后运行的文件被更新,作为旨在但不是2个独立的配置文件被更新,更新后添加在一起在每个实例的更新的文件正本内。

任何指针是值得赞赏!

configuration powershell updates windows
2021-11-22 15:53:32
1

最好的答案

0

什么你需要的是使用一个循环,使每一文件可分开处理:

# since we're using the regex `-replace` operator, the dots need to be escaped
$ipToFind    = [regex]::Escape('X.X.X.X')
$replaceWith = 'VALUE'
Get-ChildItem -Path 'C:\Program Files\ProgramX\config' -Filter 'brand-protocol-port-*-config.ext' -File | ForEach-Object {
    (Get-Content -Path $_.FullName -Raw) -replace $ipToFind, $replaceWith | Set-Content -Path $_.FullName
}
2021-11-22 16:12:34

工作完美谢谢你!
TechnicalFool96

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................