Solved

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

Posted on 2015-01-29
4
110 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 83

Accepted Solution

by:
oBdA earned 500 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 83

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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