I have the following array with similar PSObjects :
PS C:\Test\$events
total events
----- ------
867 {@{category_id=1; content_ver=Version: 2022-10-26 rev. 008; Sequence: 221026008; count=1; cybox=; device_end_time=1666837251000; device_ip=X.X.X.X; device_location=; device_time=1666837251000; feature_name=MALWARE_PROTECTION; feature_ui…
867 {@{category_id=1; content_ver=Version: 2022-10-30 rev. 003; Sequence: 221030003; count=1; cybox=; device_end_time=1667163237000; device_ip=X.X.X.X; device_location=; device_time=1667163237000; feature_name=MALWARE_PROTECTION; feature_uid=…
867 {@{category_id=1; content_ver=Version: 2022-10-30 rev. 003; Sequence: 221030003; count=1; cybox=; device_end_time=1667163237000; device_ip=X.X.X.X; device_location=; device_time=1667163237000; feature_name=MALWARE_PROTECTION; feature_uid=…
867 {@{category_id=1; content_ver=Version: 2022-10-30 rev. 003; Sequence: 221030003; count=1; cybox=; device_end_time=1667163237000; device_ip=X.X.X.X; device_location=; device_time=1667163237000; feature_name=MALWARE_PROTECTION; feature_uid=…
867 {@{category_id=1; content_ver=Version: 2022-10-30 rev. 003; Sequence: 221030003; count=1; cybox=; device_end_time=1667163237000; device_ip=X.X.X.X; device_location=; device_time=1667163237000; feature_name=MALWARE_PROTECTION; feature_uid=…
every object from $events.events has similar properties, because they're coming from an API query with pagination, with one query page being a line in the array.
My question : how can I merge all objects from this list as a single PSObject, and keeping the exact same properties ?
CodePudding user response:
I'm guessing you're looking to group all objects by the Total
property and create a new unified object that contains all objects in Events
.
If my assumption is right, you can group them using Group-Object
and then create a new instance:
$newObject = $events | Group-Object Total | ForEach-Object {
[pscustomobject]@{
Total = [Linq.Enumerable]::Sum([int[]] $_.Group.Total)
Events = $_.Group.Events
}
}