Solved

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

Posted on 2015-01-29
4
113 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 84

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 84

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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.
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…

730 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