Automate Twitter Ads Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Twitter Ads ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Twitter Ads.



Are you looking for a quick and easy way to access Twitter Ads data from PowerShell? We show how to use the Cmdlets for Twitter Ads and the CData ADO.NET Provider for Twitter Ads to connect to Twitter Ads data and synchronize, automate, download, and more.

The CData Cmdlets for Twitter Ads are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time access to Twitter Ads.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Twitter Ads API, but also an SQL interface; this tutorial shows how to use both to retrieve Twitter Ads data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Twitter Ads. To access Twitter Ads data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Twitter Ads.

After obtaining the needed connection properties, accessing Twitter Ads data in PowerShell consists of three basic steps.

All tables require authentication. You must use OAuth to authenticate with Twitter. OAuth requires the authenticating user to interact with Twitter using the browser. For more information, refer to the OAuth section in the Help documentation.

PowerShell

  1. Install the module:

    Install-Module TwitterAdsCmdlets
  2. Connect:

    $twitterads = Connect-TwitterAds
  3. Search for and retrieve data:

    $entity = "ORGANIC_TWEET" $adstats = Select-TwitterAds -Connection $twitterads -Table "AdStats" -Where "Entity = `'$Entity`'" $adstats

    You can also use the Invoke-TwitterAds cmdlet to execute SQL commands:

    $adstats = Invoke-TwitterAds -Connection $twitterads -Query 'SELECT * FROM AdStats WHERE Entity = @Entity' -Params @{'@Entity'='ORGANIC_TWEET'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Twitter Ads\lib\System.Data.CData.TwitterAds.dll")
  2. Connect to Twitter Ads:

    $conn= New-Object System.Data.CData.TwitterAds.TwitterAdsConnection("InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the TwitterAdsDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT EntityId, Entity from AdStats" $da= New-Object System.Data.CData.TwitterAds.TwitterAdsDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.entityid $_.entity }