0

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. Is this even possible?

Here is some example nodes I am trying to edit based on the value of '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". I tried using -replace but I could not get it to work properly.

#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)

2 Answers 2

2

When working with XML data XPath is usually the most versatile way of accessing nodes and their attributes. 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.

$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.

Using the -replace operator (which is case-insensitive by default) is also possible:

$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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.