Mike Ormond's Blog

Musings on mobile development and Windows Phone 7 in particular.

Updated Twitter PowerShell Script

Updated Twitter PowerShell Script

  • Comments 1

I’ve made a few updates to my Twitter PowerShell script, including a fix to ensure it complies with a change to the Twitter API and switching to tinyurl. Unfortunately my code highlighter doesn’t work so well with PowerShell so here it is in all it’s B&W glory.

############################################################################## 
## 
## Tweet-Blogpost.ps1  original code by Mike Ormond (http://mikeo.co.uk)
## 
## Take a blogpost URL and post title and post a Twitter status update
## Uses TinyUrl to get a link to the post
## 
## Example Usage: 
##        $postlink = "http://blogs.msdn.com/mikeormond/archive/2008/11/07/a-bit-of-an-experiment.aspx"
##        $posttitle = "A Bit of an Experiment"
##        $twitusername = "Twitter username here"
##        $twitpassword = "Twitter password here"
##
##        .\tweet-blogpost.ps1 $postlink $post.title $twitusername $twitpassword
## 
############################################################################## 

param (    [string] $postlink, 
            [string] $posttitle, 
            [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)
{
  [System.Net.ServicePointManager]::Expect100Continue = $false

  $request = [System.Net.WebRequest]::Create($RequestUrl)
    
  if ($Username) {
    $request.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
  }
    
  $request.Method = $RequestMethod
  
  if ($RequestMethod -ieq "POST") {
    $request.ContentType = $RequestContentType        

    $formdata = [System.Text.Encoding]::UTF8.GetBytes($PostString)
    $request.ContentLength = $formdata.Length
    $requestStream = $request.GetRequestStream()
    $requestStream.Write($formdata, 0, $formdata.Length)
    $requestStream.Close()
  }

  $response = $request.GetResponse()

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

##
## Generate the tiny URL 
##

$tinyurlrequest = "http://tinyurl.com/api-create.php?url=" `
                            + [System.Web.HttpUtility]::UrlEncode($postlink)

write-progress "Tweeting" "Getting tiny URL" -cu $tinyurlrequest

$tinyurl = SubmitWebRequest $tinyurlrequest "GET"

write-debug "Tweeting - Received tiny URL Response: $($tinyurl)"

##
## Post update to Twitter
##

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

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

write-progress "Tweeting" "Posting status update" -cu $tweetstring
write-debug "Tweeting: $($posttitle)"

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

write-debug "Tweeting - Posted status update. Response: $($twitResponseText)"
Technorati Tags: ,