Ready to get started?

Download a free trial of the Facebook Ads Data Provider to get started:

 Download Now

Learn more:

Facebook Ads Icon Facebook Ads ADO.NET Provider

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

Automate Facebook Ads Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Facebook Ads data from PowerShell? This article demonstrates how to utilize the Facebook Ads Cmdlets for tasks like connecting to Facebook Ads data, automating operations, downloading data, and more.

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

PowerShell Cmdlets or ADO.NET Provider?

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

Once you have acquired the necessary connection properties, accessing Facebook Ads data in PowerShell can be enabled in three steps.

Most tables require user authentication as well as application authentication. Facebook uses the OAuth authentication standard. To authenticate to Facebook, you can use the embedded OAuthClientId, OAuthClientSecret, and CallbackURL or you can obtain your own by registering an app with Facebook.

See the Getting Started chapter of the help documentation for a guide to using OAuth.

PowerShell

  1. Install the module:

    Install-Module FacebookAdsCmdlets
  2. Connect:

    $facebookads = Connect-FacebookAds
  3. Search for and retrieve data:

    $name = "Acct Name" $adaccounts = Select-FacebookAds -Connection $facebookads -Table "AdAccounts" -Where "Name = `'$Name`'" $adaccounts

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

    $adaccounts = Invoke-FacebookAds -Connection $facebookads -Query 'SELECT * FROM AdAccounts WHERE Name = @Name' -Params @{'@Name'='Acct Name'}

ADO.NET

  1. Load the provider's assembly:

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

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

    $sql="SELECT AccountId, Name from AdAccounts" $da= New-Object System.Data.CData.FacebookAds.FacebookAdsDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.accountid $_.name }

Update Facebook Ads Data

PowerShell

Update-FacebookAds -Connection $FacebookAds -Columns @('AccountId','Name') -Values @('MyAccountId', 'MyName') -Table AdAccounts -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.FacebookAds.FacebookAdsCommand("UPDATE AdAccounts SET Name='Acct Name' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.FacebookAds.FacebookAdsParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Facebook Ads Data

PowerShell

Add-FacebookAds -Connection $FacebookAds -Table AdAccounts -Columns @("AccountId", "Name") -Values @("MyAccountId", "MyName")

ADO.NET

$cmd = New-Object System.Data.CData.FacebookAds.FacebookAdsCommand("INSERT INTO AdAccounts (Name) VALUES (@myName)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.FacebookAds.FacebookAdsParameter("@myName","Acct Name"))) $cmd.ExecuteNonQuery()

Delete Facebook Ads Data

PowerShell

Remove-FacebookAds -Connection $FacebookAds -Table "AdAccounts" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.FacebookAds.FacebookAdsCommand("DELETE FROM AdAccounts WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.FacebookAds.FacebookAdsParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()