Andy Oakley

Archive for the ‘twitter’ tag

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 ,