我正在修改配置文件的PowerShell脚本。我有这样的文件:PowerShell函数替换或添加文本文件中的行
#####################################################
# comment about logentrytimeout
#####################################################
Logentrytimeout= 1800
谁应该是这样的:
#####################################################
# comment about logentrytimeout
#####################################################
Logentrytimeout= 180
disablepostprocessing = 1
segmentstarttimeout = 180
如果有一个按键(Logentrytimeout),只需将其更新到给定值。忽略注释,提到关键字(以#开头的行)。密钥不区分大小写。
如果未设置密钥(disablepostprocessing和segmentstarttimeout),请将密钥和值附加到文件。我的功能到目前为止是这样的:
function setConfig($file, $key, $value)
{
(Get-Content $file) |
Foreach-Object {$_ -replace "^"+$key+".=.+$", $key + " = " + $value } |
Set-Content $file
}
setConfig divider.conf "Logentrytimeout" "180"
setConfig divider.conf "disablepostprocessing" "1"
setConfig divider.conf "segmentstarttimeout" "180"
- 什么是正确的正则表达式?
- 如何检查是否有替换?
- 如果没有替换:如何将$ key +“=”+ $ value附加到文件中?