-
-
Save darrenjrobinson/74c71227c86c5745ed3b76d0846c59b2 to your computer and use it in GitHub Desktop.
Send SMS/Text Message via Twillo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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