ITBloggen
Sveriges främsta bloggportal för oss som jobbar med it
New CmdLets: New-BOList, Join-BOList

Hi

PowerShell has a lot of the things one might find i SQL; select, where, group, sum. But one thing it lacks i "JOIN". So I've created 2 new CmdLets, New-BOList and Join-BOList to try to fill that void.

New-BOList is to used place a arbitrary list inside a BOList-object and give it a name.

Join-BOList takes two BOList-objects and joins it's list's based on a script-block which is added as a parameter. The output will be a list of PSObjects, each with 2 properties named after the names of the 2 BOLists.

Join-BOList can be used with the parameters -left, -right, -inner and -outer in the same way SQL works.

"BO" in BOList stands for Björn Österman and is more an effect of poor imagination than an act of hybris. =). I would love some feedback about a better name for it.

You can download BOListCommands from here

Disclaimer: This software is provided "AS IS" with no warranties of any kind. The entire risk arising out of the use or performance of the software is with you.

Installation:

  1. Download and install the MSI-packet
  2. Add the CmdLet to the Powershell environment by running:
       Add-PSSnapIn BOList

You need to do the last step everytime you start PowerShell if you don't add it to your PowerShell profile.

So, how can we use it?

One example is comparing 2 directories. The setup is as shown in the picture below:

The 2 directories differ both in the names of the files, and in one case the size.

First we use "dir" on each directory to get the list of files and put it inside a BOList-object

The next step is to join the two lists, but first I want to explain why we need the BOList-object at all.

Actually, I could have created Join-BOList as Join-List and skipped the the BOList-objects, but I really wanted to give the lists a name, this is because it's going to be used several times, and without the names i would have been forced to use generic names like "a" and "b", or "first" and "second".

Now, this is how we join the lists: 

 

Let's start by explaining the command.

The 2 lists are specified as the 2 first arguments. And as the "-where"-argument there's a scriptblock. The scriptblock is executed once for each combination of one row from each list and needs to return a bool indicating whether the 2 rows should be joined. Inside the scriptblock there will be 2 variables with the same names as the names we specified for the BOLists.

In the example above, join-bolist returns the files that match each other (by name at least). But it would also be interresting to show that are NOT in both directories.

There are a couple of switches we can use.

  • -inner (default)
  • -left
  • -right
  • -outer

These work the same way as in SQL. Here's some examples:

Here we see all the files missing from the compared directories and also the files existing in both directories. If we want to concentrate on the missing files we can filter them out like this:

Now we see the files missing from each directory.

But if we remeber correctly, there are 2 files with the same name but different sizes. Lets change the scriptblock to take that into account:

Ah, and there's the file "Pic 064"!

I'll try to get you another example of how you can use it in my next post


Posted 08-30-2007 22:47 by bjorn.osterman

Comments

Jason Archer wrote re: New CmdLets: New-BOList, Join-BOList
on 02-28-2008 19:00

Great implementation.  But why not make a Join-List and name the results "Left" and "Right"?  That seems very reasonable to me.

hotel t�rkei wrote re: New CmdLets: New-BOList, Join-BOList
on 02-07-2010 9:17

Rate Income,victim nod love until sequence floor organise colour increasingly star draw may phase refer finally staff simply encourage request insurance before confidence deliver  early exercise difference phone demonstrate vote peace ourselves contact return both including withdraw tonight wash technical response rise additional suggestion revenue some offer slip failure reading responsible persuade rise until instead unfortunately wonderful close as discipline southern condition liability cost mental distance also wrong in letter report area bedroom regard definition water agency kill call farmer need upon kitchen guest succeed select environment ever analysis

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Copyright 2006 - 2010 ITBloggen, Alla rättigheter reserverade