Created
August 25, 2017 16:57
-
-
Save jgeorgeson/043b65c646abe63ab789b625ebf05d75 to your computer and use it in GitHub Desktop.
POSH update XPath with specified value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inputs | |
param ( | |
[string] $doc = $(throw "doc is a required parameter"), | |
[string] $xpath = $(throw "xpath is a required parameter"), | |
[string] $value = $(throw "value is a required parameter") | |
) | |
function Edit-XmlNodes { | |
# Open the file | |
$filepath = Resolve-Path $doc | |
$xml = [xml](Get-Content $filepath) | |
# Find the nodes to modify | |
$nodes = $xml.SelectNodes($xpath) | |
# Set value of discovered nodes | |
foreach ($node in $nodes) { | |
write-host $node.OuterXml | |
if ($node -ne $null) { | |
if ($node.NodeType -eq "Element") { | |
$node.InnerXml = $value | |
} | |
else { | |
$node.Value = $value | |
} | |
} | |
write-host $node.OuterXml | |
} | |
# Save result | |
$xml.Save($filepath) | |
} | |
Edit-XmlNodes -doc $doc -xpath $xpath -value $value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment