4

Unable to run the following Bash/Zsh command in Powershell:

$KeyPath = Join-Path -Path $this.Plate -ChildPath "install/tekton.key"
kubectl create secret docker-registry regcred `
    --docker-server="https://gcr.io" `
    --docker-username=_json_key `
    --docker-email="name@org.iam.gserviceaccount.com" `
    --docker-password="$(cat $KeyPath)"

I get error:

error: exactly one NAME is required, got 5
See 'kubectl create secret docker-registry -h' for help and examples

If I run this command directly in bash it works:

kubectl create secret docker-registry regcred --docker-server="https://gcr.io" --docker-username=_json_key --docker-email="name@org.iam.gserviceaccount.com" --docker-password="$(cat ./tekton.key)"
Chris G.
  • 23,930
  • 48
  • 177
  • 302

1 Answers1

2

I don't know if it's the cause of your problem, but there are two potential problems:

  • An expanded value that contains spaces causes PowerShell to double-quote the argument as a whole when it rebuilds the command line behind the scenes (on Windows):

    • For instance, if $(cat $KeyPath) ($(Get-Content $KeyPath)) expands to one two, PowerShell passes "--docker-password=one two" behind the scenes, not --docker-password="one two".

    • Whether this changes the meaning of the argument depends on how the target program parses its command line - I don't know what kubectl does.

      • If you do need to address this, escape the enclosing " (double quotes) with `` ``` (the backtick, PowerShell's escape character to make PowerShell pass your argument in the original syntax form:

        • --docker-password=`"$(cat ./tekton.key)`"
      • Note that - unlike in POSIX-like shells such as Bash and Zsh - you normally do not enclose a variable reference or subexpression in "..." in order to pass it through safely; e.g., --foo=$someVar or --foo=$(Get-Date) work fine, even if $someVar or the output from Get-Date contains spaces or wildcard characters.

  • If file $KeyPath contains multiple lines, the lines are concatenated with spaces in the argument:

    • For instance, if the file contains "a`nb`n" ("`n" being a newline), PowerShell will pass
      "--docker-password=a b".

    • By contrast, POSIX-like shells such as Bash or Zsh will preserve the interior newlines, while trimming (any number of) trailing ones.


On a side note: PowerShell's handling of embedded double-quoting in arguments passed to external programs is broken - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775