Solved

Powershell Script to parse XML files needs path of file parsed included on each line

Posted on 2015-01-29
4
108 Views
Last Modified: 2015-01-29
I need a way to document what file the report fields are being pulled from in my CSV file. I can't figure it out, please help! My script is below:

$get = get-childitem "D:\Data\Scripts\Projects\Reports\Files" -include *.xml -recurse 
ForEach ($xmlfile in $get)
{
$name = Select-Object FullName
$data = $name
$Report = $xmlfile
$Fields = [xml](Get-Content $Report)
$data += $Fields.reportsettings.fieldlist.field | select @{ L = 'Description'; E = {$_.desc}},
                                                                        @{ L = 'Field Name'; E = {$_.criterion}},
                                                                        @{ L = 'Field ID'; E = {$_.id} } }
$data | Export-Csv D:\Data\Scripts\Projects\Reports\testfields.csv -NoTypeInformation    

Open in new window

0
Comment
Question by:vpetersen
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Like that?
$get = get-childitem "D:\Data\Scripts\Projects\Reports\Files" -include *.xml -recurse
$Data = @()
ForEach ($xmlfile in $get)
{
	$Fields = [xml](Get-Content $xmlfile.FullName)
	$data += $Fields.reportsettings.fieldlist.field | Select-Object -Property `
		@{L = 'Description';	E = {$_.desc}},
		@{L = 'Field Name';		E = {$_.criterion}},
		@{L = 'Field ID';		E = {$_.id}},
		@{L = 'Source File';	E = {$xmlfile.FullName}}
}
$data | Export-Csv D:\Data\Scripts\Projects\Reports\testfields.csv -NoTypeInformation

Open in new window

0
 
LVL 1

Author Comment

by:vpetersen
Comment Utility
I tried something like that and it didn't work. This is what the result shows.

Name                           Value
----                           -----
E                              $xmlfile.FullName
L                              Source File
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
I saved the following as test.xml in D:\Temp:
<reportsettings>
	<fieldlist>
		<field>
			<desc>Description</desc>
			<criterion>Criterion</criterion>
			<id>ID</id>
		</field>
	</fieldlist>
</reportsettings>

Open in new window

I used this as script (which I copied and pasted back from this site, and just replaced the first line to read the test file, and the last line to output to the console):
$get = get-item "D:\Temp\test.xml"
$Data = @()
ForEach ($xmlfile in $get)
{
	$Fields = [xml](Get-Content $xmlfile.FullName)
	$data += $Fields.reportsettings.fieldlist.field | Select-Object -Property `
		@{L = 'Description';	E = {$_.desc}},
		@{L = 'Field Name';		E = {$_.criterion}},
		@{L = 'Field ID';		E = {$_.id}},
		@{L = 'Source File';	E = {$xmlfile.FullName}}
}
$Data | ft -au

Open in new window

And got this as output, which looks just like it should:
Description Field Name Field ID Source File
----------- ---------- -------- -----------
Description Criterion  ID       D:\Temp\test.xml

Open in new window

So please post the exact script you used, and a sample xml.
0
 
LVL 1

Author Comment

by:vpetersen
Comment Utility
I'm an idiot and I didn't see everything you changed. That worked perfectly, thanks so much!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now