"az keyvault secret set" to set localDB connection string errors in powershell due to parenthesis in (localDB)

Samuel Alexander 31 Reputation points
2023-03-23T16:12:39.2333333+00:00

In powershell,

Using az keyvault secret set to set the localdb connection string as a key vault secret fails because of the closing parenthesis ")" that precedes the "\".

az keyvault secret set --name ConnectionString-messaging --vault-name kvtemptest --value "server=(LocalDb)\\MSSQLLocalDb;database=messaging;trusted_connection=yes;Pooling=false;"

the error I get is

\MSSQLLocalDb was unexpected at this time.

Tried the below too

(backtick) but same results az keyvault secret set --name ConnectionString-bwalk-messaging --vault-name kvtemptest --value "server=(LocalDb)\MSSQLLocalDb;database=Bwalk_Messaging;trusted_connection=yes;Pooling=false;" `

(doubling the braces) `az keyvault secret set --name ConnectionString-bwalk-messaging --vault-name kvtemptest --value "server=((LocalDb))\MSSQLLocalDb;database=Bwalk_Messaging;trusted_connection=yes;Pooling=false;"``

(enclosing single quotes)
az keyvault secret set --name ConnectionString-bwalk-messaging --vault-name kvsamtemptest --value "server='(LocalDb)\\MSSQLLocalDb';database=Bwalk_Messaging;trusted_connection=yes;Pooling=false;"

the command works gr8 in git bash console

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,041 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 43,926 Reputation points
    2023-03-24T12:27:36.82+00:00
    
    Hello there,
    
    The best way to do this is to add a Connection String from the Azure portal:
    
    From your Function App UI, click Function App Settings
    Settings / Application Settings
    Add connection strings
    
    They will then be available using the same logic as if they were on a web. config, e.g.
    
    var conn = System.Configuration.ConfigurationManager
                     .ConnectionStrings["MyConn"].ConnectionString;
    
    Hope this resolves your Query !!
    
    --If the reply is helpful, please Upvote and Accept it as an answer–
    
    0 comments No comments

  2. Shweta Mathur 27,301 Reputation points Microsoft Employee
    2023-03-30T12:28:35.6033333+00:00

    Hi @Samuel Alexander ,

    Thanks for reaching out.

    This is due to special characters in the command PowerShell such as () and backslashes which are interpreted by the shell and parse the command.

    You can use --% parameter in PowerShell also known as the "stop-parsing" which you use this to treats all remaining arguments on the command line as literal strings and does not try to parse or interpret them in any way.

    --% parameter is used to bypass PowerShell's parsing and interpretation of the command line, and pass the arguments directly to an external command.

    az --% az keyvault secret set --name ConnectionString-messaging --vault-name kvtemptest --value "server=(LocalDb)\\MSSQLLocalDb;database=messaging;trusted_connection=yes;Pooling=false;"
    
    
    

    Hope this will help.

    Thanks,

    Shweta

    Please remember to "Accept Answer" if answer helped you.

    0 comments No comments