Create an Opsgenie alert

I was wondering if any of you had a working powershell script for creating an alert in ops genie. Specifically just the part that formats and creates the alert. Not any logic that triggers it.

I came across a post of someone else having a similar issue. I tried what the poster originally said they tried, and then one of the suggestions. I get the same problem that the OP had. Post is here https://stackoverflow.com/questions/60217365/opsgenie-powershell-alert-post-not-working

I’ve tried the 2 ideas posted using my own api key and teamname, but no luck. I get the same errors as the OP

The first code returns the below error. It makes sense…but I can’t modify the [ ] any way to make this work.

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

The second code returns this error:

[pre]
+ $request + ~ Missing ‘=’ operator after key in hash literal. At \ae-file\Tech\path\powershell scripts.not working\OpsGenieAlert.ps1:45 char:9 + $request + ~ The hash literal was incomplete.
[/pre]


`<span class="pln">$api </span><span class="pun">=</span> <span class="str">"XXX"</span><span class="pln">
$URI </span><span class="pun">=</span> <span class="str">"https://api.opsgenie.com/v2/alerts"</span><span class="pln">
$head </span><span class="pun">=</span> <span class="pun">@{</span><span class="str">"Authorization"</span> <span class="pun">=</span> <span class="str">"GenieKey $api"</span><span class="pun">}</span><span class="pln">
$body </span><span class="pun">=</span> <span class="pun">@{</span><span class="pln">
            message </span><span class="pun">=</span> <span class="str">"testing"</span><span class="pun">;</span><span class="pln">
            responders </span><span class="pun">=</span>
                <span class="pun">]@{</span><span class="pln">
                    name </span><span class="pun">=</span> <span class="str">"TEAMNAMEHERE"</span><span class="pun">;</span><span class="pln">
                    type </span><span class="pun">=</span> <span class="str">"team"</span>
                <span class="pun">}]</span></pre>
<span class="pun">}</span> <span class="pun">|</span> <span class="typ">ConvertTo</span><span class="pun">-</span><span class="typ">Json</span>

$request <span class="pun">=</span> <span class="typ">Invoke</span><span class="pun">-</span><span class="typ">RestMethod</span> <span class="pun">-</span><span class="typ">Uri</span><span class="pln"> $URI </span><span class="pun">-</span><span class="typ">Method</span> <span class="typ">Post</span> <span class="pun">-</span><span class="typ">Headers</span><span class="pln"> $head </span><span class="pun">-</span><span class="typ">ContentType</span> <span class="str">"application/json"</span> <span class="pun">-</span><span class="typ">Body</span><span class="pln"> $body
$request</span>`
`<span class="pln">$api </span><span class="pun">=</span> <span class="str">"XXX"</span><span class="pln">
$URI </span><span class="pun">=</span> <span class="str">"https://api.opsgenie.com/v2/alerts"</span>
<span class="com"># Declare an empty array </span><span class="pln">
$responders </span><span class="pun">=</span> <span class="pun">@()</span></pre>
<span class="com"># Add a new item to the array</span><span class="pln">
$responders </span><span class="pun">+=</span> <span class="pun">@{</span><span class="pln">
name </span><span class="pun">=</span> <span class="str">"TEAMNAMEHERE1"</span><span class="pln">
type </span><span class="pun">=</span> <span class="str">"team1"</span>
<span class="pun">}</span><span class="pln">
$responders </span><span class="pun">+=</span> <span class="pun">@{</span><span class="pln">
name </span><span class="pun">=</span> <span class="str">"TEAMNAMEHERE2"</span><span class="pln">
type </span><span class="pun">=</span> <span class="str">"team2"</span>
<span class="pun">}</span>

$body <span class="pun">=</span> <span class="pun">@{</span><span class="pln">
message </span><span class="pun">=</span> <span class="str">"testing"</span><span class="pln">
responders </span><span class="pun">=</span><span class="pln"> $responders
</span><span class="pun">}</span> <span class="pun">|</span> <span class="typ">ConvertTo</span><span class="pun">-</span><span class="typ">Json</span>

$invokeRestMethodParams <span class="pun">=</span> <span class="pun">@{</span>
<span class="str">'Headers'</span> <span class="pun">=</span> <span class="pun">@{</span>
<span class="str">"Authorization"</span> <span class="pun">=</span> <span class="str">"GenieKey $api"</span>
<span class="pun">}</span>
<span class="str">'Uri'</span> <span class="pun">=</span><span class="pln"> $URI
</span><span class="str">'ContentType'</span> <span class="pun">=</span> <span class="str">'application/json'</span>
<span class="str">'Body'</span> <span class="pun">=</span><span class="pln"> $body
</span><span class="str">'Method'</span> <span class="pun">=</span> <span class="str">'Post'</span>
<span class="pun">}</span>

$request <span class="pun">=</span> <span class="typ">Invoke</span><span class="pun">-</span><span class="typ">RestMethod</span> <span class="lit">@invokeRestMethodParams</span>`