Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Created March 31, 2019 00:23
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 darrenjrobinson/74c71227c86c5745ed3b76d0846c59b2 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/74c71227c86c5745ed3b76d0846c59b2 to your computer and use it in GitHub Desktop.
Send SMS/Text Message via Twillo
function Send-TextNotification {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
$to,
[Parameter(Mandatory = $true)]
$from,
[Parameter(Mandatory = $true)]
$accountSID,
[Parameter(Mandatory = $true)]
$auth,
[Parameter(Mandatory = $true)]
$msg
)
try {
# Send Message
$body = @{Body = $msg; To = $to; From = $from;}
$result = Invoke-RestMethod -Method Post -Uri "https://api.twilio.com/2010-04-01/Accounts/$($accountSID)/Messages.json" -Body $body -Credential $auth -UseBasicParsing
if ($result.sid) {
Write-host -ForegroundColor green "Message to: $($result.to) is $($result.status)"
}
} catch {
Write-Error "Failed to send Notification. ErrorCode: $($_.Exception.Response.StatusCode.value__) ErrorDetails: $($_.Exception.Response.StatusDetails) ErrorDescription: $($_.Exception.Response.StatusDescription)"
}
}
# TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Twillo Account SID
$accountSID = "yourTwilloAccountSID"
# Twillo Account Token
$accountToken = "yourTwilloAccountToken"
# Basic Auth
$p = $accountToken | ConvertTo-SecureString -asPlainText -Force
$auth = New-Object System.Management.Automation.PSCredential($accountSID, $p)
# To
[string]$to = '+DestinationMobileNumber'
# From
[string]$from = '+TwilloAccountMobileNumber'
# Message
[string]$msg = "Everything is on fire. Investigate and Remediate immediately!!"
Send-TextNotification -from $from -to $to -auth $auth -msg $msg -accountSID $accountSID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment