Ваш Json не совсем корректен, поэтому мне нужно было немного его настроить:
$Tasks = ConvertFrom-Json @'
[
{
id: 1,
name: "post question",
status: "FAILED",
tasks: [
{
id: 10,
name: "post answer",
status: "PLANNED",
tasks: [
{
}
]
},
{
id: 20,
name: "post answer",
status: "FAILED",
tasks: [
{
id: 50,
name: "any text",
status: "PASS",
tasks: [
{
}
]
}
]
}
]
},
{
id: 2,
name: "Load question",
status: "SUCCESS",
tasks: [
{
id: 30,
name: "post answer",
status: "PLANNED",
tasks: [
{
}
]
}
]
}
]
'@
На самом деле рекурсивная функция довольно проста:
Function Get-Tasks($Tasks) {
ForEach($Task in $Tasks) {
If ($Task.id) {$Task | Select-Object id, name, status}
If ($Task.Tasks) {Get-Tasks $Task.Tasks}
}
}
Get-Tasks $Tasks
Результат:
id name status
-- ---- ------
1 post question FAILED
10 post answer PLANNED
20 post answer FAILED
50 any text PASS
2 Load question SUCCESS
30 post answer PLANNED