Solved

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

Posted on 2015-01-29
4
114 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

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 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

717 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