Scott Hanselman

Progress Bars in PowerShell

July 31, '07 Comments [5] Posted in PowerShell
Sponsored By

PowerShell has a thousand nice features, but one of the nicer ones that I end up using all the time is the built in Write-Progress cmdlet. Shady the Intern came by today with a PowerShell script that printed dots to report progress, like:

Doing some stuff........................

I recommend he switch to Write-Progress. A nice feature of Write-Progress that I don't see used enough is the assigning of IDs to Activities, and then referencing those IDs as ParentIDs when writing out the progress of a Child Activity.

for ($i = 1; $i -le 10; $i++) 
{
  write-progress -id 1 -activity "Doing some stuff" -status "whatever" -percentComplete ($i*10);
   sleep 1; 
   for ($j = 1; $j -le 10; $j++)
   {
      write-progress -id 2 -parentId 1 -activity "Doing some child stuff" -status "yay" -percentComplete ($j*10)
      sleep 0.75
   }
}

PowerShell Progress

Here's a trivial example. Two for loops, each sleeping for a bit. The second for is a child of the first. Notice that ID of first loop is -activity 1 and the second references that activity via -parentActivity. This really adds, in my opinion, to the fit and finish of any script.

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Paper - Managing Large Scale System Deployment and Configuration with Windows PowerShell

July 31, '07 Comments [11] Posted in PowerShell
Sponsored By

Hanselman - Managing System Deployment with PowerShell.pdf - Adobe ReaderLiterally 1 million years ago, as I remember it (actually around November of 2006) I wrote a paper on our Financial Services solution that uses PowerShell to manage deployment.

There were a few Channel 9 Videos on the subject and a whole category on PowerShell was born on my blog. I even did a few presentations on the topic.

The paper was/is like 28 pages long and I wanted to get it out ASAP. I gave it to Dino at Microsoft and he was trying to get it to a guy at TechNet, and I wasn't supposed to blog it or release it or anything until they had their exclusive.

Fast forward to darn-near August 2007. My last day at Corillian is Thursday and my first day at Microsoft is in Sept...so, here's me releasing the paper myself and phooey on all those folks who were taking so long to publish it. It's too late for Corillian to fire me and (maybe) too late for Microsoft to un-hire me.*

Thanks to the Corillian Team who work on this project and who did all the hard work I take credit for. Thanks again to Geoffrey Bard for the Signing PowerShell Scripts section.

* Just kidding, of course I asked first. Seriously, you know me so well.

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Unit Testing Silverlight with Selenium

July 31, '07 Comments [9] Posted in Programming | Silverlight | Tools
Sponsored By

Selenium is a web testing tool that runs directly in the browser. That means it's all JavaScript. It'll run on the Three Major OSes and on the Three Major Browsers (and others!), so that's cool. There's even a recorder/playback IDE called Selenium IDE that takes the form of a Firefox plugin.

One of the things that's particularly interesting about Selenium is that it uses HTML Tables as its input Domain Specific Language. Go ahead and read that sentence again just to drink it in.

For example, if you had a page called "/default.html" and on that page there was a button called "myButton" here's the code to open the page and click that button. 

<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">SeleniumTest</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/Default.html</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>myButton</td>
    <td></td>
</tr>
</tbody></table>

This would be a "test" page and you'll also need a master "Suite" that would contain many tests. A Suite is just a table of tests:

<table>
<tr><td><b>Suite Of Tests</b></td></tr>
<tr><td>
<
a href="./SeleniumTest.html">Test Suite</a>
</
td></tr> </table>

Because of cross-site scripting issues, you have to install Selenium (just download the zip and upload the contents to your website) on your server and visit the pages from there. For example, here's the Selenium Test Runner on my server here at Hanselman.com.

As Selenium is all JavaScript, I thought it'd be nice to make sure it can test Silverlight applications. I can see how a testing framework that could easily interact with Silverlight would be a useful thing, so I set off to prove the concept.

First, I made a super-simple XAML of a TextBlock and a Circle:

<Canvas
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ButtonSharp="clr-namespace:ButtonSharp;assembly=ClientBin/ButtonSharp.dll"
    x:Class="ButtonSharp.Page;assembly=ClientBin/ButtonSharp.dll"
    Width="320" Height="240"
    Background="White"
    x:Name="rootCanvas"
    >
    <Ellipse Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="Circle" 
Width="169" Height="169" Canvas.Left="81" Canvas.Top="32"/> <TextBlock x:Name="MyText" Text="Hello World from my Script Tag"/> </Canvas>

In the code-behind I made a few Scriptable methods. By marking them Scriptable, they are made available to the "outside world" - in this case, JavaScript.

