【发布时间】:2021-08-05 13:58:47
【问题描述】:
我想通过 powershell 脚本中的 cli 命令向 aws 发送事件。这是我需要发送到事件桥的 Json:
[
{
"Sensor":"{\"id\":\"880/2021-04-13\",\"attributes\":\"green\",\"Name\":\"SensorGreen\",\"state\":\"SUCCEEDED\"}",
"Source":"google.com"
}
]
这是在 powershell 中尝试过的:
$json='[{"Sensor":"{\"id\":\"880/2021-04-13\",\"attributes\":\"green\",\"Name\":\"SensorGreen\",\"state\":\"SUCCEEDED\"}","Source":"google.com"}]'|ConvertTo-Json -Compress
aws events put-events --entries $json --region "eu-central-1"
那行不通。我什至尝试将它写入一个 json 文件并读取它并从文件中发送它,但它不起作用。它以某种方式导致我的“传感器”有太多“\”斜线或没有斜线,这是必要的。我什至尝试创建一个对象并将 Sensor 对象转换为 Json,然后再将整个对象转换为转义,但它不起作用。
编辑:
i tried also this as mentioned in the answer:
$RequestObject = [pscustomobject] @(@{
Sensor = [pscustomobject] @{
id = "880/2021-04-13"
attribute = "green"
Name = "SensorGreen"
state = "SUCCEEDED"
}
Source = "google.com"
})
$RequestObject.Get(0).Sensor=$RequestObject.Get(0).Sensor | ConvertTo-Json -Compress
$Json = $RequestObject | ConvertTo-Json -Compress
aws events put-events --entries $Json --region "eu-central-1"
我将@() 包含在一个数组中,并将传感器对象两次转换为 json 以包含斜杠。但结果是:
Error parsing parameter '--entries': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
如果我仅将命令行界面与 aws 命令一起使用并将对象直接放入命令中,那么它可以工作.. 但 powershell 不能。
【问题讨论】:
标签: json amazon-web-services powershell powershell-2.0 aws-cli