Automate Facebook Integration Tasks from PowerShell
The CData ADO.NET Provider for Facebook is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Facebook.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Facebook; this tutorial shows how to use the Provider to create, retrieve, update, and delete Facebook data.
Once you have acquired the necessary connection properties, accessing Facebook 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.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Facebook\lib\System.Data.CData.Facebook.dll") -
Connect to Facebook:
$conn= New-Object System.Data.CData.Facebook.FacebookConnection("InitiateOAuth=GETANDREFRESH;") $conn.Open() -
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
$cmd = New-Object System.Data.CData.Facebook.FacebookCommand("UPDATE Posts SET Target='thesimpsons' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Facebook.FacebookParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Facebook Data
$cmd = New-Object System.Data.CData.Facebook.FacebookCommand("INSERT INTO Posts (Target) VALUES (@myTarget)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Facebook.FacebookParameter("@myTarget","thesimpsons")))
$cmd.ExecuteNonQuery()
Delete Facebook Data
$cmd = New-Object System.Data.CData.Facebook.FacebookCommand("DELETE FROM Posts WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Facebook.FacebookParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject