Twitter PowerShell Script


Here’s the PowerShell script I came up with that I referred to in my previous post that integrates with the social networking scripts to additionally generate a Twitter update with a link to the post. I’m sure this script could be improved – I’m no PowerShell expert – but I’ve got it to a point where it seems to work reliably. I’m also not sure of the best way to debug PowerShell scripts. Coming to it from the .NET world it’s a bit of a shock to the system (at least the way I’ve been debugging PowerShell). I should probably have read this post first and saved myself a lot of trouble.

############################################################################## 
##
## TweetBlogpost.ps1 original code by Mike Ormond (http://mikeo.co.uk)
##
## Take a blogpost URL and post title and post a Twitter status update
##
## Example Usage:
## $postlink = “http://blogs.msdn.com/mikeormond/archive/2008/11/07/a-bit-of-an-experiment.aspx”
## $posttitle = “A Bit of an Experiment”
## $snipusername = “snipr username here”
## $snippassword = “snipr API key here”
## $twitusername = “Twitter username here”
## $twitpassword = “Twitter password here”
##
## .\tweetblogpost.ps1 $postlink $post.title $snipusername $snipapi $twitusername $twitpassword
##
##############################################################################

param ( [string] $postlink,
[string] $posttitle,
[string] $snipusername,
[string] $snippassword,
[string] $twitusername,
[string] $twitpassword)

[System.Reflection.Assembly]::LoadWithPartialName(System.Web“) | Out-Null

function SubmitWebRequest( [string] $RequestUrl,
[string] $RequestMethod,
[string] $RequestContentType,
[string] $PostString,
[string] $Username,
[string] $Password)
{
$request = [System.Net.WebRequest]::Create($RequestUrl)
$request.Method = $RequestMethod
$request.ContentType = $RequestContentType

if (![String]::IsNullOrEmpty($Username))
{
$request.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
}

$formdata = [System.Text.Encoding]::UTF8.GetBytes($PostString)
$request.ContentLength = $formdata.Length

$requestStream = $request.GetRequestStream()
$requestStream.Write($formdata, 0, $formdata.Length)

$response = $request.GetResponse()

$reader = new-object System.IO.StreamReader($response.GetResponseStream())
$returnvalue = $reader.ReadToEnd()

$requestStream.Close()
$requestStream.Dispose()

return $returnvalue
}

##
## Generate the snip URL from snipurl.com
##

$sniplink = [System.Web.HttpUtility]::UrlEncode($postlink)
$snipuser = $snipusername
$snipapi = $snippassword
$snipstring = `
[String]::Format(“sniplink={0}&snipuser={1}&snipapi={2}“, $sniplink, $snipuser, $snipapi)

write-progress “Tweeting” “Getting snip URL” -cu $sniplink

$snipResponseText = SubmitWebRequest `
http://snipurl.com/site/getsnip” `
“POST” `
“application/x-www-form-urlencoded” `
$snipstring `
[String]::Empty `
[String]::Empty

writedebug “Tweeting – Getting snip URL Response: $($snipResponseText)”

## Grab the snip URL which is in the id element
$sr = newobject System.IO.StringReader($snipResponseText);
$xr = [System.Xml.XmlReader]::Create($sr);
$xr.ReadToDescendant(“id”);
$snipurl = $xr.ReadElementString(“id”);
$sr.Close();
$xr.Close();

##
## Post update to Twitter
##

## Check if we need to truncate post title as were limited to 140 chars total
if ($posttitle.Length > 100)
{
$posttitle = $posttitle.Substring(0, 100)
}

$tweet = “New blogpost: “ + $posttitle + ” (“ + $snipurl + “)”
$tweetstring = [String]::Format(“status={0}”, $tweet)

writeprogress “Tweeting” “Posting status update” cu $tweetstring

$twitResponseText = SubmitWebRequest `
“https://twitter.com/statuses/update.xml” `
“POST” `
“application/x-www-form-urlencoded” `
$tweetstring `
$twitusername `
$twitpassword

writedebug “Tweeting – Posting status update Response: $($twitResponseText)”


Technorati Tags: ,


Comments (2)

  1. I’ve made a few updates to my Twitter PowerShell script , including a fix to ensure it complies with