Pass Dynamic Flags to NuGet With PowerShell Splatting

While working on a PowerShell script for packaging multiple NuGet packages, I discovered a peculiarity in calling NuGet with PowerShell splatting. I wanted to do something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Function Package-Project(
    [string]$folder,
    [switch]$build,
    [switch]$symbols
) {
    pushd $folder
    $nugetArgs = @{Properties="Configuration=$configuration"}
    if($build) { $nugetArgs.Build=$True }
    if($symbols) { $nugetArgs.Symbols=$True }
    nuget pack @nugetArgs
    popd
}

Package-Project ProjectA
Package-Project ProjectB

Even though I’ve “Splatted” $nugetArgs into my NuGet call, the way NuGet handles arguments causes it to choke on the HashSet of arguments, and throw an error like the following:

1
2
3
4
5
6
nuget : Unknown option: '-Build:True'
At line:1 char:38
+ nuget pack $project $nugetArgs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Unknown option: '-Build:True':String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Digging into the source code, I found that they have a pretty customized argument parser, which seems to make passing a HashSet pretty much impossible. (If I’m wrong, leave a comment!)

Fortunately, there’s a fairly reasonable workaround: Pass an array of arguments rather than a HashSet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Function Package-Project(
    [string]$folder,
    [switch]$build,
    [switch]$symbols
) {
    pushd $folder
    $nugetArgs = "-Properties", "Configuration=$configuration"
    if($build) { $nugetArgs += "-Build" }
    if($symbols) { $nugetArgs += "-Symbols" }
    nuget pack @nugetArgs
    popd
}

Package-Project ProjectA
Package-Project ProjectB

So there you have it. Use a list of parameter names and values rather than a HashSet when dynamically invoking NuGet. This is actually somewhat intuitive from a classic CMD standpoint, but not so much from a PowerShell perspective.

I am now accepting new clients for part-time consulting and software development projects. Learn more

I haven't configured comments for this blog, but if you want to get in touch, you can find me on Twitter