Automate Facebook with a Free PowerShell Module

Automate Facebook with a Free PowerShell Module

  • Comments 10
  • Likes

Summary: Guest Blogger Jon Newman shows how to automate Facebook with a free Windows PowerShell module.

 

Microsoft Scripting Guy Ed Wilson here. It’s Guest Blogger Week! Today, we have Jon Newman from Microsoft who is going to talk to us about his new Windows PowerShell Facebook module. Take it away, Jon.

 

Thanks to Ed Wilson for giving me space on the Hey, Scripting Guy! Blog!

My name is Jon Newman, and I’m an old hand at Windows PowerShell. I was on the Windows PowerShell 1.0 team, with Bruce and Jim and all the rest. My primary responsibility was for pipeline execution and error handling. Someday I should write a blog entry on FullyQualifiedErrorId, but that is for another time. I started at Microsoft in September 1989, and I have been writing management UI for Windows Server for 20 years. The Facebook project doesn’t actually have anything to do with Microsoft though.

A few years ago, I joined the board of Hillel at the University of Washington. Like many other businesses and organizations, they use Facebook extensively to interact with program participants and backers. I was struck, however, by the lack of the sort of automation commonly used to manage other mission-critical IT. I looked around and did not find anything usable in this space, although there are a couple other mostly obsolete Windows PowerShell integration projects. So I decided to jump in. This is my first open source project and I am sure I have a lot to learn.

First, my Facebook IT Manifesto motivating this project: Facebook is today mission-critical to many organizations’ customer relationships, as significant as are email and web presence. Therefore, such organizations should manage their Facebook presence with the same IT rigor as other mission-critical technologies such as email and websites. I assert in particular that:

  1. An organization’s Facebook presence must be under ultimate administrative control of the organization itself, and not any particular employees. Customer relationships within Facebook must carry forward as employees leave, enter, and change roles within the organization.
  2. An organization must be able to manage its customer relationships within Facebook members in conjunction with other means of contacting the same customers.
  3. An organization must be able to automate routine management of their Facebook presence.
  4. An organization must document its formal Facebook processes with the same rigor as its formal processes for other elements of IT.
  5. Where social networking technologies other than Facebook have similar importance to an organization, the same applies to them as well.

As a partial answer to need #3, I offer the Facebook PowerShell Module. This technology (still in alpha at this writing) enables organizations to automate and integrate Facebook using Windows PowerShell.

Let us dive right into some examples, and talk theory later:

PS C:\Windows\system32> get-fbobjectdata

email        : jonn_msft_testuser@hotmail.com

id           : 100002097205662

last_name    : NewmanTest

name         : JonTest NewmanTest

first_name   : JonTest

languages    : {@{id=106059522759137; name=English}, @{id=105673612800327; name

               =German}}

link         : http://www.facebook.com/profile.php?id=100002097205662

locale       : en_US

timezone     : -7

updated_time : 2011-05-29T15:04:10+0000

birthday     : 06/30/1964

gender       : male

OK, so I’ve skipped a step. You first need to run New-FBConnection from the Windows PowerShell ISE, which grants permission to access your Facebook account. If you forget, the module will talk you through it, but if you need the details, go to this webpage.

If you do not specify which Facebook object you want to know about, the default is you. This is my test account and the default list of fields.

PS C:\Windows\system32> get-fbobjectdata -fields languages

Id                                 languages

