I am pretty new to PowerShell and have the following code:
$jsonResponse = @"
{
"departments":[{"id":81,"department":"Sales"},{"id":61,"department":"IT Support"}]
}
"@
$myCustomObject = $jsonResponse | ConvertFrom-Json
$myCustomObject.departments.department[0]
$myCustomObject.departments.department[1]
Which allows me to access elements of the customObject (converted from JSON).
What I need is the ability to loop through the object so I can access each element i.e.
object_loop
{
$myCustomObject.departments.department[x]
}
where x is the loop increment.
Sorry of this is silly question but I have googled and can't find a simple example.
Cheers for any help.
Duncs
It is as trivial as
foreach($obj in $myCustomObject.departments)
{
Write-Host ("Got" + $obj.department)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With