Andy Oakley

Powershell to Twitter

without comments

I have too much time on my hands this morning. The following script makes it easy to get and set Twitter status. I’ve seen similar ones elsewhere but they all seemed to have external dependencies, this one does not.

#twitter.ps1:
$script:username = "foo"
$script:password = "bar"
function get-twitter
{
 $wc = new-object System.Net.Webclient
 $wc.Credentials = new-object System.Net.NetworkCredential $script:username,$script:password 
 $rest = $wc.DownloadString("<a href="http://twitter.com/statuses/friends_timeline.xml">http://twitter.com/statuses/friends_timeline.xml</a>")
 $xml = [xml]$rest
 $xml.statuses.status
}
function set-twitter
{
 param($status)
 $wc = new-object System.Net.Webclient
 $wc.Credentials = new-object System.Net.NetworkCredential $script:username,$script:password 
 [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
 $encodedstatus = [System.Web.HttpUtility]::UrlEncode($status)
 $postdata = "status=$encodedstatus"
 $postbytes = [System.Text.Encoding]::ASCII.GetBytes($postdata)
 $wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
 [System.Text.Encoding]::ASCII.GetString($wc.UploadData("<a href="http://twitter.com/statuses/update.xml">http://twitter.com/statuses/update.xml</a>", $postbytes))
}

Written by Andy

September 1st, 2008 at 10:14 am

Posted in Powershell, Technology

Tagged with ,

No Responses to 'Powershell to Twitter'

Subscribe to comments with RSS or TrackBack to 'Powershell to Twitter'.

  1. Ah! You beat me to it ;-) Thanks anyway.

    Kris

    2 Sep 08 at 10:35 pm

  2. This is the second PowerShell Twitter script I’ve found online, and both of them return the error: “The remote server returned an error: (417) Expectation Failed.” I thought it was my credentials, but your function for getting the friend’s timeline works just fine. Is this anything you ran into?

    HensonStu

    3 Aug 09 at 7:01 am

Leave a Reply