Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.