Update:
The answer below addresses your question as asked, based on your (seemingly incorrect) claim that the first command mentioned in your question worked as-is from cmd.exe
. The points made below generally apply to the syntax of arguments passed to commands in PowerShell.
Based on a later comment, you state that what solved your problem was to pass a JSON string to the aws
CLI's --tagging
parameter:
You state that '{\"TagSet\": [{\"Key\":\"public\",\"Value\":\"yes\"}]}'
worked for you (which represents the following verbatim JSON string:
{"TagSet": [{"Key":"public","Value":"yes"}]
).
- Here too it is the quoting (
'...'
) that makes the argument work, as discussed in the bottom section.
- As an aside: This strange need to manually
\
-escape "
characters embedded in an argument passed to external programs is indeed an - unfortunate - requirement in PowerShell up to at least v7.2.x, due to a long-standing bug. It may get fixed in a future version, which may require opt-in: see this answer.
However, apart from the PowerShell syntax problem that is the subject of your question, it looks like your original command simply used incorrect aws
shorthand syntax (a shorter alternative to passing JSON): according to the docs for the put-object-tagging
subcommand, the following should work - in addition to the required quoting ('...'
) for PowerShell's sake, note the [...]
around {...}
which were missing from your own original command:
'TagSet=[{Key=public,Value=yes}]'
Replace:
TagSet={Key=public,Value=yes}
with:
'TagSet={Key=public,Value=yes}'
That is, quote your argument, which prevents PowerShell from interpreting {...}
as a script block, which is what caused your problem.
Note the use of a '...'
, i.e a single-quoted verbatim string, which is the best choice if an argument contains no embedded variable references or subexpression (for the latter, you need string interpolation in the form of "..."
, i.e., a double-quoted expandable string).
PowerShell differs from other shells in two notable respects:
its escape character is `
(the so-called backtick), not \
(as in POSIX-compatible shells such as Bash) or ^
(as in cmd.exe
)
it has more metacharacters than other shells, such as {
, }
, and @
As for the specific error message you saw:
The content of a script block ({ ... }
) is parsed as PowerShell code, and the content in your case, Key=public,Value=yes
, is parsed as a command, whose syntax happens to be invalid:
Key=public
is interpreted as a command name
,Value=yes
is parsed as its argument, which - due to the ,
- is parsed as an array, but a literal array passed as an argument must not start with ,
, which is what the error message is trying to say: it's missing an array element before ,
You can more easily provoke this error by submitting the following on the command line:
foo , bar # -> Error "Missing argument in parameter list."