Search code examples
jsonpowershellaws-cliamazon-cloudwatch

String parsing error while creating cloudwatch alarm via aws cli in PowerShell


When I am trying to create the cloudwatch alarms for multiple instances via powershell wth of aws cloud cli (below)

$instanceNames = "i-000000123456789", "i-000000123456777";

foreach ($instanceName in $instanceNames) {
    aws cloudwatch put-metric-alarm --alarm-name 'Major Alert > Memory Usage exceeds 90%' --alarm-description 'Major Alert > Memory Usage exceeds 90%' --actions-enabled --alarm-actions 'arn:aws:sns:us-east-1:123456789:Test_Topic' 'arn:aws:sns:us-east-1:123456777:Test_Topic' --metric-name 'Memory % Committed Bytes In Use' --namespace 'CWAgent' --statistic 'Average' --dimensions '[{"Name":"InstanceId","Value":"'$instanceName'"}]' --period 300 --evaluation-periods 1 --datapoints-to-alarm 1 --threshold 90 --comparison-operator 'GreaterThanThreshold' --treat-missing-data 'missing'
}

I am getting the below error message:

Error formatting a string: Input string was not in a correct format.. At line:4 char:1

  • aws cloudwatch put-metric-alarm --alarm-name 'Major Alert > Memory Us ...
  •   + CategoryInfo          : InvalidOperation: ({0}"}]:String) [], RuntimeException
      + FullyQualifiedErrorId : FormatError
    
    

Error formatting a string: Input string was not in a correct format.. At line:4 char:1

  • aws cloudwatch put-metric-alarm --alarm-name 'Major Alert > Memory Us ...
  •   + CategoryInfo          : InvalidOperation: ({0}"}]:String) [], RuntimeException
      + FullyQualifiedErrorId : FormatError
    
    

Even when I am simply running the cloudwatch cli command for creating an alarm - I getting the same error for parsing the json syntax "Value":"'+$instanceName+'"

Any inputs on this will be highly appreciated !!

Last part parsing error:

Sir, looking for your last assistance: this is the verbatim string '[{"Name":"instance","Value":"C:"},{"Name":"InstanceId","Value":"i-0123abcxyz"},{"Name":"objectname","Value":"LogicalDisk"}]' and I have parsed it with the below one theway you showed "[{"\`Name\`":"\`instance\`","\`Value\`":"\`C:\`"},{\`"Name\`":\`"InstanceId\`",\`"Value\`":\`"'$instanceName'\`"},{"\`Name\`":"\`objectname\`","\`Value\`":"\`LogicalDisk\`"}]" but still I'm getting error.

Am I missing anything else here ? still it is showing incorrect format. any response on this


Solution

  • There are two problems:

    • You're trying to use string interpolation, yet you're using a verbatim (single-quoted) string ('...'), in which embedded variable references such as $instanceName are by design not expanded (interpolated).

      • You must use an expandable (double-quoted) string ("...") for string interpolation (for a concise summary of the syntax and rules of PowerShell's string interpolation, see this answer).

      • Inside such a string, any embedded " chars. must be escaped as `" or "".

    • Due to a long-standing bug present in Windows PowerShell and PowerShell (Core) up to v7.2.x, " characters embedded in arguments passed to external programs must manually be escaped with \ (note that to PowerShell itself \ has no special meaning).

    Therefore, formulate your --dimensions argument as follows:

    • Up to PowerShell 7.2.x:

      --dimensions "[{\`"Name\`":\`"InstanceId\`",\`"Value\`":\`"$instanceName\`"}]"
      
    • In PowerShell 7.3+ (manual \-escaping of " no longer necessary):

      --dimensions "[{`"Name`":`"InstanceId`",`"Value`":`"$instanceName`"}]"
      

    For programmatic escaping and an alternative solution for in Windows PowerShell and PowerShell (Core) 7.2.x and below, see this answer.