I'm new to JSON and PowerShell. I'm wanting to receive a JSON object from AWS that indicates a server instance's status, and then read that status property programmatically so I can execute different logic based upon whether or not the instance is running.
Here's my PowerShell script:
$json = aws ec2 describe-instance-status --instance-id [instance ID redacted]
$outPath = 'C:\Scripts\results.json'
$json | ConvertTo-Json -depth 100 | Set-Content $outPath
$newJson = Get-Content $outPath -Raw | ConvertFrom-Json
echo $newJson.InstanceStatuses.InstanceStatus.Status
Here's the $json and $newJson to the console if I echo either of those out:
{
"InstanceStatuses": [
{
"AvailabilityZone": "us-east-2c",
"InstanceId": "[instance ID redacted]",
"InstanceState": {
"Code": 16,
"Name": "running"
},
"InstanceStatus": {
"Details": [
{
"Name": "reachability",
"Status": "passed"
}
],
"Status": "ok"
},
"SystemStatus": {
"Details": [
{
"Name": "reachability",
"Status": "passed"
}
],
"Status": "ok"
}
}
]
}
However, if I try to access and echo out one of the properties (e.g. echo $newJson.InstanceStatuses.InstanceStatus.Status) I get nothing at the console. No error, no output, just a blank line.
If I try to access a property as an array (as hinted at in this SO answer), I get a cannot index into null array
exception. I get the same exception if I try to treat the JSON as a dictionary and access properties by keys (as suggested in this SO answer).
I've reviewed the examples given in this SO question, this SO question, and this SO question in the course of (attempted) debugging, but have had no success.