Differing output from Where and Where-Object

One of the lesser known features of PowerShell are some “magic” methods that get added to most (all?) collection objects that replace the slower Where-Object and ForEach-Object cmdlets with basically the same functionality. They’re considered magic because they aren’t well documented even years after they were introduced. (Thank goodness for bloggers) I’ve used ForEach quite a bit, but often forget about it’s Where counterpart and apparently had never actually done much with it until today when I ran into a weird issue where I couldn’t set the value of a property on a returned object.

The setup is a pretty classic needle-in-a-haystack problem where you have an array of objects and need to update a property on just one of them. Pretty classically you’d do something like this.

It works great but if you’ve got a really big array of complex objects it can start to take a long time to process. So today I had remembered the aforementioned magic methods and figured it would be a lot faster to use Where to do essentially the same thing. Except I got a really unexpected error.

Maybe it was returning a single element array? Running “$testElement -is [array]” said “False” but “$testElement[0].attr = ‘something'” worked just fine, so what was going on here? Time for some more “magic” in the form of the pstypenames property.

Of course while I was tinkering with all of that and doing some research on the Where and ForEach methods I ran across that article I linked further up and figured out the correct solution to the problem.

Finding an item in an array of PSCustomObjects

(This is mostly so I can find it again someday when I need it again.)

When working with REST interfaces with PowerShell it’s pretty common to get JSON responses that have information that is returned as arrays of PSCustomObjects. If you need to update a property of one of those objects you can’t just do something simple like:

In order to set the value of a property you’re going to have to find the index of that particular object in the array and then manipulate it directly. Thankfully this is easier than it sounds because we have access to the static methods of the .Net Array object, and FindIndex in particular. The previous example actually ends up being something like this:

Standardized Tests for Standardized Parameters

Something that you find when writing PowerShell modules to wrap API functions for external systems is that a lot of your functions tend to have a consistent subset of parameters that get used for things like credentials and specifying an endpoint. For example in the private TeamCity module that I maintain the parameter block for every function that interacts with a server has:

(Whether that is the best pattern I’m still not sure, but it’s beside the point of what I’m talking about here. If you have better ideas I’d love to hear about them!)

If you are writing good unit tests for your functions you need to test those parameters in every single one of those functions and ideally you want to test those parameters consistently to make sure that FunctionA doesn’t use them slightly differently than FunctionB. Additionally if I find a better way of testing those parameters I don’t want to have to update¬†dozens (or more!) of Describe blocks. There had to be a way of writing those tests once and then calling those tests¬†consistently when testing every one of those functions and it turns out to be pretty simple.

The trick is to consider that Pester is pretty consistent about scope inside the Describe and Context blocks so if we were to dot-source some external file in the correct context we should be able to inherit anything that’s in that file. Declaring variables with consistent test values and wrapping tests inside of a function definition means that we can do the dot-source in the scope of a Describe block and then reference those variables and call those sets of tests in every function’s tests.

For example, let’s start with a file called “StandardTests.ps1” that defines two variables and a function to test those two variables:

Then making uses of those variables and tests might look something like this:

The only thing left is to run the tests!
Successful Test output

Reporting Pester Code Coverage Metrics to TeamCity

As previously mentioned I’ve been doing a lot of work with PowerShell modules at work where I have recently gotten all the parts for a full continuous delivery pipeline working for those modules. A big section of that pipeline runs through TeamCity and while the existing ability to have Pester test results show up in the build results is really great, code coverage is slightly less obvious but in the end fairly simple.

The trick is to use the -PassThru parameter with Invoke-Pester and then use TeamCity’s build reporting interaction to get the values into the system. The end result will look a lot like this:

Pester code coverage right in your TeamCity build results!
Pester code coverage right in your TeamCity build results!

Simple test coverage check for script modules

I’ve been spending a lot of time at work writing PowerShell modules and as part of that effort we’ve been trying to make sure we’re doing at least some unit testing on those module functions (Using Pester of course!). Unfortunately we’ve had a few instances where a new function gets added to a module without any unit tests being added. We’ve structured our modules so that every function has it’s own source file and accompanying tests file and all of them are located in a \Functions\ folder in the project. Ideally the CodeCoverage parameter for Invoke-Pester would catch this sort of problem but it only runs tests for files with a certain file name structure and so if it runs across Some-Function.ps1 without an accompanying Some-Function.Tests.ps1 it doesn’t care. Today I finally got a little tired of finding broken functions and decided to do something about it, the result is Coverage.Tests.ps1:

I’ve got to think that it shouldn’t be hard to add a similar test for my other pet peeve: Missing help comment blocks!

My Year in Soda: 2014

I’ve been trying to remember to post my impressions of sodas that I try to Twitter through the year and figured I’d put together a Year In Review to collect all of the notes for posterity.

This year’s high points: Phancy Sparkling Limeade and Empire Spruce Beer.

