Twillio - calls or SMS with PowerShell

This article is about PowerShell and the tasks I use it for.

I had some trouble finding examples of placing calls with Twillio. Here is what worked for me.


Place a voice call with PowerShell and Twilio:

#Run these with your SID and Token once to set the variables

#[Environment]::SetEnvironmentVariable('TWILIO_ACCOUNT_SID', 'AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX','User')

#[Environment]::SetEnvironmentVariable('TWILIO_AUTH_TOKEN','XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX','User')


# Pull in Twilio account info, previously set as environment variables

$sid =[Environment]::GetEnvironmentVariable('TWILIO_ACCOUNT_SID','User')

$token =[Environment]::GetEnvironmentVariable('TWILIO_AUTH_TOKEN','User')

$number = "+15555555555"


$targetnumber=@() #Using this method to handle additions to an array does not scale well, but it is easy to read

#$targetnumber += "+55555555555" #John

$targetnumber += "+15555555555" #Jane

foreach ($person in $targetnumber)

{

$url = "https://api.twilio.com/2010-04-01/Accounts/$sid/Calls.json"

$params = @{ To = "$person"; From = $number; Twiml = '<?xml version="1.0" encoding="UTF-8"?><Response><Say voice="Polly.Joey">Hello. This is an alert. Something may have gone wrong. .</Say></Response>' }

$p = $token | ConvertTo-SecureString -asPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential($sid, $p)

Invoke-WebRequest $url -Method Post -Credential $credential -Body $params -UseBasicParsing | ConvertFrom-Json | Select sid, body

}