Home > Software engineering >  Powershell script output to Discord
Powershell script output to Discord

Time:10-09

removing source for Proprietary reasons

ERROR

Out-String : A positional parameter cannot be found that accepts argument 'System.Object[]'. At line:176 char:28

  • content = $summaries | Out-String `
    

CodePudding user response:

Note: This isn't a complete answer, but it addresses your immediate problem - broken [pscustomobject] literal syntax - and provides some background information.

Format-Table, as all Format-* cmdlets, is only intended to produce output for display as opposed to programmatic processing.

To produce data (objects), use the Select-Object, which uses the same syntax with respect to the -Property parameter as Format-Table, notably including the calculated properties your Format-Table call uses.

Select-Object outputs [pscustomobject] instances that each have the specified properties.

Thus, you may be looking for this:

# Note The ` at the end of the line continues the Select-Object call
# on the next line, and the following lines are an array of 
# property names / calculated properties that are passed 
# to the positionally implied -Property parameter
$payload = $summaries | Select-Object `
    start, 
    end, 
    @{ Label = 'issued_amount';          Expression = { '{0:C0}'     -f $_.issued_amount              }; Align = 'right' },
    @{ Label = 'maturing_amount';        Expression = { '{0:C0}'     -f $_.maturing_amount            }; Align = 'right' }, 
    @{ Label = 'inflation_compensation'; Expression = { '{0:C0}'     -f $_.inflation_compensation     }; Align = 'right' }, 
    @{ Label = 'secondary_purchased';    Expression = { '{0:C0}'     -f $_.secondary_purchased_amount }; Align = 'right' }, 
    @{ Label = 'secondary_sold';         Expression = { '{0:C0}'     -f $_.secondary_sold_amount      }; Align = 'right' }, 
    @{ Label = 'change';                 Expression = { '{0:$#,##0}' -f $_.change                     }; Align = 'right' }

$payload is now an array of [pscustomobject] instances with the specified properties.

However, it looks like you cannot pass such an array directly to Send-DiscordMessage, except perhaps via the -Text parameter, which requires you to create a formatted string representation via Out-String:

Send-DiscordMessage -Uri $hookUrl -Text ($payload | Out-String)
  • Related