Virtual Micropolis: Progress and Upcoming Display!

I mentioned back in March that I had started working on a wiki to provide further information about our Micropolis modules and I am fairly proud to say that we’re definitely making progress on filling the site with content. It’s not complete yet, by any stretch of the imagination, but it’s got a good base of content and the visual style is starting to come together as well.

If you haven’t had a chance to look at it, or you haven’t looked since last March, now is a great time to go take a peek at http://www.virtualmicropolis.com and you can get updates by subscribing to any of the RSS feeds on the site or by following the Mayor’s twitter feed @vmicropolis!

We have also been invited back to the Saint Anthony Park Library to display our layout as part of their post-renovation grand re-opening party! If you missed the display in March this is a great time to come out and have a look at our layout with some additional modules by Thomas Anderson. Plus Peter Hoh is returning with his educational models, the travelling DK Books display for their Star Wars LEGO books we be on hand, at least one stormtrooper from the local 501st, and refreshments provided by the little grocery store up the street! Mark your calendars for Wednesday August 14, 2013 from 6-8pm and be sure to tell anyone else who likes LEGO.

LibraryShowFlyer_August Saint Anthony Park Library Show - August 2013


(PDF Version of August Library Show Flyer)

How To Hard Boil Eggs

  1. Put eggs in pot
  2. Cover eggs with cold water
  3. Cover pot with lid
  4. Place pot on stove and set heat to high
  5. Set timer for 20 minutes
  6. When water boils remove lid and remove pot from burner
  7. Let eggs cool slightly, then chill in refrigerator

You wouldn’t think that I would need to write that down in a place that, in theory, I will never forget it but every year around this time I ask Jennifer or Sigrid something along the lines of, “How do you boil eggs again?”. Clearly even though I have been coloring eggs with the kids for the past five or six years, hard boiling eggs once a year is not enough for it to be retained in my long term memory. So this time I am writing it down so that next year I don’t have to ask them for it yet again. I hope that I’m not betraying part of the tradition by doing so…

The observant among you may be thinking something along the lines of, “I didn’t think he was a Christrian…” and in fact I am not. At least I have not been for approximately the past nearly twenty years. My faith in that particular range of beliefs was already starting to fall apart during high school despite my family and community’s sincere and steadfast efforts to the contrary and getting out into the world on my own took care of the rest of it pretty quickly. As with many people who were newly “Not a Christian” I did go through a phase of pretty explicitly and strongly rejecting anything and everything associated with it and I still feel that way vaguely about things like Christmas (though for quite awhile really due more to the orgiastic consumer culture aspects of it than anything else) but a year or two after Jennifer and I got together I suggested that I might like to color eggs with the kids once they were old enough to have just enough attention span and manual dexterity to make it not also involve repainting the dining room.

Every year since then I have looked forward to coloring eggs some spring-ish Saturday afternoon before a holiday that I otherwise don’t really participate in.

Partly I enjoy spending the time with the kids. Partly I just enjoy the challenge and entertainment of coloring my own eggs (I get to do half to one-third of the dozen). In large part though I do it because I have fond memories of spring-ish Saturday afternoons in my childhood with the rest of the family around the house for once before one of the busiest days, for my family at least, of the year. My dad had planted his Good Friday potatoes and was taking most of the day off in a set of weeks that he otherwise had none. My mom was worrying over getting the house cleaned and everyone’s clothes ready and the food cooked and keeping us kids from maiming each other and keeping whatever other plates spinning that I, as an adult about the same age as she was at the time, only now begin to comprehend.

But really the rush and press of spring and summer is nearly upon us and it’s nice to just sit and color some eggs.

No tag for this post.

Introducing Virtual Micropolis

Virtual Micrpolis Logo

After a few years of owning the domain name, I am finally getting off my proverbial butt and doing something with VirtualMicropolis.com. My original idea had been, as is somewhat usual for me, a bit grandiose. I was going to get the spec moved over there and make it a community for everyone who built Micropolis to come and post their stuff. Because there aren’t any other places on the Internet to build a community (like Flickr, MOCPages, Facebook, Google+, ad infinitum…), or something.

Anyway, the point really came home last weekend in Des Moines where we were displaying our little corner of Micropolis again and we also had the TwinLUG QR Code out on the table next to it. As usual we got several people who tried to use it, and mostly it worked (The lighting was a bit weird), but the overwhelming response to being sent to the TwinLUG site was one of disappointment. What people really wanted to see was lots of pictures and maybe some more stuff about what they were looking at right then. Obviously it was finally time to do something about it.

To get this really rolling though I was going to need to scale things back to just a place where we could put up information and pictures about just the modules that Jennifer and I own. Almost all of them are ones that we designed with the exceptions of some that I bought off a TwinLUG member before he moved out of the country a few years ago (Hi Gary!). Thanks to the wonderful photography skills of Alyska Bailey-Peterson we had a base of some excellent photos to go along with the drek that I manage to shoot so that we could at least get the site off the ground before having to figure out where we were going to get more good pictures.

