Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 856
  • Last Modified:

Powershell Parse XML and create multiple files

For instnace .....................

<Item>
   <value>a</value>
   <value>b</value>
</Item>

How do I extract contents of  <value> </value>  .InnerXml or "a" to a file and create a naming convention. then do the same for  <value> </value>  .InnerXml or "b"   and so on is this possible?

[xml]$xml = (get-content "path.xml")


Foreach ($value in $Item) {

write-host $value.InnerXml

$xml.save("C:\Scripts\new.xml*")
}
This doesent work.
0
benpal2476
Asked:
benpal2476
  • 4
  • 2
1 Solution
 
Dale HarrisProfessional Services EngineerCommented:
Try this:

$XML | gm

This will give you the possible properties and methods you can use.

You're looking for the last property, which is often the name of the first branch of the tree (in an Infopath Form, it's Myfields)

$XML.myfields | gm

This will give you the possible branches below this.

$XML.myfields.name

This will show you the value of the name property.  You can easily assign this to a variable.

This may or may not be completely what you need, but I think it will definitely get you started.

-Dale Harris
0
 
benpal2476Author Commented:
[xml]$xml = (get-content "path.xml")


Foreach ($value in $Item) {

write-host $value.InnerXml

#$xml.save("C:\Scripts\new.xml*")
#without the "*" it writes to ("C:\Scripts\new.xml")

}

#but I want it to get InnerText for the <value> </value> there are multiple <value> within the xml I would like to get each <value> and output that data to a seperate file.
0
 
Dale HarrisProfessional Services EngineerCommented:
Try this instead:

foreach ($group in $xmlData.benchmark.group)
{
  $group.id
  $group.title
  $group.rule.id
}


Again, you're going to want to try to get a single value at the least to make sure it's working like you planned.

DH
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Dale HarrisProfessional Services EngineerCommented:
Also, if you'd like to have me help a little more, you can post a portion of your XML code (so I can actually run it through and attempt to work with it myself) and I'll see if I can't cook something up.

DH
0
 
benpal2476Author Commented:
[xml]$xml = (get-content "C:\Users\user\Desktop\xml")

$xml.SelectNodes("/Item/Alias") |
Select * | Format-List

I created this it gives me a break down of Alias in the document, I know there are 40 Alias from doing a .count but now i'd like to save the innertext or inner xml of Alias to a seperate file.

Thanks for your time.
0
 
Dale HarrisProfessional Services EngineerCommented:
Using the following command will help:
$VariablewithStuffInside| get-member

This is the best way to see what kind of properties and method are in that object, just so you can see if you can hit a command to make it work.

I would love your xml file  you're actually importing with 40 aliases.  This way, I can just take a few minutes to look it over, generate the exact code you need, since you seem to having a difficulty getting the information you need out of this.

DH
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now