Skip to content

Instantly share code, notes, and snippets.

@jgeorgeson
Created August 25, 2017 16:57
Show Gist options
  • Save jgeorgeson/043b65c646abe63ab789b625ebf05d75 to your computer and use it in GitHub Desktop.
Save jgeorgeson/043b65c646abe63ab789b625ebf05d75 to your computer and use it in GitHub Desktop.
POSH update XPath with specified value
# 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