postgresqlpowershellshellquotingazure-cloud-shell

Connect to Postgres Flexible Server via "az postgress" from PS1 Script when Password has characters needing escapes


I want to run a command on Azure Postgres Flexible Server via a PS1 (PSCore) script. The password can contain characters like < and ) that the PSCore parser chokes on.

I'm attempting to use the az postgres flexible-server execute command. I'm thinking this is more of a PS1 script issue than a Postgres or az postgres issue.

Example command:

az postgres flexible-server execute -n the-instance-name -u theName -p the%WeIRDbutSecure#PasswordE<JMCiOw<>IQ) -d theDatabase -q "select * from MyTable"

I get an error like:

ParserError:
Line |
   1 |  … he-instance-name -u theName -p the%WeIRDbutSecure#PasswordE<JMCiOw<>IQ) -d theDatabase  …
     |                                                                          ~
     | Unexpected token ')' in expression or statement.

I've tried assigning the%WeIRDbutSecure#PasswordE<JMCiOw<>IQ) to a local variable. That moved the problem to:

> was unexpected at this time.

I've tried breaking placing the entire string in double quotes as well as single quotes. Neither works -- same > was unexpected at this time. error. I've also tried embedding single backtick characters within the string, as in:

the%WeIRDbutSecure#PasswordE`<JMCiOw`<`>IQ`)

but continue to get the same > was unexpected at this time. error.

My ultimate goal is to embed this command-line command in a PS1 script that I execute later.

Has anyone ever been able to pass a -p password that contained characters that PS1 would like to interpret at directives? How did you escape these characters or the entire password?


Solution

  • Thanks @mklement0 for the great links. That was a set of good reads to refamiliarize myself with.

    It turned out in my case that I needed to surround the password with " characters.

    I failed to mention that I am calling az postgres flexible-server execute in a PS1 script that is itself called from another PS1 script, all running in pwsh.

    Ultimately what worked was, basically:

    # $rawPW is the raw password from a KV Secret or elsewhere
    $p = """" + $rawPW + """"
    ...
    az postgres flexible-server execute -n the-instance-name -u theName -p $p -d theDatabase -q "select * from MyTable"