4

Django 1.8, Heroku, Powershell.

I'm trying to set an Environment Variable for my django secret key in Heroku:

(venv) PS WORKFOLDER> heroku config:set SECRET_KEY=eoik6-&dnr9elgmrt7-%3hu_&37$3hg!9c6x!^khjr3!z*z&b4

I'm getting this error msg (3 times - since I have 3 ampersands in the string):

At line:1 char:77
+ heroku config:set SECRET_KEY=eoik6-&dnr9elgmrt7-%3hu_&37$3hg!9c6x!^khjr3!z*z&b4
+                                                                             ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

When I tried the suggested solution (to put double quotes around the ampersands):

(venv) PS WORKFOLDER> heroku config:set SECRET_KEY=eoik6-"&"dnr9elgmrt7-%3hu_"&"37$3hg!9c6x!^khjr3!z*z"&"b4

I got this error:

SECRET_KEY: eoik6-
'dnr9elgmrt7-%3hu_' is not recognized as an internal or external command, operable program or batch file.
'37$3hg!9c6x!^khjr3!z*z' is not recognized as an internal or external command, operable program or batch file.
'b4' is not recognized as an internal or external command, operable program or batch file.

I also tried escaping with slash, putting quotes around the whole string, etc. Same result. So how can I set my environment variable?

woodduck
  • 339
  • 2
  • 12

3 Answers3

6

I just tried a few things and it works when you put single quotes around the whole VALUE (and double quotes around the ampersands):

heroku config:set SECRET_KEY='eoik6-"&"dnr9elgmrt7-%3hu_"&"37$3hg!9c6x!^khjr3!z*z"&"b4'

verifiable by the command heroku config

woodduck
  • 339
  • 2
  • 12
1

I'm not familiar with Heroku, but assuming that heroku is executable and config:set SECRET_KEY=eo... is a commandline argument, you could try this:

& 'heroku' @('config:set', 'SECRET_KEY=eoik6-"&"dnr9elgmrt7-%3hu_"&"37$3hg!9c6x!^khjr3!z*z"&"b4')
beatcracker
  • 6,714
  • 1
  • 18
  • 41
  • I just tried it. That works too! Thanks! Seems our answers overlapped. – woodduck Aug 21 '15 at 01:31
  • 1
    @woodduck Thanks, PS is somewhat tricky with external executables and commandline parameters, see [this answer](http://stackoverflow.com/a/29565317/4424236) to understand why this code works. – beatcracker Aug 21 '15 at 02:36
0

There is one more character that can cause issues, ')'. It produces a similar error: x was unexpected at this time. Fortunately, the fix is exactly the same as with the ampersand - simply putting double quotes around it solves the issue. Sample working solution:

heroku config:set SECRET_KEY='7d"("5r9"^"dfghfd=fsdghw%9$")"zz")"yer68"^"346k-4_qndr%!c4h=r'

Edit: another problematic character, '^'. It's harder to spot since powershell simply omits it from the SECRET_KEY. It needs to be surrounded by double quotes too.

Domin
  • 65
  • 7