Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Twilio Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Connecting to Twilio
You can authenticate to Twilio using either an Auth Token or an API key.Auth Token
You can authenticate to Twilio using an Auth Token and an Account SID.
Open the Twilio Console Dashboard, navigate to the Account Info section, and set these connection properties:
- AccountSID: the value of the Account SID field.
- AuthToken: the value of the Auth Token field. Click Show to unhide it.
API Key
To authenticate to Twilio using an API key:
- Open the Twilio Console Dashboard and click Account Info > API Keys > Go to API Keys. The API keys & tokens page opens.
- Click Create API key. The Create new API key menu opens.
- Set Friendly name to a name you want to use to refer to the token.
- Set Region to the region in which you want the API key to apply.
- Set Key type to your desired key type. The options include:
- Standard - Grants access to all Twilio API features except for managing API Keys, Account Configuration, and Subaccounts.
- Main - Grants access to the same features as the Standard key type, with the added ability to manage API Keys, Account Configuration, and Subaccounts.
- Restricted - Allows granular access to a subset of the Twilio API features that the standard APIKey grants. If you select this option, you must manually specify the permissions that the token grants.
- Click Create. This opens the Copy secret key page.
- Note the values of the SID and Secret fields. The latter is only shown once, so make sure you copy it before leaving this page.
- Set the following connection properties:
- AuthScheme: APIKey
- AccountSID: the value of the Account SID field in the Account Info section of the Twilio Console Dashboard.
- APIKeySID: the SID of the API key you generated earlier, which you noted in step 4.
- APIKeySecret: the API key secret of the API key you generated earlier, which you noted in step 4.
Creating a Connection Object
You can then use the Connect-Twilio cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Twilio -AccountSid 'MyAccountSid' -AuthToken 'MyAuthToken'
Retrieving Data
The Select-Twilio cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Twilio -Connection $conn -Table "Applications" -Columns @("Sid, Name") -Where "Sid='AP5ddf534702934bd3a446d293e8cdeb1f'"
The Invoke-Twilio cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-Twilio -Connection $conn -Table Applications -Where "Sid = 'AP5ddf534702934bd3a446d293e8cdeb1f'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myApplicationsData.csv -NoTypeInformation
You will notice that we piped the results from Select-Twilio into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-Twilio -AccountSid 'MyAccountSid' -AuthToken 'MyAuthToken'
PS C:\> $row = Select-Twilio -Connection $conn -Table "Applications" -Columns (Sid, Name) -Where "Sid = 'AP5ddf534702934bd3a446d293e8cdeb1f'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
"Connection": {
},
"Table": "Applications",
"Columns": [
],
"Sid": "MySid",
"Name": "MyName"
}
Deleting Data
The following line deletes any records that match the criteria:
Select-Twilio -Connection $conn -Table Applications -Where "Sid = 'AP5ddf534702934bd3a446d293e8cdeb1f'" | Remove-Twilio
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Twilio, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyApplicationsUpdates.csv | %{
$record = Select-Twilio -Connection $conn -Table Applications -Where ("Sid = `'"+$_.Sid+"`'")
if($record){
Update-Twilio -Connection $conn -Table Applications -Columns @("Sid","Name") -Values @($_.Sid, $_.Name) -Where "Sid = `'$_.Sid`'"
}else{
Add-Twilio -Connection $conn -Table Applications -Columns @("Sid","Name") -Values @($_.Sid, $_.Name)
}
}