![]() |
VOOZH | about |
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.
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.
Install the module:
Install-Module FacebookAdsCmdlets
Connect:
$facebookads = Connect-FacebookAds -InitiateOAuth "$InitiateOAuth"
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'}
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")
Connect to Facebook Ads:
$conn= New-Object System.Data.CData.FacebookAds.FacebookAdsConnection("InitiateOAuth=GETANDREFRESH;")
$conn.Open()
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-FacebookAds -Connection $FacebookAds -Columns @('AccountId','Name') -Values @('MyAccountId', 'MyName') -Table AdAccounts -Id "MyId"
$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()
Add-FacebookAds -Connection $FacebookAds -Table AdAccounts -Columns @("AccountId", "Name") -Values @("MyAccountId", "MyName")
$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()
Remove-FacebookAds -Connection $FacebookAds -Table "AdAccounts" -Id "MyId"
$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()
CodeProject
Download a free trial of the Facebook Ads Data Provider to get started:
Download NowLearn more:
👁 Facebook Ads IconRapidly create and deploy powerful .NET applications that integrate with Facebook Ads.