希望有人能指出我正確的方向。
背景:
環境是 Windows 10 - 我需要為我們的用戶更新組態檔中的一行,從數字 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 "
結果是,如果有 John Smith 和 John Walker 的組態檔 - 在腳本運行后,檔案會按預期更新,但不是更新 2 個單獨的組態檔,而是更新它們,然后在每個實體中將更新的檔案添加到一起檔案相互之間存在。
任何指標表示贊賞!
uj5u.com熱心網友回復:
您需要的是使用回圈,以便可以單獨處理每個檔案:
# 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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363105.html
