Reputation: 35679
I'm hitting a brick wall trying to detect whether a node already exists while manipulating an XML file in Powershell.
Unfortunately the XmlDocument I'm processing uses default namespaces which I think confuses things.
Here's a subset of the XML
<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns="http://www.sdltridion.com/2009/GUI/Configuration" xmlns:ext="http://www.sdltridion.com/2009/GUI/extensions" xmlns:cmenu="http://www.sdltridion.com/2009/GUI/extensions/ContextMenu">
<editor name="MyExtension">
<installpath xmlns="http://www.sdltridion.com/2009/GUI/Configuration">C:\Program Files (x86)\Tridion\web\WebUI\Editors\MyExtension</installpath>
<configuration xmlns="http://www.sdltridion.com/2009/GUI/Configuration">Editor\Configuration\editor.config</configuration>
<vdir xmlns="http://www.sdltridion.com/2009/GUI/Configuration">MyExtension</vdir>
</editor>
</editors>
</Configuration>
I want to check whether the editor node with the name attribute set to "MyExtension" exists in Powershell and add it if it doesn't.
Adding it is fine (apart from some namespace shenanigans) but detecting it has me stumped.
Here's what I've tried so far:
# Update System.config
$filename = $tridionInstallLocation + '\web\WebUI\WebRoot\Configuration\System.config'
$conf = [xml](gc $filename)
[System.Xml.XmlNamespaceManager] $nsm = new-object System.Xml.XmlNamespaceManager $conf.NameTable
$nsm.AddNamespace("x", "http://www.sdltridion.com/2009/GUI/Configuration")
# Editor
$xpath = "//x:Configuration/x:editors/x:editor[name=MyExtension]"
# $existingModelNode = $conf.SelectSingleNode($xpath, $nsm)
$existingModelNode = Select-Xml $conf -XPath $xpath
Write-Host $existingEditorNode # This is blank always
if($existingEditorNode -eq $null)
{
$editors = [System.Xml.XmlElement]$conf.Configuration.editors
$myElement = $conf.CreateElement("editor", $nsm.LookupNamespace("x"))
$nameAttr = $myElement.SetAttribute("name", $name)
$myElement.InnerXml = "<installpath xmlns='http://www.sdltridion.com/2009/GUI/Configuration'>" + $editorInstallLocation + "</installpath><configuration xmlns='http://www.sdltridion.com/2009/GUI/Configuration'>" + $editorConfigFile + "</configuration><vdir xmlns='http://www.sdltridion.com/2009/GUI/Configuration'>" + $name + "</vdir>"
$editors.AppendChild($myElement)
}
else
{
Write-Host "Editor node already exists in System.config with name $name, skipping"
}
I've been going round in circles trying various different methods. Can anyone help me out?
Upvotes: 1
Views: 5914
Reputation: 68243
Seems like this should work:
@($config.Configuration.editors.editor.name) -contains 'MyExtension'
Upvotes: 3