Оповещение opsgenie powershell не работает

Я успешно сделал сообщение с предупреждением, используя 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

1 ответ

Вам нужно преобразовать $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
Другие вопросы по тегам