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

huangapple go评论53阅读模式
英文:

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

问题

I understand your request to translate the provided text without addressing your translation request. Here's the translated text:

当我尝试通过Powershell和AWS Cloud CLI为多个实例创建CloudWatch告警时(如下所示),我收到以下错误消息:

$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'
}

我收到以下错误消息:
> 字符串格式错误:输入字符串格式不正确。
> 在第4行的位置:1处

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

> 字符串格式错误:输入字符串格式不正确。
在第4行的位置:1处

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

即使我只运行用于创建警报的CloudWatch CLI命令,我仍然收到有关解析JSON语法的相同错误
"Value":"'+$instanceName+'"

对此的任何建议将不胜感激!!

最后一部分解析错误:

先生,寻求您的最后一次帮助:
这是原文字符串 '[{"Name":"instance","Value":"C:"},{"Name":"InstanceId","Value":"i-0123abcxyz"},{"Name":"objectname","Value":"LogicalDisk"}]',我已根据您展示的方式进行了解析:[{"\`Name\`":"\`instance\`","\`Value\`":"\`C:\`"},{\`"Name\`":\`"InstanceId\`",\`"Value\`":\`"'$instanceName'\`"},{"\`Name\`":"\`objectname\`","\`Value\`":"\`LogicalDisk\`"}]",但我仍然收到错误消息。我还遗漏了其他什么吗?仍然显示格式不正确。对此有任何回应吗?

英文:

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

答案1

得分: 1

以下是翻译好的部分:

<!-- language-all: sh -->

存在两个问题:

* 您正在尝试使用*字符串插值*,但您正在使用[文本 (单引号) 字符串 (`'...'`)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules#single-quoted-strings),其中嵌入的变量引用,如 `$instanceName`,按设计不会被展开(插值)。

   * 您**必须使用可展开的 (双引号) 字符串 (`"..."`)** 进行字符串插值(有关 PowerShell 字符串插值的语法和规则的简要总结,请参阅[此答案](https://stackoverflow.com/a/40445998/45375))。

   * 在这种字符串内部,任何_嵌入的_ `"` 字符必须转义为 `` `" ```""`
* 由于一个长期存在的 bug,直到 PowerShell v7.2.x,传递给_外部程序_的参数中嵌入的 `"` 字符必须_手动_用 `\` 转义(请注意,对于 PowerShell 本身,`\` 没有_特殊的_含义)。

  * 请参阅[此答案](https://stackoverflow.com/a/66837948/45375)以获取背景信息。

因此,请按以下方式构建您的 `--dimensions` 参数:

* 在 PowerShell 7.2.x 及更早版本:<sup>[1]</sup>

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


* 在 PowerShell 7.3+(不再需要手动转义 `"`):

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

---


<sup>[1] 为您的问题更新的字符串拼写如下:
 <pre>"[{\`"Name\`":\`"instance\`",\`"Value\`":\`"C:\`"},{\`"Name\`":\`"InstanceId\`",\`"Value\`":\`"$instanceName\`"},{\`"Name\`":\`"objectname\`",\`"Value\`":\`"LogicalDisk\`"}]"</pre></sup>
英文:

<!-- language-all: sh -->

There are two problems:

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

  • Due to a long-standing bug present up to PowerShell v7.2.x, &quot; 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:<sup>[1]</sup>

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

    --dimensions &quot;[{`&quot;Name`&quot;:`&quot;InstanceId`&quot;,`&quot;Value`&quot;:`&quot;$instanceName`&quot;}]&quot;
    

<sup>[1] Spelled out for the string in the update to your question:
<pre>"[{`"Name`":`"instance`",`"Value`":`"C:`"},{`"Name`":`"InstanceId`",`"Value`":`"$instanceName`"},{`"Name`":`"objectname`",`"Value`":`"LogicalDisk`"}]"</pre></sup>

huangapple
  • 本文由 发表于 2023年7月4日 21:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定