You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS CLI在PowerShell调用describe-alarms时us-east-1区域编码异常

解决PowerShell调用AWS CLI读取us-east-1区域CloudWatch告警的编码异常问题

问题根源

us-east-1区域的CloudWatch告警名称里包含了\uf13f这类特殊Unicode字符,AWS CLI默认输出编码和PowerShell的默认读取编码不兼容,导致字符编码失败,进而触发JSON解析错误。

解决方法

方法1:强制统一UTF-8编码处理

修改命令,让AWS CLI明确输出UTF-8编码,同时让PowerShell以UTF-8格式读取输出,彻底解决编码冲突:

$alarmNames = aws cloudwatch --profile $profile --region $region describe-alarms --query MetricAlarms[].AlarmName[] --output json --encoding utf-8 | Out-String -Encoding utf8 | ConvertFrom-Json
  • --encoding utf-8:指定AWS CLI输出UTF-8格式内容
  • Out-String -Encoding utf8:确保PowerShell正确识别UTF-8编码的输出,避免乱码或编码错误

方法2:预处理输出,过滤特殊字符

如果不需要保留特殊字符,可以先清理输出内容中的非ASCII字符,再解析JSON:

# 获取原始输出
$rawOutput = aws cloudwatch --profile $profile --region $region describe-alarms --query MetricAlarms[].AlarmName[] --output json --encoding utf-8 | Out-String -Encoding utf8
# 过滤所有非ASCII字符(或针对性替换特定字符)
$cleanOutput = $rawOutput -replace '[^\x00-\x7F]', ''
# 解析JSON
$alarmNames = $cleanOutput | ConvertFrom-Json

如果需要保留部分特殊字符,可将替换规则改为针对性处理\uf13f:

$cleanOutput = $rawOutput -replace '\uf13f', '替换为你需要的字符'

方法3:从根源修复告警名称

直接登录AWS控制台,切换到us-east-1区域,找到包含特殊字符的CloudWatch告警,修改告警名称为仅含合法ASCII字符的名称。这种方法能一劳永逸,避免后续脚本再次遇到相同问题。

内容的提问来源于stack exchange,提问作者Money Times

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 20:46:15