I have an array of PSCustomObject created with several properties on them. Some properties are ints, some strings, and others are what I surmise to be dictionary object (the objects are being returned from Invoke-RestMethod) Here's an example:
> $items[0]
id : 42
name : Modularize the widget for maximum reuse
priority : {[id, 1], [order, 1], [name, High]}
project : {[id, 136], [name, Wicked Awesome Project]}
Ultimately, what I want to do is to "flatten" this structure so that I can pipe to to Export-CSV and all the data is retained. Each property would become a column. Example:
id : 42
name : Modularize the widget for maximum reuse
priority_id : 1
priority_order : order
priority_name : High
project_id : 135
project_name : Wicked Awesome Project
So, my thought was to enumerate through the properties and if any one is a Dictionary/HashTable/PSCustomObject to enumerate through it's properties and add them the to parent property to flatten this structure out.
However, I have not been able to successfully infer whether or not a property is a Dictionary/HashTable/PSCustomObject. I loop through all the properties like so.
foreach($property in $item.PsObject.Properties)
{
Write-Host $property
Write-Host $property.GetType()
Write-Host "-------------------------------------------------------------"
# if a PSCustomObject let's flatten this out
if ($property -eq [PsCustomObject])
{
Write-Host "This is a PSCustomObject so we can flatten this out"
}
else
{
Write-Host "Use the raw value"
}
}
For properties that appear to be PSCustomObject this prints out the following.
System.Management.Automation.PSCustomObject project=@{id=135}
System.Management.Automation.PSNoteProperty
-------------------------------------------------------------
However, I am unable to to conditionally check that this ia PSCustomObject. All conditionals that I have tried fall under the else condition. I have tried replacing [PSCustomObject] with [Dictionary] and [HashTable]. Note that checking the type won't help since they all appear to be PSNoteProperty.
How can I check that a property is in fact a PSCustomObject and therefore needs to be flattened?