New PSCustom Object

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PSCustom Object

Post by jvierra »

If you are trying to do a cross tab then that is something completely different from what you asked for.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: New PSCustom Object

Post by sekou2331 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New PSCustom Object

Post by jvierra »

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]$_}

This topic is 6 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked