Remembering those IT practioners that have served

Today we observe Memorial Day in the United States. I happened to have spent part of my Memorial Day returning from a *minication* in Dallas, Texas. On my flight home, it was announced that a female WWII veteran who served in the WASP program was flying with us. Since flight time is often blogging time for me, I decided to write this article to express my thanks to all of those who have served the United States in uniform. Thank you to all of you!  I also wanted to use this space to thank those who at one point served in the US military who are now civilian programmers, sysadmins or DBAs.

Naturally the list must start off with Rear Admiral Grace Hopper, responsible amongst other things for COBOL and the term debugging. Honorable mentions must follow for the sci-fi writer Robert A Heinlein and his wife Virginia. While not programmers by profession, they were both veterans and inspirations to many in our field. Although the three of them have sailed beyond the sunset, I am thankful for their service.

In addition, one cannot forget those that have served and are still living. Microsoftie and University of Washington Professor Buck Woody served in the Air Force, as did Audrey Hammonds, half of datachix. Thank you both for your service!

Finally, a special thanks to those who serve in a civilian IT role while still in the active reserves like Jonathan Kehayias of SQLSkills, better know as SQLPoolboy to some. Thank you Jonathan, and those like you for your continuing service.

This list is by no means exhaustive, just the people I could think of. I invite everyone to write in the comments below a personal message of thanks to fellow programmers, sysadmins and dbas that have served in the US military.

Using MongoDB in Powershell

Readers of this blog know that I’ve been using MongoDB for a while, and I’ve recently become very excited about Powershell. Well recently I’ve been able to combine the two together for pure dynamically typed, schema-less, non-relational awesomeness. Such awesomeness is begging to be shared.

To get started you will need version 1.1 of the official 10gen CSharp driver for Mongodb. At the time of this writing 1.1 has not been released, so your going to have to pull the latest version from github and compile it yourself. The minimal revision of the trunk you will need is this one. First download and install the MSI (or download the source, build the MSI and install it). Then open up your favorite text editor or Powershell IDE. Personally I am a fan of Powershell ISE or plain old vim, with some configuration modifications. Now we will load the bson and driver DLLs via the Add-Type cmdlet.

# Check to see if we are running the 64 bit version of Powershell.
# See
if ([intptr]::size -eq 8) {
    $mongoDriverPath = (Get-ItemProperty "HKLM:SOFTWAREWow6432NodeMicrosoft.NETFrameworkv3.5AssemblyFoldersExMongoDB CSharpDriver 1.0").'(default)';
else {
    $mongoDriverPath = (Get-ItemProperty "HKLM:SOFTWAREMicrosoft.NETFrameworkv3.5AssemblyFoldersExMongoDB CSharpDriver 1.0").'(default)';
Add-Type -Path "$($mongoDriverPath)MongoDB.Bson.dll";
Add-Type -Path "$($mongoDriverPath)MongoDB.Driver.dll";

Since the Csharp Driver MSI is 32 bits, it creates the registry entries in the Wow6432Node. Therefore, we have to check to see if we are running in the 32 or 64 bit version of Powershell . Credit to an anonymous commenter on the msgoodies blog for providing this size of a pointer trick to determine if you are running a 32 or 64 bit system.

The next thing we want to do is to create a BSON document. This is surprisingly easy.

[MongoDB.Bson.BsonDocument] $doc = @{
    "_id"= [MongoDB.Bson.ObjectId]::GenerateNewId();
    "FirstName"= "Justin";
    "LastName"= "Dearing";
    "PhoneNumbers"= [MongoDB.Bson.BsonDocument] @{
        'Home'= '718-555-1212';
        'Mobile'= '646-555-1212';

As you can see Powershell can convert a HashTable to a BsonDocument. This is because of the public constructor BsonDocument(IDictionary hashTable). Powershell can use these one parameter constructors to cast an object. You can use the same Hashtable trick for the QueryDocument and UpdateDocument classes.

Now that we have our BsonDocument, its time to perform basic crud operations.

$db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/powershell');
$collection = $db['example1'];
Write-Host "Insert";

$updates = @{'$set' = @{'email'= ''}};
$query = @{"_id"= $doc['_id']}

Write-Host "Update";
$collection.Update([MongoDB.Driver.QueryDocument]$query, [MongoDb.Driver.UpdateDocument]$updates);

Write-Host "Delete";

This gives us the output below.

_id                                                         4dc5e5a43b42bd41d0154679
LastName                                                    Dearing
PhoneNumbers                                                {Mobile=646-555-1212, Home=718-555-1212}
FirstName                                                   Justin

PS E:srcgistgist-854911> powershell.exe -file .MongoTest.ps1


Name                                                        Value
----                                                        -----
_id                                                         4dc636c33b42bd3d7c8dc3a4
LastName                                                    Dearing
PhoneNumbers                                                {Mobile=646-555-1212, Home=718-555-1212}
FirstName                                                   Justin
FirstName                                                   Justin
LastName                                                    Dearing
PhoneNumbers                                                {Mobile=646-555-1212, Home=718-555-1212}
_id                                                         4dc636c33b42bd3d7c8dc3a4

As you can see, its not very hard to use the 10Gen MongoDB Csharp driver from within Powershell. Using Powershell with the MongoDb C-Sharp driver has many possibilities. First of all, adhoc mongodb queries from inside of powershell. Secondly, The code for this example is available in its entirety here.

Setx where have you been all my life?

For me, the reference implementation of mixed emotions is the combination of anger, relief, joy, and frustration when “Why isn’t there a way to do X?!” becomes “How come no one ever told me about Y?!” This past Friday, I got to experience that entire bag of emotion thanks to setx.exe.

Setx (technetss64) is a command line utility that sets environmental variables permanently on windows. This behavior is distinct from the set command (technetss64) which only affects the current cmd.exe session. To clarify, there are three levels of environment variables:

  • Machine level All users on a given machine see these
  • User Level Each individual user on a system has a set of these variables
  • Session Level When you actually spawn a cmd.exe process, it allows you to have a set of transient variables for the session

Now, until I knew about setx, I had two ways of setting environment variables permanently. The first was to go through several layers of the windows GUI. The second, preferable to me, method was to use PowerShell as I illustrate elsewhere on this blog. However, that method requires a lot of keystrokes or some aliasing. Setx however simplifies the syntax quite nicely.

One thing to note about setx, as per ss64, it is available on windows 7 and through the resource kits. If your windows installation does not have setx.exe, try installing a resource kit.