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
}
1
  • 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 Commented Nov 18, 2019 at 8:18

2 Answers 2

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)
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 <BulkList> is a node, not a namespace. Namespaces are defined via xmlns attributes and used by prefixing a node name with the namespace name (<namespace:node>). Only the default namespace (the one defined via xmlns= is applied without prefix. Related.
did not work, still do not know how to construct node and childs
"Did not work" is not a valid problem description. And the code for constructing nodes is in my answer.
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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.