Dusting off the cobwebs a bit

Over the past couple of years since I last added a new post to this blog I’ve been halfheartedly working on converting from WordPress to one of the modern static generators but luckily a recent involuntary, but somewhat welcome, change in employment has freed me up to finally (hopefully) make some progress on that front. With any luck I’ll have the site updated to new hosting and back-end by the end of the year. I’ll be trying to document that process at least in the code repos, and possibly with a few posts here as well.

Update: It looks like I’ll be able to do one of the important settings in Terraform finally very soon.

No tag for this post.

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.