Scott Hanselman

Windows Home Server Twitter Notification Plugin

July 15, 2009 Comment on this post [7] Posted in Coding4Fun | Home Server | Source Code
Sponsored By

A while back, the very wise Brendan Grant sent me some sample code that would use Twitter to report the health of one's Windows Home Server. I always meant to update the code to use TweetSharp to talk to Twitter, as well as add some robustness for connected/disconnected scenarios, but I'm just never going to get around to it. Instead, here it is as he sent to me.

There's a REALLY vibrant community around Windows Home Server plugins and if you've got a WHS and you want it to do something that it doesn't do, I'd encourage you to jump in.

Even as I'm posting this, I'm sure there are better and more interesting implementations. However, I like what Brendan has done to abstract away the core COM-based API of WHS for use in managed code.

Here's the full program...note again that the PostTweet() method is hacked together and should use a more robust technique:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using Microsoft.HomeServer.SDK.Interop.v1;

namespace Twitter_Test
class Program
static string username = "";
static string password = "";

static void Main(string[] args)
IWHSInfo info = new WHSInfoClass();
//Register application name
info.Init("WHS Twitter Client");

NotificationCallbackClass notificationClass = new NotificationCallbackClass();
//Register notification callback class

//Check current state
Console.WriteLine("Current State: " + notificationClass.GetHealthState().ToString());

notificationClass.HealthChanged += new EventHandler(notificationClass_HealthChanged);

Console.WriteLine("Monitoring for health changes. Press to exit.");

static void notificationClass_HealthChanged(object sender, HealthChangedEventArgs e)
Console.WriteLine("Current State " + e.Health.ToString());
PostTweet(username, password, "Your Windows Home Server's health is now: " + e.Health.ToString());

private static Status PostTweet(string username, string password, string message)
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
// determine what we want to upload as a status
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + message);
// connect with the update page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
// set the method to POST
request.Method = "POST";
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded";
// set the length of the content
request.ContentLength = bytes.Length;

request.ServicePoint.Expect100Continue = false;

// set up the stream
Stream reqStream = request.GetRequestStream();
// write to the stream
reqStream.Write(bytes, 0, bytes.Length);
// close the stream

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(response.GetResponseStream());
string s = sr.ReadToEnd();

XmlSerializer ser = new XmlSerializer(typeof(Status));
object o = ser.Deserialize(new StringReader(s));
Status status = o as Status;

return status;

There interesting part is the Eventing part where he makes changes in your Home Server turn into .NET Events via callbacks. Check the code for details. You can get events when Physical Disks are changed, when Backup States change, or when basically anything happens. There's a number of folks on Twitter already who have their Windows Home Servers tweeting.

If you've got, or you're using a plugin to report your Home Server status on Twitter (or SMS or whatever) leave a comment and I'll update the post! I'm sure there are better solutions than this little sample.

Here's the code if you want it, and remember, it may kill your pets. If so, don't blame me as I'll deny everything. It's a sample you found on the Internet, what did you expect?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 15, 2009 13:35

is a spiffy ready-to-install plugin for WHS...

July 15, 2009 18:06
Why set the Authentication: Basic headers manually when you could just set the Credentials property to a NetworkCredential?
request.Credentials = new NetworkCredential(username, password);
July 15, 2009 18:48
Woot! Dakota State University represents again. Way to go Brendan!! Now I'm jealous. Very jealous
July 15, 2009 22:13
Thanks Scott, this looks really cool. I'm loving my WHS.
July 16, 2009 15:39
Using Basic auth over an unencrypted connection?? I don't use Twitter, but I would think they have SSL, right?
July 17, 2009 9:26
Shouldn't use ASCII. I've been burnt TOO many times where I've shared source code them someone in Lithuania says all their text turns to question marks.
July 24, 2009 0:10
You guys should check out the Twitterizer project: ... it might help simplify the code and it's got a quickly growing community behind it.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.