Forum Discussion
Run Start-Process with -ArgumentList when an argument also contains a -a
Hi, Joe.
You are correct that "-a" is being interpreted as "-ArgumentList" and therefore considered as the second instance of -ArgumentList within that command. Confirmation can be found here:
If you look at the -ArgumentList parameter definition above, ArgumentList is meant to be an array of strings, which could be expressed as either of the following:
# Strictly correct.
Start-Process "somepath\glcm.exe" -ArgumentList @("productId", '-a "key"');
# "Shorthand" (barely).
Start-Process "somepath\glcm.exe" -ArgumentList "productId", '-a "key"';
However, if you ignore the comma separating the array elements then it is not an array at all. In such a scenario, PowerShell interprets whatever values beyond the first as parameters belonging to the calling commandlet, and that's where "-a" pattern matches "-ArgumentList".
To avoid this, use commas to separate the parameters as well as enclosing single parameters that contain spaces (such as '-a "key"') in quotes (note that double and single quotes have different behaviours in PowerShell, though I won't go into that here) as shown in the above examples.
By using the comma separator, PowerShell will treat all "joined" values as part of the ArgumentList string array.
I would also encourage you to use named parameters rather than positional parameters, as the latter is more prone to breaking between platforms or when the commandlet itself is updated in such a way that the parameter ordering changes. While this is incredibly rare, it's also entirely avoidable for practically no extra effort.
For example:
# Using named parameters instead of positional parameters, along with strict parameter specification.
Start-Process -FilePath "somepath\glcm.exe" -ArgumentList @("productId", '-a "key"');
Cheers,
Lain
- jfkruegerOct 14, 2024Copper ContributorThank you so much for the reply, what you say does make sense. I did try your last example and I get the same error:
Run powershell Start-Process -FilePath "$env:PROGRAMDATA\GrapeCity\gclm\gclm.exe" -ArgumentList @("product-id", '-a "License-Key"');
Start-Process : Cannot bind parameter because parameter 'ArgumentList' is specified more than once. To provide
multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter
value1,value2,value3".
At line:1 char:115
+ ... clm.exe -ArgumentList 742a5cf8-2204-4873-9dc9-ff56d67411bd -a "065042 ...
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : ParameterAlreadyBound,Microsoft.PowerShell.Commands.StartProcessCommand
Error: Process completed with exit code 1.- jfkruegerOct 14, 2024Copper Contributor
From looking at the error message, I tried this command (wrapping the -ArgumentList in quotes):
powershell Start-Process -FilePath "$env:PROGRAMDATA\GrapeCity\gclm\gclm.exe" "-ArgumentList 'product-id', '-a licensekey'";
And it works!
Thank you for your help!
- LainRobertsonOct 15, 2024Silver Contributor
Hi, Joe.
I'm glad it's working - that's all that matters, but there's some inconsistencies here.
From the second last post, the error's showing that there is no comma between the product key and the -a, which doesn't reflect the command you mentioned running:
Next, enclosing the entire ArgumentList component in double quotes causes everything - including the "-ArgumentList" to be treated as a single parameter to gclm.exe. This seems not to be an issue for gclm.exe, but it's important to understand what's going on as there will be other times this strategy won't work.
Here's an example, where I'm using the command prompt in lieu of gclm.exe.
Output
Here, you can see the whole string's been passed in as the parameter.
Cheers,
Lain