?
Solved

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

Posted on 2015-01-29
4
Medium Priority
?
117 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 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 40578120
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
ID: 40578141
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 85

Expert Comment

by:oBdA
ID: 40578192
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
ID: 40578262
I'm an idiot and I didn't see everything you changed. That worked perfectly, thanks so much!
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

840 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