100002097205662          {@{id=106059522759137; name=English}...

If you want fields other than the default fields, see Facebook’s documentation. In this case, we requested the nondefault information “languages.”

PS C:\Windows\system32> get-fbfriends

Id                                 name

<number>                    Matthew <name>

<number>                    Mikael <name>

<number>                    Naomi <name>

<number>                    Jon <name>

Facebook data is basically a gigantic relational database. You are an object, you can have lots of fields, and the fields can be references to other objects. In this case, you are asking for the list of objects in your Friends list.

PS C:\Windows\system32> get-fbfriends -fields gender | group gender

Count                Name                           Group

3                      male                             {@{id=604652494; gender=male}, @{id=67396290

1                      female                          {@{id=1022311059; gender=female}}

Because this is Windows PowerShell, you get all the goodness of its standard commands such as Where, Select, and Group.

function Backup-FBFriends

{

    $timeString = $(get-date) $dateTime.ToString("yyyy-MM-dd-HH-mm-ss")

    $filename = "c:\temp\friends.$timeString.csv"

    write-verbose "Backing up friends list to $filename"

    $friends = Get-FBFriends

    $userdata = $friends | Get-FBObjectData

    $userdata | Export-Csv $filename

}

The preceding code is a more sophisticated example (part of FacebookExamples.ps1 that comes with FacebookPSModule). This script automatically backs up your Friends list to a CSV file in c:\temp. It’s the sort of thing you could run in a scheduled task, so you can monitor changes over time. 

The final example is the Write-EventAttendeeCsv script (also in FacebookExamples.ps1). I have this running in a scheduled task at the client site. It’s a little too long to reproduce here, but it’s a good example of what you can accomplish with FacebookPSModule. The script does the following:

  1. Gets the list of all events associated with a particular group.
  2. Filters to those events in the upcoming week.
  3. Extracts the RSVP list for those events.
  4. Writes an attendee list file for each event to the temp directory.

This way, program staff can print out an attendee list before the event, based on RSVPs for the Facebook event. 

So, is FacebookPSModule right for you? In theory, you could use this to automate anything you can do using the Facebook website. The reality isn’t quite so simple.

First, there are a few things that Facebook doesn’t let you do:

  • You can’t read the list of your friends’ friends. I think this is on purpose—Facebook just decided that allowing Facebook applications to do this is this too much of a privacy risk. As far as Facebook is concerned, FacebookPSModule is a Facebook application, even when you’re just accessing your own data.
  • You can’t write status messages that link pages such as @Microsoft. I think this is just an oversight in the API.
  • You can link photos, but you can’t link directly to other users’ photos in Facebook’s CDN (for example, photos in other users’ albums). This is permitted from the website but not from the API.

I will probably discover more issues as I fill out and explore the module’s capabilities.

More importantly, users should be aware that the Facebook API churns fast, especially the authentication stack. Their interfaces from two years ago are already deprecated and largely broken. You’ll have to keep an eye on your automation to make sure it is working properly.

FacebookPSModule stores your Facebook access token by default (encrypted using DPAPI via ConvertFrom-SecureString) in your user profile, which is why subsequent commands including scheduled tasks can read it. You should think about security for your user profile, because anyone with access to the access token can perform Facebook tasks against your account. In the worst-case scenario:

  1. On Facebook.com, click Account, and then click Privacy Settings.
  2. Under Apps and Websites, click Edit your settings. Under Apps you use, click Edit Settings.
  3. For Test Application jonn_msft (or whichever AppId you used), click Edit. On the next page, click Remove app to revoke permissions for the application, including its access token.

Finally, most important of all, FacebookPSModule is (as of August 15, 2011) only in alpha. There are plenty of known issues, including issues with large data sets. I will probably change the cmdlet names and signatures as I learn more about this space.

FacebookPSModule is really a thin layer over the Facebook C# SDK, which is itself a thin layer over the Facebook Graph API. When you run New-FBConnection, you are requesting an OAuth 2.0 access token for an application I registered. The application doesn’t really exist, and you can use another application ID if you like; you just have to specify an application ID to get the access token.

I could share some war stories about developing this, in particular about supporting .NET Framework 4.0 and converting .NET Dynamic Objects to Windows PowerShell. If there is enough interest, I will write a follow-up blog entry.

What I need from you are more real-world scenarios. Tell me what you want to do, and I can make sure FacebookPSModule does it well. If you want to help with the development, please do! http://facebookpsmodule.codeplex.com/discussions is the place to talk.

Happy Facebooking!

 

Thank you, Jon, for writing a really cool Facebook module, and for taking the time to share your experience with us.

Join us tomorrow for day 2 of Guest Blogger Week when Trevor Sullivan will be our guest.

 

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Jon,

    Liking what you have done in the Facebook Module and testing it right now. Only thing I noticed when started to work with the module is that you used plural for some of the nouns used in your commands.

    Example: get-fbfriends instead of get-fbfriend

    get-command -module facebook | where {$_.Name -like "*s"}

    /Stefan

  • Stefan, thank you for your comment. I would like to keep discussions of the module on Codeplex, so I have moved this to facebookpsmodule.codeplex.com/.../269199. I hope you'll be in touch there!

  • Como baixar este conteúdo de uma maneira mais simples !!!!

  • Thanks Ed!

    A helpfull idea.

  • Now we wait to see who will be the first out with a Twitter module.

  • @ArPosh I also would LOVE to see a good Twitter module. There is an API, and I have seen some scripts ... so it is "doable."

  • Hi Jon,

    thank you very much for this insight into your PS facebook module.

    It would be great to hear more of your war stories during the development process :-)

    Taming .Net 4.0 dynamics and converting them to be consumed by powershell might be very interesting stuff ...

    Klaus.

  • Klaus, just as you note, managing .NET 4.0 dynamic objects was the chewiest work here. The code comments contain some discussion of this. I would like to keep discussion on FacebookPSModule in one place, so please follow up on facebookpsmodule.codeplex.com/discussions. Thanks, Jon

  • Rich/Ed, AFAIK you're exactly right about Twitter. There is no reason why someone couldn't do it. I'd like to hear a little more about your specific business case before I tried to tackle this. And, of course, please follow up on facebookpsmodule.codeplex.com/discussions so that we can keep the discussion in one place. Thanks, Jon

  • Does this module still work with facebook? When I perform a New-FBConnection, a blank box pops up.