首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Opsgenie powershell警报开机自检不起作用

Opsgenie powershell警报开机自检不起作用
EN

Stack Overflow用户
提问于 2020-02-14 06:22:25
回答 1查看 657关注 0票数 1

我已经成功地使用Python发布了一条警告帖子,但我的powershell警告创建无法正常工作。我只是在我的响应中得到了一堵HTML墙,没有创建警报。消息是唯一的必填字段。下面是我正在使用的,但它不起作用

代码语言:javascript
复制
$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代码,它工作得很好。

代码语言:javascript
复制
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有关,我还没搞清楚到底是什么。如果我移除它们,它将不起作用。如果我把第一个转过来,它就不会工作了。我可以成功创建警报,但是我一直收到以下错误:

代码语言:javascript
复制
] : 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
EN

回答 1

Stack Overflow用户

发布于 2020-02-14 16:14:28

您需要将$body转换为JSON

代码语言:javascript
复制
$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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60217365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档