Automate Facebook Ads Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Facebook Ads ADO.NET Provider

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



Are you looking for a quick and easy way to access Facebook Ads data from PowerShell? We show how to use the Cmdlets for Facebook Ads and the CData ADO.NET Provider for Facebook Ads to connect to Facebook Ads data and synchronize, automate, download, 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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Facebook Ads API, 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.

After obtaining the needed connection properties, accessing Facebook Ads data in PowerShell consists of three basic 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 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 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 System.Data.CData.FacebookAds.FacebookAdsParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()