friends! Starting to work with powershell and encountered a problem. I need to output the values of some variables in a JSON format string in a loop. For this, I use the following code:
.....
foreach ($value in $memoryLast5min)
{
$valueInt = [convert]::ToInt32($value, 10);
if ($valueInt -ge $500MB)
{
$index = [array]::IndexOf($memoryLast5min, $value);
$JSON += @{'problemUsers' = @(
@{
"userName" = $userNames[$index]
"memoryLast5min" = $value
"readLast5min" = $readLast5min[$index]
"writeLast5min" = $writeLast5min[$index]
}
)
} | ConvertTo-JSON;
}
}
.....
return $JSON
In the variable $JSON I get the following result:
{
"problemUsers": [
{
"userName": "User1",
"writeLast5min": 29,
"memoryLast5min": 181,
"readLast5min": 25
}
]
}{
"problemUsers": [
{
"userName": "User2",
"writeLast5min": 80,
"memoryLast5min": 396,
"readLast5min": 74
}
]
}{
"problemUsers": [
{
"userName": "User3",
"writeLast5min": 32,
"memoryLast5min": 169,
"readLast5min": 29
}
]
}
But I need this result:
{
"problemUsers": [
{
"userName": "User1",
"writeLast5min": 29,
"memoryLast5min": 181,
"readLast5min": 25
},
{
"userName": "User2",
"writeLast5min": 80,
"memoryLast5min": 396,
"readLast5min": 74
},
{
"userName": "User3",
"writeLast5min": 32,
"memoryLast5min": 169,
"readLast5min": 29
}
]
}
Please, help me fix the code
@{'problemUsers' =from your$jsonstatement and the json conversion from your loop. Then outside of the loop do@{'problemUsers' = $json} | convertto-Json$JSON = @{'problemUsers' = $JSON} | convertto-Json