0

I am having 3 arrays

 $IPAddress
 $VMName
 $HostName

What i would like to do is each value in these arrays should be stored in corresponding XML entries.

For ex. All these 3 arrays are having around 10 entries.

First entry in all the three arrays should be stored in first XML tag, second entries in second XML tag and so on. My XML file should be looks like below.

<VM>
  <IPAddress>IPAddress[0]</IPAddress>
  <VMName>VMName[0]</VMName>
  <HostName>HostName[0]</HostName>
</VM>

<VM>
  <IPAddress>IPAddress[1]</IPAddress>
  <VMName>VMName[1]</VMName>
  <HostName>HostName[1]</HostName>
</VM>

How to store the values into XML?

1 Answer 1

2
$pattern = '
<VM>
  <IPAddress>{0}</IPAddress>
  <VMName>{1}</VMName>
  <HostName>{2}</HostName>
</VM>
'
$elements = 0..($IPAddress.Length-1) | % { $pattern -f $IPAddress[$_], $VmName[$_], $HostName[$_] }

And now simply wrap the $elements in a root and store in a file.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.