For this project I think we really needed a Wiki. A blog or other groupware CMS system was just going to have too much overhead and complexity for the basic requirements of setting up easily linkable pages that could be simply protected from spam or other ne’er-do-wells with some file management capabilities. I finally settled on DokuWiki and I’m pretty happy with the results so far. My one small issue is that for some reason there are no simple methods of setting text alignment, but everything else is great so I’m ignoring that as much as possible. If you’re looking for a good Wiki platform you should definitely add them to your list of candidates.

As of right now I’ve got pages up for eight modules and material for a couple more before we start to run out of images, but I think it’s a decent start and hopefully we’ll be able to keep some momentum on the project for awhile.

While right now the site is all about our collection and the layouts that we have been part of but I think we would be glad to broaden the content in the not too distant future. I do have things locked down so that even if you register for an account you can not make any edits until I tweak the account so please contact me if that is your intent.

Good Display at the Library

Decent Sized Layout

As I mentioned a couple of weeks ago, Jennifer and I put together a little display of our Micropolis modules at the library yesterday. I took the photo above just before we started to take it apart and get it home. I have no idea who the kid is, but I think the image turned out pretty well even if a solitary figure like that doesn’t given any good idea of how busy it got.

Going in yesterday afternoon we really didn’t know how many people were going to show up. We had put it together on pretty short notice so it hadn’t gotten into the Library’s regular promotional material and we had only sent it around to the neighborhood mailing list and a few groups and friends. So we were very slightly worried that very few people were going to show. Well, there were no worries about that.

We don’t really have any good way of estimating how many people came through, though the consensus appears to be that we can safely figure there were at least a hundred people who came through. From there, depending on who you ask, it was maybe as high as three hundred. The first hour was the most busy and for awhile the crowd around Micropolis was 2-3 deep.

Peter’s display got a lot of attention too and I’d be surprised if he wasn’t really hoarse given how much chatting he did with people while demonstrating his stuff. I was pretty sure he’d be a good fit when we invited him to show with us and I’m glad I was right. He really has a great way of interacting with everyone. When I asked him how well he thought it went during the display, he answered by showing me that he had handed out a third of the brochures for the Science Museum of Minnesota’s summer education series. Granted, this neighborhood is a pretty good crowd for that sort of thing and you can never tell how many of those made it home AND didn’t go immediately into the recycling bin, but it’s at least a nice anecdotal metric.

I should also publicly and sincerely thank my sister and brother-in-law who came in and monitored our free play areas for us while we were busy interacting with everyone around the main display. The library had said that having bins of parts out for general play would be absolutely fine but that they had to be closely monitored for liability reasons. Becca and Raulie arrived in plenty of time and took care of things for the full two hours. Having those areas available so that when the kids got done NOT being able to touch the city they could go and put together their own stuff worked out beautifully, and we couldn’t have done it without them.

As for the Micropolis display itself it ended up being a perfect rectangle at 5.5 by 10.5 blocks. Considering that it only included modules that the two of us own I am really proud of the size of that display. We did leave out two lesser quarter block modules and some waterfront but it wasn’t a tough choice to do so. It was also extremely gratifying to finally see a properly complete Bluff portion of the display. Up until this point my gorge module has always had to be tucked away into a corner because we didn’t have enough other bluff modules to cover two of the sides but the spurt of building in the last couple of weeks meant that finally we had a nice hill. Three of those modules are non-compliant with the spec, but in a case like this it’s easy to make that kind of exception. In a larger display with more contributors I think it still might work but we’ll have to see.

As usual one of the most common questions we got was, “How many LEGO bricks/parts are in this?” and I had taken the time to figure out an answer before we got there: About 100,000. I think we’re probably easily within 10-15% of that, but at some point I’m going to have to figure out a more accurate number. I arrived at that estimate by estimating an average of 500 parts per quarter block. If we expand that out to 5.5 x 10.5 blocks or 11 x 21 quarter blocks, which is 231 quarter blocks times 500 to get 115,500. I do think it’s possible that the average per quarter block is a bit high but I don’t see it being any lower then 400 and that still puts us in spitting distance of 100,000 so I’m just going to go with that for now.

I find it consistently interesting seeing what other people appreciate in a bigger layout like this. Living with the collection and getting the family’s opinion tends to split two, and occasionally three, ways depending on where Jennifer and I are at on particular questions. Run about a hundred people through the room though and the diversity of opinion explodes. People point out modules that maybe one of us thinks isn’t the best work, but something about it speaks to that individual. It’s a great thing to watch happen.

In any case, I think we can call the display an unqualified success. We’re vaguely discussing the possibility of doing it again next year. In many ways though, it was a good dress rehearsal for two weeks from now in Des Moines, IA.

No tag for this post.