Skip to main content

Powershell to Twitter

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.

[sourcecode language="powershell"]

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)) } [/sourcecode]