我已经成功地使用Python发布了一条警告帖子,但我的powershell警告创建无法正常工作。我只是在我的响应中得到了一堵HTML墙,没有创建警报。消息是唯一的必填字段。下面是我正在使用的,但它不起作用
$api = "XXX"
$URI = "https://api.opsgenie.com/v2/alerts"
$head = @{"Authorization" = "GenieKey $api"}
$body = @{
message = "testing";
responders =
]@{
name = "TEAMNAMEHERE";
type = "team"
}]
} | ConvertTo-Json
$request = Invoke-RestMethod -Uri $URI -Method Post -Headers $head -ContentType "application/json" -Body $body
$request这是我做的python代码,它工作得很好。
import requests
import json
def CreateOpsGenieAlert(api_token):
header = {
"Authorization": "GenieKey " + api_token,
"Content-Type": "application/json"
}
body = json.dumps({"message": "testing",
"responders": [
{
"name": "TEAMNAMEHERE",
"type": "team"
}
]
}
)
try:
response = requests.post("https://api.opsgenie.com/v2/alerts",
headers=header,
data=body)
jsontemp = json.loads(response.text)
print(jsontemp)
if response.status_code == 202:
return response
except:
print('error')
print(response)
CreateOpsGenieAlert(api_token="XXX")编辑:所以我发现这和我的“响应者”栏目有关。这跟...but有关,我还没搞清楚到底是什么。如果我移除它们,它将不起作用。如果我把第一个转过来,它就不会工作了。我可以成功创建警报,但是我一直收到以下错误:
] : The term ']' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At \\file\Tech\user\powershell scripts\.not working\OpsGenieAlert.ps1:7 char:17
+ ]@{
+ ~
+ CategoryInfo : ObjectNotFound: (]:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException发布于 2020-02-14 16:14:28
您需要将$body转换为JSON
$api = "XXX"
$URI = "https://api.opsgenie.com/v2/alerts"
# Declare an empty array
$responders = @()
# Add a new item to the array
$responders += @{
name = "TEAMNAMEHERE1"
type = "team1"
}
$responders += @{
name = "TEAMNAMEHERE2"
type = "team2"
}
$body = @{
message = "testing"
responders = $responders
} | ConvertTo-Json
$invokeRestMethodParams = @{
'Headers' = @{
"Authorization" = "GenieKey $api"
}
'Uri' = $URI
'ContentType' = 'application/json'
'Body' = $body
'Method' = 'Post'
}
$request = Invoke-RestMethod @invokeRestMethodParamshttps://stackoverflow.com/questions/60217365
复制相似问题