[Scriptable]
    public partial class Page: Canvas
    {
        public Page()
        {
            Loaded += new EventHandler(Page_Loaded);
        }

        TextBlock tb = null;
        Ellipse el = null;

        void Page_Loaded(object o, EventArgs e)
        {
            WebApplication.Current.RegisterScriptableObject("scott", this);
            tb = (TextBlock)this.FindName("MyText");
            el = (Ellipse)this.FindName("Circle");
        }

        [Scriptable]
        public void SetCircleColor()
        {
            tb.Text = "Hello from C#";
            el.Fill = new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0x00));

        }

        [Scriptable]
        public string GetCircleColor()
        {
            SolidColorBrush b = el.Fill as SolidColorBrush;
            return b.Color.ToString();
        }
}

The code does two useful things. First, it registers the class with the WebApplication via RegisterScriptableObject and names it "scott."  You'll see later where we refer to "scott" in JavaScript and you'll know we mean this class. Second, it finds the TextBlock and the Ellipse and squirrels them away for later use.

Back over on the outside, in the HTML page's JavaScript, I make two silly functions:

<script type="text/javascript">
    function changeColor()
    {
        var control = document.getElementById("SilverlightControl");
        control.Content.scott.SetCircleColor();
    }
    function getColor()
    {
        var control = document.getElementById("SilverlightControl");
        thing = control.Content.scott.GetCircleColor();
        alert(thing);
    }
</script>

Notice where we call the two managed C# methods via our "scott" object. Then I add two regular HTML buttons:

<input type="button" name="changeColor" value="Change Color" onclick="changeColor()"/>
<input type="button" name="checkColor" value="Check Color" onclick="getColor()"/>

You click one button and it calls changeColor which should make make the Circle red, and the other will ask the Circle what color it is and display the result in a JavaScript alert. You're welcome to run this little thing here if you like.

Note that you WILL need the Silverlight 1.1 Alpha Refresh first.

Now I make a Selenium Test like this. You can also use the Selenium IDE to record, write manually, step through and save tests if you're not into the whole Notepad thing.

<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">SeleniumTest</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>http://www.hanselman.com/silverlight/seleniumtest/ButtonSharp/Default.html</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>changeColor</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>checkColor</td>
    <td></td>
</tr>
<tr>
    <td>assertAlert</td>
    <td>#FFFF0000</td>
    <td></td>
</tr>
</tbody></table>

The Selenium Reference is excellent and contains all the commands you can use. Remember, each command is one line in  test table like:

command target value

And there's many you can use. You can also make your own by making a file called user-extensions.js, and there's lots of details on the Selenium site. You can also use actual JavaScript in the targets and values if you include 'javascript{ }' in the parameter.

I'm sure that if I knew JavaScript better I could better access the Silverlight object and test all kinds of things. I'll leave this exercise to the Smarter Than I Reader.

You can pass parameters to the Selenium Test Reader like the test file via ?test= etc. If you want to run my Silverlight App inside the Selenium Test Runner in your own browser, just go here. Do note the format of the URL. You can also just visit the runner and enter the URLs yourself. You can have as many tests as you like in the suite.

NOTE: When you startup the runner, change these settings for a more dramatic experience. Turn the speed down to Slow with the slider, and click Highlight elements Then click either of the Green Play Buttons.

Selenium Functional Test Runner v0.8.2 [1727] - Mozilla Firefox

It'd be interesting what kinds of community extensions to Silverlight could be created for Selenium to make it even easier. I also wonder how well Watir or Watin can interact with Silverlight.

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Upgrading your project from Silverlight 1.1 Alpha to Alpha Refresh

July 31, '07 Comments [2] Posted in Silverlight
Sponsored By

I am by no means a Silverlight expert. That said, here's the things I personally needed to do to update an existing Silverlight 1.1 Alpha project to Silverlight 1.1 Alpha Refresh.

First, I loaded up my existing Visual Studio 2005 project in Visual Studio 2008 Beta 2 and went through the conversion wizard. No warnings, no errors, but the wizard won't touch your JavaScript.

Next, I add a FRESH New Silverlight Project to my solution. I added this project for reference, and I'll delete it later.

I did a diff between the .js's that I had and the new one. Notice a few changes:

Silverlight.createObjectEx({
    source: "Page.xaml",
    parentElement: document.getElementById("SilverlightControlHost"),
    id: "SilverlightControl",
    properties: {
        width: "100%",
        height: "100%",
        version: "1.1",
        enableHtmlAccess: "true"
    },
    events: {}
});

You don't refer to the Silverlight object via Sys.Silverlight any more. Also, enableHtmlAccess takes a string "true" when before a boolean worked for me. Also, the version has changed to "1.1".

