Tuesday, October 18, 2011

Switch/Flag Parameters in PowerShell

I wanted to create a function with a ‘-force’ option.  First, I tried to add a [boolean] parameter, but that doesn’t work because it needs a value with the argument; I want it to be invoked with just the presence of the switch/flag.  Here’s how I finally did it:

function Update-Something {
param(
[switch] $force)

if ($force) {
#do something here
}
}


# use like:
$> Update-Something -force

Friday, October 14, 2011

DotSpatial Accepted Pull Request!

Tooting my own horn: the DotSpatial project accepted a pull request of mine for nuspec and build helper files -- woot!

Thursday, October 13, 2011

Rounding In PowerShell

Another simple one (from John D. Cook):

$> [int] 1.1
1

$> [int] 1.5
2

$> [int] 1.8
2

PowerShell: Parse Date from String

Incredibly simple (checkout this PowerShell Cookbook):
$> (get-date 2000/01/01)

Saturday, January 01, 2000 12:00:00 AM

Thursday, October 6, 2011

NuGet Without Committing Packages

I like NuGet.  I’m using it to distribute the #foo REST project.  I had this idea that it would be nice NOT to check in package binaries to source control.  As usual, I’m not the first one with that idea.  God bless all the people that are smarter than me:
PM> Enable-PackageRestore
Attempting to resolve dependency 'NuGet.CommandLine (≥ 1.4)'.
Successfully installed 'NuGet.CommandLine 1.4.20615.182'.
Successfully installed 'NuGet.Build 0.16'.

Copying nuget.exe and msbuild scripts to D:\Code\StarterApps\Mvc3Application\.nuget
Successfully uninstalled 'NuGet.Build 0.16'.
Successfully uninstalled 'NuGet.CommandLine 1.4.20615.182'.

Don't forget to commit the .nuget folder
Updated 'Mvc3Application' to use 'NuGet.targets'
Enabled package restore for Mvc3Application

Wednesday, October 5, 2011

CodePlex and Windows Live Writer

I’m working on a simple POCO message based service layer to sit on top of ASP.NET MVC.  The project (not yet published) is/will be hosted at CodePlex.  As part of this blog, I discovered Windows Live Writer and have been happy with it.  I am even more happy that it works with CodePlex wiki pages.  And I’m giddy that you can use the Insert Code plugin to add code snippets without leaving Live Writer (don’t be freaked out that the last release was in 2006…).

As an aside, I use the more robust SyntaxHighlighter with the PreCode Live Writer plugin for this blog.  I use version 2.x so that the toolbar buttons still show.  There are publicly hosted source files for SyntaxHighlighter, if you’re interested.  I basically then followed the install instructions, substituting the 2.x hosted file URLs where needed.

Tuesday, October 4, 2011

PowerShell Format-List (fl)

Staying with the late to the party theme on my PowerShell adventure, today I discovered Format-List (fl alias).  You can pipe results into it and get more detailed output.

Without Format-List:
PS> [Array]

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Array System.Object
With Format-List:
PS> [Array] | fl

Module : CommonLanguageRuntimeLibrary
Assembly : mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7
7a5c561934e089
TypeHandle : System.RuntimeTypeHandle
DeclaringMethod :
BaseType : System.Object
UnderlyingSystemType : System.Array
FullName : System.Array
AssemblyQualifiedName : System.Array, mscorlib, Version=4.0.0.0, Culture=neutral, Pub
licKeyToken=b77a5c561934e089
Namespace : System
GUID : 200fb91c-815d-39e0-9e07-0e1bdb2ed47b
IsEnum : False
GenericParameterAttributes :
IsSecurityCritical : False
IsSecuritySafeCritical : False
IsSecurityTransparent : True