I need a function with the following requirements:
The function is assigned an unknown object. It is not known how many properties and / or sub-objects the object has. The function then iterates recursively through the object until it knows all the properties of the object.
PowerShell should then output this like a JSON file. But without the added characters ( { } [ ] , ).
I'm pretty sure the ConvertTo-Json cmdlet has such a function, but I can't script / use it myself.
Please format the whole thing with the cmdlet "Format-List" or something like that and not manually with tabs or spaces.
Here is a small example: Example object:
$Car = [PSCustomObject] @{
Tire = [PSCustomObject] @{
Color = "Black"
Count = 4
}
Doors = [PSCustomObject]@{
Color = "Blue"
Diameter = 21
}
}
Output of a JSON file:
{
"Tire": {
"Color": "Black",
"Count": 4
},
"Doors": {
"Color": "Blue",
"Diameter": 21
}
}
Required output:
Tire:
Color: Black
Count: 4
Doors:
Color: Blue
Diameter: 21
The output should be saved in a string variable.
I've been researching for days, but can't find anything.
Thanks you so much.