Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Process
{
Try
{
# Sends HTTPS request to the Slack Web API service.
$WebRequest = Invoke-WebRequest -Uri $Uri -Body $PostMessage
# Conditional logic to generate custom error message if JSON object response contains a top-level error property.
If ($WebRequest.Content -like '*"ok":false*')
{
# Terminates with error if the response contains an error property, parses the error code and stops processing of the command.
Throw ($WebRequest.Content -split '"error":"')[1] -replace '"}',''
} # If
} # Try
Catch
{
# Terminates with error and stops processing of the command.
Throw ("Unable to send request to the web service with the following exception: " + $Error[0].Exception.Message )
} # Catch
} # Process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment