Tuesday, August 14, 2012

FaceBook module for Powershell.

Hi,
In the world of Powershell everything Blue. Powershell community is fill with lots of amazing thinkers, developers and contributors. There are lots of nice peoples who spend day and night on writing a script and then give them to the community to use it freely. There are lots of other peoples too who are very active on Powershell Forms , you just need to as a question and i am sure that you will hear from any of them soon.
One the module i like in powershell is “Facebook Powershell Module” which are created by “Jon Newman”.
The first question came in mind  why to use this module? and the Answer is, It is Cool, it is geeky and it is more then a module.
lets think about a scenario, for example.
  1. You can write a simple script which contain a list of all of your friends birthday date and that script run everyday,and using Facebook module you can wish them on their Facebook account and you never miss anyone birthday.
  2. If you are running a group , page and love to share “Quote of the Day”, then create a list of 365 quotes and post them automatically using Facebook module.
  3. If your wife /girlfriend is angry with you and complaining that you don't give her time, to impress her  just collect few love quotes and post it on her wall in every one hour ;o)
Powershell is all about automation, lets automate the Facebook. ;o)
I am using a Facebook module to post a message on my Facebook wall when any of my server fails.  Logically we created a new Facebook account for this and we added that Facebook account as friend {all IT admins}, and there are few scripts which runs every night and in case they found anything worn they post a message on their walls and in the morning we all can see it.
Lets Start.
You can download the module from this link “http://facebookpsmodule.codeplex.com/”  and the current version of module is “FacebookPSModule Alpha 0.6.3” .
Download the module and save it on your desktop.
13-08-2012 18-16-12
this is a zip file extract it and run the “FacebookPsModule Aplha 0.6.3.msi”
13-08-2012 18-17-20
A welcome window will be open “Click on Next”
14-08-2012 11-49-54
The default folder to save the module file in “WindowsPowershell” located in My Document or Documents, Leave it as it is and click on “Next”
14-08-2012 11-50-04
Installation starts.
13-08-2012 18-17-34
Installation Complete , click on “Close”
13-08-2012 18-18-18
You can verify that Facebook Module has copied to the “Documents\WindowsPowershell\Module\Facebook “
13-08-2012 18-18-56
Now lets open powershell and import the module.
Import-Module facebook
13-08-2012 18-19-25
and let’s see which comdlets it has.
Get-Command -Module Facebook
Wow!!!!!
13-08-2012 18-20-06
Let run the Cmdlet New-Connection to connect to the Facebook …
New-FBConnection

errr, error ….
13-08-2012 18-25-52

The New-FBConnection cmdlet runs only in STA mode, you have to do this one .

Lets run Powershell in STA mode.

Open Run and type “Powershell.exe –STA” and click on OK.

13-08-2012 18-25-59

when powershell windows open. Import Facebook Module once again and run New-FBConnection

14-08-2012 12-08-50

when you run the New-FBConnection cmdlet a Facebook login windows will open.

13-08-2012 18-26-11
Now provide your Email ID and password and make sure that you checked on “Keep me logged in” and then click on “Log In”

13-08-2012 18-26-35
and now a Facebook app page will open , click on “Log in With Facebook”

13-08-2012 18-27-31
Now the module is ask for Permission and just click on “Allow”.

13-08-2012 18-27-45

after you clicked on allow, all is done and you will see this . that means connection with your Facebook account is successful.
13-08-2012 18-28-03
we have to enter these Login Details once only { i tested this with multiple reboots of my machine i am able to access the Facebook account without enter user credentials } , as per Facebook Module documentation,,  “Once you create an “access token”, it will by default be cached in %LOCALAPPDATA%\FacebookPowerShellModule_CachedToken.txt, so you will not have to re-enter it as long as you use your current user account.”
Okeis. now lets test it. using New-FBFeed cmdlet.
New-FBFeed -Message "this is a test message"
13-08-2012 18-31-13
and the result is.
Bingo Smile 
13-08-2012 18-31-05
Now next step it to automate it.

i wrote a simple script to test it .

This script Import Facebook Module first, and it will test the connectivity of all the servers those are defined in $servers and if the connection to the server is successful it just wrote and message in the console and it the connection is unsuccessful then it wrote on Facebook Wall. 

Import-Module Facebook
New-FBConnection 
$servers =  "Dc-local","Not-Exists"

foreach ( $server in $servers ) {
if (test-Connection -ComputerName $Server -Count 2 -Quiet ) { 
write-Host "$Server is alive and Pinging " -ForegroundColor Green
} else
{
New-FBFeed -Message "$Server seems dead not pinging"
}
} 

When i run the script the output was below. My Dc-Local is live and Not-Exists is not so the below output is showing me the Facebook FeedID

13-08-2012 18-54-33
Now lets check the Facebook wall.
Yo!! Our server was not reachable and we sucessfully posted that message to the Wall.. Smile
13-08-2012 18-54-53
We successfully automated the Facebook using powershell.
Thanks
Aman Dhally
join aman on facebook Join aman on Linkedin follow aman on Twitter

1 comment:

  1. When i signin it dont ask for permissions please help

    ReplyDelete

Note: Only a member of this blog may post a comment.