New PSCustom Object

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
jvierra
Posts: 11476
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: New PSCustom Object

Post by jvierra » Thu Sep 07, 2017 12:22 pm

If you are trying to do a cross tab then that is something completely different from what you asked for.

User avatar
sekou2331
Posts: 227
Joined: Wed Aug 15, 2012 6:07 am

Re: New PSCustom Object

Post by sekou2331 » Fri Sep 08, 2017 4:23 am

For each file I want all the data in one column. It looks like the script is not doing that.

File1 - value1,value2, value3,value4,value5,value6,value7,value8,value9,value10
File2 - value11,value12, value13,value14,value15,value16,value17,value18,value19,value20
File3 - value21,value22, value23,value24,value25,value26,value27,value28,value29,value30


File1 File2 File3
_____ _______ ______
value1 value11 value21
value2 value12 value22
value3 value13 value23
value5 value14 value24
value6 value15 value25
value7 value16 value26
value8 value19 value27
value9 value18 value28
value10 value20 value30

User avatar
jvierra
Posts: 11476
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: New PSCustom Object

Post by jvierra » Fri Sep 08, 2017 4:45 am

That is because you want a "crosstab" result an not just an object representation of the data. This is a much more difficult task as it requires reordering all data. You need to create on object for each column in the XML data that has one property for each file and add the elements in a loop generating one object for each column.

It will look something like this:

Code: Select all

$files = 'C:\Program Files\file1.xml','C:\Program Files\file2.xml','C:\Program Files\file3.xml'
[array]$csvdata = foreach($file in $files){
	[xml]$xml = Get-Content $file
	$xml.SelectSingleNode('//add [@key="value"]').Value -split ','
}

$hashes = New-Object hashtable[] 20
for($j = 0;$j -lt $csvdata.Count;$j++){
	for($i = 0;$i -lt 20;$i++){
		$hashes[$j]["File $j"] = $csv[$i][$i]
    }
}

$hashes | ForEach-Object{[pscustomobject]$_}


Locked