Using PowerShell to call an IFTTT Webhook

Sometimes, you want/need to have PowerShell do an action, but it’s hard to do through PowerShell, or it can’t be done at all. If the website If This Then That (IFTTT) can do it instead, you can still do it through PowerShell, with a little extra work. The way to do this will be to call a webhook on an IFTTT Applet and pass the information you need to send to the third-party service. A great example of this would be if you need to automate posting a new post to FaceBook. Posting to Facebook is shockingly hard as you have to write all the pieces to post to Facebook manually, and there’s no current way to post to Facebook via PowerShell. However, you can call an IFTTT Action and have that action do the actual posting to Facebook for you.

Calling a webhook from PowerShell is pretty straightforward; you just call the URL of the webhook by using the Invoke-Webmethod cmdlet in PowerShell. To send data to the IFTTT Action, you’ll need to do a couple of different things. The first thing you’ll notice about IFTTT webhooks is that they only accept three parameters named “value1”, “value2,” and “value3”, and these parameters are all lowercase (even though they show up capitalized in the UI).

As you can see in the sample code below, I create a variable called “$Body” and set this to the three values I want to pass to IFTTT. I then use ConvertTo-JSON to convert this array to a JSON document, which I then pass to the -Body parameter in Invoke-WebRequest. You also need to tell the webhook that the data coming through is JSON, which is done by setting the Content-Type header set to “application/json” (again, as you can see in the sample below).

$Body = @{"value1"=$ApiKey ;
"value2" = $PostUrl ;
"value3" = $PostText ; }
    
$Body = $Body | ConvertTo-JSON

Invoke-WebRequest -Uri $Uri -method 'POST' -Headers @{"Content-Type"= "application/json"} -Body $Body

That’s it. Once you have the Webhook being called, the value of the value3 variable (IFTTT calls these ingredients) will be posted to Facebook.

In a future post, I’ll show how to validate the ApiKey that I’m passing in as value1.

Denny

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trust DCAC with your data

Your data systems may be treading water today, but are they prepared for the next phase of your business growth?