简体   繁体   English

Powershell使用-replace编辑节点中的一部分文本?

[英]Powershell using -replace to edit a portion of text in a node?

I am trying to write a powershell script using -replace or something equivalent to search a specified node, based on conditions, and replace only a portion of the text with other text. 我正在尝试使用-replace或等效项来编写Powershell脚本,以根据条件搜索指定的节点,并仅将文本的一部分替换为其他文本。 Is this even possible? 这有可能吗?

Here is some example nodes I am trying to edit based on the value of 'Path': 这是我尝试根据'Path'的值编辑的一些示例节点:

<Configuration ConfiguredType="Property" Path="\Package.Variables[User::var1].Properties[Value]" 
    ValueType="String">
        <ConfiguredValue>Some Text Here</ConfiguredValue>
</Configuration>

<Configuration ConfiguredType="Property" Path="\Package.Variables[User::var2].Properties[Value]" 
    ValueType="String">
        <ConfiguredValue>More Text Here</ConfiguredValue>
</Configuration>

Below is my current code setup to replace the entire string but id prefer for it to replace "text" with "content" so the node will now say "Some Content Here". 下面是我当前的代码设置,以替换整个字符串,但id希望它用“ content”替换“ text”,因此节点现在将说“ Some Content Here”。 I tried using -replace but I could not get it to work properly. 我尝试使用-replace,但无法使其正常工作。

#defaults
$xml = [xml](Get-Content $file.FullName)
$node = $xml.DTSConfiguration.Configuration

#updating individual attributes

$pathVar = "var1"
$confVal = ""
($xml.DTSConfiguration.Configuration | Where-Object {$_.Path -like ("*{0}*" -f $pathVar)}).ConfiguredValue = ("{0}" -f $confVal)
$xml.Save($file.FullName)

When working with XML data XPath is usually the most versatile way of accessing nodes and their attributes. 使用XML数据时, XPath通常是访问节点及其属性的最通用的方法。 In your case you want to select the <ConfiguredValue> child node of a <Configuration> node whose Path attribute contains the substring defined in the variable $pathVar . 你的情况,你要选择的<ConfiguredValue>一个的子节点<Configuration>其节点Path属性包含在变量定义的子串$pathVar

$xpath = "//Configuration[contains(@Path, '$pathVar')]/ConfiguredValue"
$node  = $xml.SelectSingleNode($xpath)
$node.'#text' = $node.'#text'.Replace('Text', 'Content')

Beware that both XPath expressions and the Replace() method are case-sensitive. 注意XPath表达式和Replace()方法都区分大小写。

Using the -replace operator (which is case-insensitive by default) is also possible: 也可以使用-replace运算符(默认情况下不区分大小写):

$node.'#text' = $node.'#text' -replace 'Text', 'Content'

The Replace() method provides better performance, though, because it does simple string replacements whereas the -replace operator does regular expression replacements. 不过, Replace()方法可提供更好的性能,因为它执行简单的字符串替换,而-replace运算符执行正则表达式替换。

If I understand your question you are replacing a string token with a string value. 如果我理解您的问题,您将用字符串值替换字符串令牌。

If that is true you can treat the xml as a string and do a replace like below: 如果是这样,则可以将xml视为字符串,并进行如下替换:

$token = 'text'
$value = 'content'
$content = Get-Content $file.FullName
$content = $content.Replace($token, $value)
$content | Out-File $file.FullName

Keep in mind your token should be unique because it will replace all instances of the token. 请记住,您的令牌应该是唯一的,因为它将替换令牌的所有实例。

If you cannot identify a unique token you can do a replace on the string after you select the value from the xml path. 如果您无法识别唯一标记,则可以在从xml路径中选择值之后对字符串进行替换。

(($xml.DTSConfiguration.Configuration | Where-Object {$_.Path -like ("*{0}*" -f $pathVar)}).ConfiguredValue = ("{0}" -f $confVal)).Replace('text','content')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM