0

This is a pre-defined variable for an API call.

$CorePluginConfigurationContext = ([xml]"
<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address></Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>
").DocumentElement

Reading PowerShell books, online documentation, XML namespaces, Nodes, Parents, Childs, Appending, "element""s", I simply could not able to add data to this specific variable.

I'm getting multiple values as parameters

Param(
    [Parameter(Mandatory=$true)]
    [String[]]$nodes = "1.1.1.1"
)

And need to add these values to the XML object.

foreach ($node in $nodes) { }

And the result would look like this:

<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address>10.10.1.19</Address>
        </IpAddress>
        <IpAddress>
            <Address>10.10.1.20</Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>

The test code:

Param (
    [Parameter(Mandatory=$true)]
    [String[]]$nodes = "1.1.1.1"
)

$CorePluginConfigurationContext = ([xml]"
<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address></Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>
").DocumentElement

foreach ($node in $nodes) {
    # Code to add multiple $node values to the XML above
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
OSB
  • 159
  • 1
  • 3
  • Unfortunately you didn't post the code you have already tried, so it is very hard to say what your problem is. So please [edit] your question and add your code. Meanwhile maybe [this might be helpful](https://stackoverflow.com/questions/26522980/select-xml-element-by-attribute-value-and-add-an-element) – Ocaso Protal Nov 18 '19 at 08:18

2 Answers2

0

For one thing, you should assign the XML document to your variable, not just the document element (i.e. the root node):

$xml = [xml]"
<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address></Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>"

It's not a big issue, since you can get the document from the node via $xml.OwnerDocument, but it's still better form to have the variable hold the actual document object.

Also, you shouldn't put empty nodes into the document if you intend to add data later, because you'd have to either remove the empty nodes or assign one of the values differently from all other values.

Next, your XML document has namespaces, so you need a namespace manager.

$uri = 'http://schemas.solarwinds.com/2012/Orion/Core'
$nm = New-Object Xml.XmlNamespaceManager $xml.NameTable
$nm.AddNamespace('ns', $uri)

Using that namespace manager you can select the node you want to append to:

$bulklist = $xml.SelectSingleNode('ns:BulkList', $nm)

Then loop over your input values, create nodes for them, and append them to the selected parent node:

foreach ($node in $nodes) {
    $addr = $xml.CreateElement('Address', $uri)
    $addr.InnerText = $node

    $ip = $xml.CreateElement('IpAddress', $uri)
    [void]$ip.AppendChild($addr)

    [void]$bulklist.AppendChild($ip)
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for pointing the right direction, I knew namespace is something that I need to focus on. I still do not understand if "BulkList" is a namespace or not. – OSB Nov 18 '19 at 09:28
  • 1
    @OSB `` is a node, not a namespace. Namespaces are defined via `xmlns` attributes and used by prefixing a node name with the namespace name (``). Only the default namespace (the one defined via `xmlns=` is applied without prefix. [Related](https://stackoverflow.com/a/35653697/1630171). – Ansgar Wiechers Nov 18 '19 at 10:08
  • did not work, still do not know how to construct node and childs – OSB Nov 19 '19 at 15:30
  • 1
    "Did not work" is not a valid problem description. And the code for constructing nodes is in my answer. – Ansgar Wiechers Nov 19 '19 at 15:55
0

Using below, I was able to do what I wanted to do.

$bulk = $CorePluginConfigurationContext.BulkList
$ipAdd = $bulk.IpAddress.Clone()
$bulk.IpAddress.Address = $nodes[0]
for ($i = 1; $i -lt $nodes.Length; $i++)
{
    $node = $nodes[$i]
    $another = $ipAdd.Clone()
    $another.Address = $node
    [void]$bulk.AppendChild($another)
}
OSB
  • 159
  • 1
  • 3