如何使用powershell更新文件中的标记
我有一个配置文件(test.properties) 文件包括:如何使用powershell更新文件中的标记,powershell,Powershell,我有一个配置文件(test.properties) 文件包括: <a>aa</a> <b>bb</b> <c>ccc</c> <d>dddd</d> <Test>Yes</Test> aa bb ccc dddd 对 我想使用powershell替换test.properties文件中的值“Nope” Existing : <Test>Nope<Test&
<a>aa</a>
<b>bb</b>
<c>ccc</c>
<d>dddd</d>
<Test>Yes</Test>
aa
bb
ccc
dddd
对
我想使用powershell替换test.properties文件中的值“Nope”
Existing : <Test>Nope<Test>
AFter Update it should be
<Test>Yes<Test>
现有:没有
更新后,它应该是
对
如何使用powershell替换此值?使用替换!下面的代码将读取文件中的内容,并用新值替换测试标记,然后将内容写回同一文件
(Get-Content yourfile.xml).replace("<Test>Nope<Test>", "<Test>Yes<Test>") | Set-Content yourfile.xml
(获取内容yourfile.xml)。替换(“否”、“是”)|设置内容yourfile.xml
另一种方法是使用基于正则表达式的-replace
操作符,而不是Markus Höglund使用的.replace()
方法
-replace "(?<=<(Test)>).*?(?=</\1)",$custValue
顺便说一句,结束标签前面不是一个
/
?谢谢你的回复。如果我想使用paramater设置“是”值,我如何设置它?例如,我有一个参数$value='Yes',我想设置为$valueNp!然后您可以使用字符串插值,像这样尝试“$($value)”(注释:(获取内容yourfile.xml)。替换('Nope',“$($value)”)|设置内容yourfile.xml,以便输出:$value@Ukchat$value受限制。请尝试使用$custValue。此外,请使用双qoutes“$($custValue)”
(get-Content .\test.properties) -replace "(?<=<(Test)>).*?(?=</\1)",$custValue|
set-Content .\test.properties