Import xml elements into powershell variables

Viewed 1853

I've figured out how to import an xml file into a Powershell variable using the [xml] tag before the variable and Get-Content, but filling every variable in Powershell from that new xml-formatted variable one at a time is painstaking.

$variable = $xml.vars.Linux.variable1
$variable2 = $xml.vars.Linux.variable2

... and so on.

Is there a shorter/faster way to just import that xml file and set Powershell variables that match the Node names and fill those variables with the associated Node values? So I don't have to specifically fill every Powershell value with it's corresponding named xml Node, I can just use the Powershell variables without lines of code filling them?

I suspect I may need a loop of some sort...current xml structure is 3 layers with category name for second layer Node and then variable names for Nodes within. For example:

<vars>
 <Windows>
  <Hostname>Contoso</Hostname>
  <IP_Address>192.168.1.1</IP_Address>
 </Windows>
 <Linux>
  <variable1>Yup</variable1>
  <variable2>Yessir</variable2>
 </Linux>
</vars>

Thanks!

3 Answers
Related