Skip to content

Instantly share code, notes, and snippets.

@ismits
Created September 24, 2019 16:49
Show Gist options
  • Save ismits/f6ad7873ee63bd1dd9f186b93fda4fd4 to your computer and use it in GitHub Desktop.
Save ismits/f6ad7873ee63bd1dd9f186b93fda4fd4 to your computer and use it in GitHub Desktop.
Update Xml via PowerShell
function UpdateXml([string]$InputFile = $(Throw "Please provide a path to the input XML file in the InputFile parameter."),
[string]$OutputFile = $InputFile,
[string]$ParentXPath = $(Throw "Please provide an XPath expression to where you want to append/edit a child XML element in the ParentXPath parameter."),
[string]$ElementName = $(Throw "Please provide the name of the XML element to append/edit."),
[string]$ElementAttributeName,
[string]$ElementAttributeValue)
{
if (Test-Path -Path $InputFile)
{
Write-Log "Loading XML document from file: $InputFile"
$Doc = Select-Xml -Path $InputFile -XPath "/"
if ($Doc -ne $null)
{
$UpdatesMade = $False
$ParentNode = $Doc.Node.SelectSingleNode($ParentXPath)
if ($ParentNode -ne $null)
{
Write-Log "Existing XML elements under '$ParentXPath':"
$ParentNode.ChildNodes | % { Write-Log "- $($_.Name)" }
$ExistingElement = $ParentNode.SelectSingleNode("./$ElementName")
if ($ExistingElement -ne $null)
{
if (![System.String]::IsNullOrEmpty($ElementAttributeName))
{
$ExistingAttribute = $ExistingElement.Attributes.GetNamedItem($ElementAttributeName)
if ($ExistingAttribute -ne $null)
{
if ($ExistingAttribute.Value -ne $ElementAttributeValue)
{
$ExistingAttribute.Value = $ElementAttributeValue
$UpdatesMade = $true
Write-Log "Updated existing XML element '$ElementName' attribute name '$ElementAttributeName' with a new attribute value '$ElementAttributeValue'."
}
else
{
Write-Log "Existing XML element '$ElementName' already has correct attribute name '$ElementAttributeName' and value '$ElementAttributeValue'."
}
}
else
{
$NewAttribute = $Doc.Node.CreateAttribute($ElementAttributeName)
$NewAttribute.Value = $ElementAttributeValue
$NewAttributeAppened = $ExistingElement.Attributes.Append($NewAttribute)
$UpdatesMade = $true
Write-Log "Updated existing XML element '$ElementName' with a new attribute name '$ElementAttributeName' and value '$ElementAttributeValue'."
}
}
}
else
{
$NewElement = $Doc.Node.CreateElement($ElementName)
if (![System.String]::IsNullOrEmpty($ElementAttributeName))
{
$NewAttribute = $Doc.Node.CreateAttribute($ElementAttributeName)
$NewAttribute.Value = $ElementAttributeValue
$NewAttributeAppened = $NewElement.Attributes.Append($NewAttribute)
}
$NewNode = $ParentNode.AppendChild($NewElement)
$UpdatesMade = $true
Write-Log "Create a new XML element '$ElementName' with a new attribute name '$ElementAttributeName' and value '$ElementAttributeValue'."
}
Write-Log "Current XML elements under '$ParentXPath':"
$ParentNode.ChildNodes | % { Write-Log "- $($_.Name)" }
}
else
{
Write-Log "Error: No element found at: $ParentXPath"
}
if ($UpdatesMade -or ($InputFile -ne $OutputFile))
{
$Doc.Node.Save($OutputFile)
Write-Log "Saved updated XML to: $OutputFile"
}
else
{
Write-Log "No updates required to file: $OutputFile"
}
}
else
{
Write-Log "Error: Unable to load XML document: $InputFile"
}
}
else
{
Write-Log "Error: Input file not found: $InputFile"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment