I have a nested hashtable with an array and I want to loop through the contents of another array and add that to the nested hashtable. I'm trying to build a Slack message block.
Here's the nested hashtable I want to add to:
$msgdata = @{
blocks = @(
@{
type = 'section'
text = @{
type = 'mrkdwn'
text = '*Services Being Used This Month*'
}
}
@{
type = 'divider'
}
)
}
$rows = [ ['azure vm', 'centralus'], ['azure sql', 'eastus'], ['azure functions', 'centralus'], ['azure monitor', 'eastus2'] ]
$serviceitems = @()
foreach ($r in $rows) {
$servicetext = "*{0}* - {1}" -f $r[1], $r[0]
$serviceitems += @{'type'='section'}
$serviceitems += @{'text'= ''}
$serviceitems.text.Add('type'='mrkdwn')
$serviceitems.text.Add('text'=$servicetext)
$serviceitems += @{'type'='divider'}
}
$msgdata.blocks += $serviceitems
The code is partially working. The hashtables @{'type'='section'} and @{'type'='divider'} get added successfully. Trying to add the nested hashtable of @{'text' = @{ 'type'='mrkdwn' 'text'=$servicetext }} fails with this error:
Line |
24 | $serviceitems.text.Add('type'='mrkdwn')
| ~
| Missing ')' in method call.
I tried looking through various Powershell posts and couldn't find one that applies to my specific situation. I'm brand new to using hashtables in Powershell.
$serviceitems.text.Add('type', 'mrkdwn')$rows = [ ['azure vm', 'centralus'] ...is a syntax error too, please fix this statement to avoid a distraction.