Archives for the Month of March, 2006

PowerShell the Oracle — Instant Answers from your Prompt

One of the scripts I like the most in my toolbox is the one that gives me answers to questions from the command line.

For the past 2 years or so, Encarta has offered an extremely useful “Instant Answers” feature.  It’s since been integrated into MSN Search, as well as a wildly popular Chat Bot.  MoW showed how to use that feature through a Monad IM interface (via the Conversagent bot,) but we can do a great job with good ol’ screen scraping.

PS:51 > Get-Answer "What is the population of China?"

China Population, total: 1,313,973,700
2006 estimate
United States Census International Programs Center

PS:52 > Get-Answer "5^(e^(x^2))=50"

5^(e ^( x^2))=50 : x=-0.942428

PS:53 > Get-Answer "define:Canadian Bacon"

Definition for Canadian bacon
lean bacon

PS:54 > Get-Answer "How many calories in an apple?"

Apples calories
1.0 cup, quartered or chopped has 65 calories
1.0 NLEA serving has 80 calories
1.0 small (2-1/2" dia) (approx 4 per lb) has 55 calories
1.0 medium (2-3/4" dia) (approx 3 per lb) has 72 calories
1.0 large (3-1/4" dia) (approx 2 per lb) has 110 calories
1.0 cup slices has 57 calories

PS:55 > Get-Answer "How many inches in a light year?"

1 lightyear = 372,461,748,226,857,000 inches

Here is the script, should you require your own command-line oracle:

## Get-Answer.ps1
## Use Encarta's Instant Answers to answer your question
## Example:
##    Get-Answer "What is the population of China?"

param([string] $question = $( throw "Please ask a question."))

function Main
    ## Load the System.Web.HttpUtility DLL, to let us URLEncode
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Web")

    ## Get the web page into a single string with newlines between
    ## the lines.
    $encoded = [System.Web.HttpUtility]::UrlEncode($question)
    $text = (new-object System.Net.WebClient).DownloadString("$encoded")

    ## Get the answer with annotations
    $startIndex = $text.IndexOf('<span class="answer_header">')
    $endIndex = $text.IndexOf('</div><div id="results">')

    ## If we found a result, then filter the result
    if(($startIndex -ge 0) -and ($endIndex -ge 0))
       $partialText = $text.Substring($startIndex, $endIndex - $startIndex)

       ## Very fragile, voodoo screen scraping here
       $partialText = $partialText -replace '<span class="answer_feedback">.*Is this useful\?',"`n"
       $partialText = $partialText -replace '<span class="attr...">',"`n"
       $partialText = $partialText -replace '<BR />',"`n"

       $partialText = clean-html $partialText
       $partialText = $partialText -replace "`n`n""`n"

       "No answer found."

## Clean HTML from a text chunk
function clean-html ($htmlInput)
    $tempString = [Regex]::Replace($htmlInput, "<[^>]*>""")

. Main


[Edit: Updated to work with Windows Live Serach, and with recent PowerShell builds.]

HanselMinutes talks about Monad

Scott Hanselman and Carl Franklin talked at length about Monad today in the HanselMinutes podcast.  It’s very flattering coverage, and hits a lot of the “WOW” points that differentiate Monad from traditional shells.

Thanks to Keith for pointing the newsgroups to this, and for introducing Scott to Monad in the first place.

Good job, Scott, and welcome to the family 🙂

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Enjoy the First Day of Spring

Today marks the first day of Spring – a date often hailed often as one with equal amounts of night and day.  This is untrue because most of us live somewhere other than the equator, because the Sun is larger than an idealized geometric point, and because our atmosphere causes refraction.  Truthfully, it is more of an Astronomical and geometric novelty than it is a practical one.

It’s still an interesting geometric novelty, though.  According to Wikipedia, the Vernal Equinox falls at 18:26 UTC.  Here’s a little trick to let you find that in your local timezone:

MSH:32 > [DateTime]::Parse("18:26 GMT")

Monday, March 20, 2006 10:26:00 AM

All pedantry aside, I hereby present to you this fluffy cute video of cats enjoying what could very well be spring days.  I do warn you, though – the video is a narcotic to small children.


[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

[Edit: Here are the dates for other years: March (3/20/06, 3/21/2007, 3/20/2008, 3/20/2009, 3/20/2010, 3/20/2011, 3/20/2012, 3/20/2013, 3/20/2014, 3/20/2015, 3/20/2016, 3/20/2017, 3/20/2018, 3/20/2019, 3/20/2020)]

MS Connect – Please provide bugs and feedback on Monad!

I’m excited to announce that we recently added Monad to the list of supported products on the Microsoft Connect site –

On the Monad team, we pride ourselves in listening to customer feedback.  We used the BetaPlace feedback system in the past, and the Connect site now makes the process much more efficient.  This is especially true in helping us determine the magnitude of a given problem, or desire for a feature request.

To be very clear, the public Beta 3 build is (more or less) what we consider “done.”  We have a handful of product changes (and a few handfuls of bug fixes) in our current builds, but you are currently working with what is very nearly the finished product.

Have a bug?  Let us know.  Have a scenario that we poorly address?  Let us know.  Have a usability problem?  Let us know.  Don’t assume that the issue will go away in the next beta, as it very likely will not.  Any future changes to V1 of Monad will be driven entirely by strong customer and partner feedback.  In the absence of this, the product will remain as-is.

Of course, we will weigh this feedback against the other very real priority – getting V1 into your hands as soon as we possibly can.  If we can't accomodate your feedback for V1, it will prove very useful in helping us plan V2.

In order to help us weigh this feedback, please prefer voting over creating new feedback items.  A vote for existing feedback is actually more important than a new piece of feedback, as the number of votes helps us measure its impact.

How to register for our feedback database:

  1. Visit
  2. Sign in with your credentials accepted by the Microsoft Passport Network
  3. Click “Apply” next to the item, “Windows Monad Shell”
  4. Agree to the Terms of Service
  5. Supply your personal information

How to submit a piece of feedback:

  1. See if your issue has already been covered:
    1. Select “Feedback” from the left-hand side of the screen
    2. Search for keywords that others may have used to describe your issue
  2. If you find an item that represents your feedback:
    1. Click its title
    2. Click “Vote Now” under the rating box to add your vote to the item’s rating
  3. If you do not find an item that represents your feedback:
    1. Select the “Submit Feedback” option from the left-hand side of the screen
    2. Complete the form, including as much detail as possible.  For this step, please see the detailed instructions available through the “Windows Monad Shell Home” section of the left-hand side of the screen

Looking forward to mountains of feedback!

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Shoe tying revolution! Ian's Shoelace Site

Peter Provost unearthed this site the other day, and it’s really great. It’s called “Ian’s Shoelace Site,” and he’s got lots of cool things to do with your laces.  That’s all well and good if you are a knot fiend, but he’s also got two very practical knots:

Believe it or not, my daughter and I spent Sunday afternoon tying our shoes!

How to get Monad Training?

This question has come up several times internally, so I thought I'd post my answer for posterity.  "My customer would like Monad training -- are there any courses avialable?"

Usually, these customers are looking for a good introduction to Monad – and not really training on the scripting language or development.  The best thing is probably to introduce them to our 2 recorded Live meetings:

If you want to really make it a hands-on experience, you could ensure that everybody has Monad installed on their machine, and watch these webcasts together.  That way, you can play with it as you learn about it.

If you want to progress further into the scripting and development aspects of Monad, the “Monad Documentation Pack” is a great resource.  Start with the "Getting Started" guide.  You can also bring in a continental breakfast, a few urns of coffee, and go over the scripting, cmdlet, and provider labs from the PDC:

For those that learn by book, O’Reilly’s Monad book is a great place to start:

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Source Code and the Monad Community

The Monad community is vibrant, and loves to write stuff.  Even better, is that they love to share that stuff.  Sometimes, even with source code.  Jeff’s JaMSH is one example of a project that has shared its source code, and Karl seems to have intentions to share the source for his MshAnalyzer.  Taken even broader, several of our resident newsgroup gurus have been bantering around the idea to create a shared workspace for MSH extensions – likely to be called “Community Extensions for MSH.” 

On the team, we love to play with these things – and even provide feedback on how they are implemented.  Sometimes, though, the licensing terms can prevent that.

If you contribute source code to the community (such as the Community Extensions for MSH workspace) and place it under an open source license, please be aware of the implications that the license type may have.  Some open source licenses (ie: the GPL) may prevent employees at Microsoft and other companies from being able to review or provide feedback on your source code.  The BSD style license is an example of an open source license type that will be easier for proprietary software companies to work with.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Seattle Commute Times Visualilizer

When I was first looking for a house, one of my primary constraints was commute times.  I started by asking people – “How long does it take you to commute to work?”  I started building a mental map, but then started thinking about putting pins on a real map to help me remember all of the data I was getting from people.

But then I thought, “Hey – computers are supposed to make this kind of stuff easier.”  And then, “Hey – I’m a software developer.  I’m supposed to make programs that make computers do useful things.”

So I did.  I built a website that allowed people to enter everything they knew about their commute times, and then another page that let them visually explore the data set.  For example, “Find me all commute times less than 30 minutes that let me commute to work by bus before 8:30 in the morning.”

With well over a thousand data points, it’s pretty useful.

When the Virtual Earth team stabilized their API a while back, I turned it into a mashup.  So if you’re thinking about commuting around the Seattle area, enjoy:

P.S: Although I wanted to use AJAX for the gimmick factor, it would have made the site perform terribly!  Given the expected usage pattern, it is much more efficient to download the entire data set at once.