gpt4 book ai didi

python - Opsgenie powershell 警报帖子不起作用

转载 作者:行者123 更新时间:2023-12-01 00:02:47 24 4
gpt4 key购买 nike

我已使用 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")

编辑:所以我发现这与我的“回复者”部分有关。它与[]有关...但我一直无法弄清楚到底是什么。如果我删除它们,它就不起作用。如果我把第一个转过来,那就不行了。我可以收到创建成功的警报,但是我不断收到以下错误:

] : 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

最佳答案

您需要将 $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 @invokeRestMethodParams

关于python - Opsgenie powershell 警报帖子不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com