Automate Facebook Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Facebook ADO.NET Provider

Connect any Web, Desktop, or Mobile .NET application with Facebook data including Events, Groups, Pages, Places, Posts and more!



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

The CData Cmdlets for Facebook 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.

Cmdlets or ADO.NET?

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

After obtaining the needed connection properties, accessing Facebook 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 FacebookCmdlets
  2. Connect:

    $facebook = Connect-FB
  3. Search for and retrieve data:

    $target = "thesimpsons" $posts = Select-FB -Connection $facebook -Table "Posts" -Where "Target = `'$Target`'" $posts

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

    $posts = Invoke-FB -Connection $facebook -Query 'SELECT * FROM Posts WHERE Target = @Target' -Params @{'@Target'='thesimpsons'}

ADO.NET

  1. Load the provider's assembly:

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

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

    $sql="SELECT FromName, LikesCount from Posts" $da= New-Object System.Data.CData.Facebook.FacebookDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.fromname $_.likescount }

Update Facebook Data

PowerShell

Update-FB -Connection $Facebook -Columns @('FromName','LikesCount') -Values @('MyFromName', 'MyLikesCount') -Table Posts -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Facebook.FacebookCommand("UPDATE Posts SET Target='thesimpsons' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Facebook.FacebookParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Facebook Data

PowerShell

Add-FB -Connection $Facebook -Table Posts -Columns @("FromName", "LikesCount") -Values @("MyFromName", "MyLikesCount")

ADO.NET

$cmd = New-Object System.Data.CData.Facebook.FacebookCommand("INSERT INTO Posts (Target) VALUES (@myTarget)", $conn) $cmd.Parameters.Add(new System.Data.CData.Facebook.FacebookParameter("@myTarget","thesimpsons")) $cmd.ExecuteNonQuery()

Delete Facebook Data

PowerShell

Remove-FB -Connection $Facebook -Table "Posts" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Facebook.FacebookCommand("DELETE FROM Posts WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Facebook.FacebookParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()