dcvl
dcvl

Reputation: 595

Opsgenie powershell alert post not working

I have successfully made an alert post using Python, but cannot get my powershell alert creation to work. I just get a wall of HTML in my response and no alert creation. Message is the only required field. Here's what I'm using and it does not work

$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

Here is my python code I made and it works just fine.

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")

EDIT: So I've figured out it has something to do with my "responders" section. It has something to do with the [ ]...but I haven't been able to figure out what exactly. If I remove them, it wont work. If I turn the first one around, it won't work. I can get the alert to create successfully, however I keep getting the following error:

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

Upvotes: 1

Views: 1319

Answers (1)

Duc Nguyen
Duc Nguyen

Reputation: 36

You need to convert your $body to 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

Upvotes: 2

Related Questions