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.
############################################################################## ## ## 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 ## ## 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" ## ## .\tweet-blogpost.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 write-debug "Tweeting - Getting snip URL Response: $($snipResponseText)" ## Grab the snip URL which is in the id element $sr = new-object 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 we're 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) write-progress "Tweeting" "Posting status update" -cu $tweetstring $twitResponseText = SubmitWebRequest ` "https://twitter.com/statuses/update.xml" ` "POST" ` "application/x-www-form-urlencoded" ` $tweetstring ` $twitusername ` $twitpassword write-debug "Tweeting - Posting status update Response: $($twitResponseText)"
PingBack from http://blogs.msdn.com/mikeormond/archive/2008/11/10/twitter-powershell-script.aspx
I’ve made a few updates to my Twitter PowerShell script , including a fix to ensure it complies with