Skip to content

Instantly share code, notes, and snippets.

@deangrant
Created January 17, 2016 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deangrant/1bcbdf99f8f730fa95b3 to your computer and use it in GitHub Desktop.
Save deangrant/1bcbdf99f8f730fa95b3 to your computer and use it in GitHub Desktop.
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