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 |
|
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 |
|
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 |
|
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.