Skip to content

Instantly share code, notes, and snippets.

@pjperez
Created December 7, 2016 16:40
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 pjperez/999967bbcd321d69dad8567c6200076d to your computer and use it in GitHub Desktop.
Save pjperez/999967bbcd321d69dad8567c6200076d to your computer and use it in GitHub Desktop.
Write-Output "PowerShell Timer trigger function executed at:$(get-date)";
$uri = "https://bing.com"
$poorsodnumber = "+44........"
if ((Invoke-WebRequest -Uri $uri -UseBasicParsing).StatusCode -eq 200) { write-output "$uri is UP" } else
{
Write-Output "$uri is DOWN. Sending SMS."
# Pull in Twilio account info, previously set as environment variables
$sid = $env:TWILIO_ACCOUNT_SID
$token = $env:TWILIO_AUTH_TOKEN
$number = $env:TWILIO_NUMBER
# Twilio API endpoint and POST params
$url = "https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json"
$params = @{ To = "$poorsodnumer"; From = $number; Body = "$uri is down!! OMG!!" }
# Create a credential object for HTTP basic auth
$p = $token | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($sid, $p)
# Make API request, selecting JSON properties from response
Invoke-WebRequest $url -Method Post -Credential $credential -Body $params -UseBasicParsing |
ConvertFrom-Json | Select sid, body
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment