JSON Web POST with PowerShell

An alternative to my previous method of using Python to make web requests for the notifications is to use PowerShell. The main advantage of using PowerShell is that it is included with Windows systems and therefore does not require the installation of any other packages. Additionally, it is far easier to trigger the script to run using the likes of Window’s task scheduler, allowing notifications to be triggered in response to events on the Windows system.

My Script

  • The first task is to once again define the URL from which the requests will be made
    • This can be found in the Flow interface under the “When HTTP request is received section” and will be unique for each flow
    • In PowerShell, variables are assigned and referenced with the “$” sign in front of them
    • $URL = "https://prod-02.australiasoutheast.logic.azure.com:443/workflows/a7.....
  • Next I define a “Parameters” object to contain the parameters I want to include in the notification
    • Note: The syntax used here is to create a PowerShell object, and not JSON. It will need to be converted to JSON in the next step
    • $Parameters = @{
       "Source"= "Computer"; 
       "Message"= "42";
    • Again I am using “Computer” as the source and “42” as the message, however this will be where you can add your own custom information.
  • Next we get PowerShell to take the parameters object we just created and turn it into a JSON object
    • $JSON = $Parameters | ConvertTo-Json
    • The result is stored in the “JSON” variable
  • Finally, we perform the actual web request
    • Invoke-WebRequest -Uri $URL -Method POST -Body $JSON -ContentType 'application/json'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s