Ran into an interesting problem today - a PowerShell script I’ve been using for deploying new releases into test environments started misbehaving.
Turned out the problem related to the folder names being used - our version numbers had just clicked over pass 999 and the wrong release was being pushed out.
A partial directory listing of the releases folder shows the problem clearly - note how the 1004 build doesn’t appear at the bottom of the list.
Most of you have probably already diagnosed the problem - an alphabetical ordering has been used instead of a numerical one.
The line of powershell looked like this:
After a brief consultation with Professor Google, I found an interesting post on sorting version numbers.
The key trick is that Powershell allows very simple casting from string values, coupled with the support of the Version class for comparisons.
Here’s the updated line of code:
And here’s how it works.
-
sort-object
does the sorting, based on a script block that provides a key for each folder -
$_.Name
returns the name of each folder -
.Split(" ")[1]
divides the name into two parts (either side of the space) and selects the later -
[Version]
converts the string into an instance of the version class, which is used for sorting.
With this result:
Sweet!
Comments
blog comments powered by Disqus