Previous Silverlight project wizards or samples might have put this in your body's onload:

<body onload="document.getElementById('SilverlightControl').focus()">

Now, if you want your control to have initial focus, you need to add the onload in a friendlier way:

// Give the keyboard focus to the Silverlight control by default
    document.body.onload = function() {
      var silverlightControl = document.getElementById('SilverlightControl');
      if (silverlightControl)
      silverlightControl.focus();
    }
You'll also need to copy the new "minimized" Silverlight.js file and notice that its size has been nearly cut in half by the process. Shiny.

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Measuring Satisfaction - We are a Nielsen Family!

July 29, '07 Comments [19] Posted in Microsoft | Musings
Sponsored By

Nielsen Ratings Here in the US (and 40 other countries), for the last 60+ years, Television Ratings have been managed by the Nielsen Media Research company. They have a special magic sauce that let's them calculate "Reach" - the number of unique viewers for any show at any time. There's lots of good details on this at Wikipedia.

They figure there are 115 million TV households and a ratings point is 1%, or 1,152,000ish households. A show might get a 9.2 rating meaning that 10.6 million folks watched that show.

I always figured they had some magic boxes hooked up to televisions, or that they bought data from Tivo, or from my cable company. I mean, my TV knows that I'm watching Dr. Who (love the new Dr. Who), so I figured it told someone else.

Surprise, we're a Nielsen Family, at least this week. An envelope showed up with three US$10 bills and this paper booklet. Ah, how retro. Even the font, color choice and look and feel had a decidedly Mike Bradyesque vibe to it.

Surely since this company has had every American Television network by the short and curlies as a loyal client for the last half-century, they must have developed an extraordinarily sophisticated system for determining what I watch. Surely there's an explanation why Jericho was canceled (fortunately they've ordered new episodes) and the secret must lie within this mysterious envelope. My expectations are high...

Nielsen Ratings2

Wow. That's...lovely. Welcome to the late 1980's. Really. I'm supposed to fill this out as I watch? This is the engine that makes the world's television ratings system spin?

This is such a confusing system that Nielsen has actually put up a website dedicated to decoding the paper booklet! I particularly like Step 5, "Returning the Booklet in the Mail" - truly a must-read if you're behind on sleep.Well, now I see why all the good shows are getting canceled. Why isn't this automated? Why aren't our TV's reporting back to the Mothership?

A TV can't tell if you're satisfied with a show, just if you're watching it. Same with a DVR. Presumably if you taped it, you wanted to watch it. This paper system is silly because it's asking me to copy down manually what someone (Tivo, Cable, whoever) must already know.

Or at the very least they could have created a data-entry website, rather than an interactive help website. Seems like if you're not clever enough to fill out this survey then you may not be able to get your computer turned on and browser navigated to http://tvdiary.tvratings.com, but perhaps that's presumptuous of me.

The only way (that I can see) to determine if someone likes a show is by asking them. Now, turning to other things that capture our attention like games. Xbox Live can tell if someone is satisfied because not only do they know when someone is playing a game, but they can see how far and how hard you're trying by looking at what Xbox Achievements you're getting. The Halo team is fanatical about their statistics. So, it seems much easier to glean the "satisfaction" one has for a game, but still, there's nothing better than asking.

Now, for a segue, but a totally honest question. If you have a piece of software that you've written, what's a good way to figure out if the end-user is really satisfied? I mean, Office, Windows, etc, all sell lots, but does that mean I'm satisfied with them? Should software include a star-ratings interface like some blog posts do on every dialog box? Perhaps when you click it, you're immediately asked for comment? Maybe a "Click here to blog about this feature" link?

SatisfactionBox

One of the coolest most out of the box thinking ideas I saw come out of Microsoft last year was the Send a Smile Tool that was used in the Office 2007 Betas. Unfortunately you had to install it yourself - it wasn't automatically part of the beta experience.

However, the idea was brilliant in its simplicity. A number of bug reporting tools like FogBugz have similar tools for reporting bugs and attaching screenshots, but Send a Smile was even simpler.

You just clicked on the happy smilie or the sad smilie, entered your comment, and checked a box to optionally send a snapshot. That's it.

As a developer, I wish this tool was automatically included with Visual Studio and any major Developer Tool. Talk to the Office team, they have a database and UI and everything to manage these screenshots and smiles. A developer version could even optionally include the text of some code you're working on.

To be clear, there's LOTS of great places to give feedback about the Developer Tools, but the lower the barrier the better. Let me get you my opinions and thoughts with one click.

Have you built satisfaction measuring tools (other than a "click here for a comments page) into your software?

Now, I want this for my TV so I can send Dr. Who and the BBC a smile.

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

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