1

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

Money Times
  • 135
  • 9
  • 1
    Replace `--dimensions '[{"Name":"InstanceId","Value":"'$instanceName'"}]'` with `--dimensions "[{""Name"":""InstanceId"",""Value"":""$instanceName""}]"` – Mathias R. Jessen Jul 04 '23 at 13:18
  • @MathiasR.Jessen Getting below error aws : At line:4 char:1 + aws cloudwatch put-metric-alarm --alarm-name 'Major Alert > Mem ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Error parsing parameter '--dimensions': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 3 (char 2) JSON received: [{Name:InstanceId,Value:i-000000123456789}] it's looking for the result to be in double quotes – Money Times Jul 04 '23 at 15:26

1 Answers1

1

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 up to PowerShell 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:[1]

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

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

[1] Spelled out for the string in the update to your question:

"[{\`"Name\`":\`"instance\`",\`"Value\`":\`"C:\`"},{\`"Name\`":\`"InstanceId\`",\`"Value\`":\`"$instanceName\`"},{\`"Name\`":\`"objectname\`",\`"Value\`":\`"LogicalDisk\`"}]"
mklement0
  • 382,024
  • 64
  • 607
  • 775