1

I am running this from PowerShell, so I'm not sure if this would happen in a different environment (the regex itself just includes everything except for those starting with 'amq.').

When I run the following command, the '^' is dropped from the pattern:

rabbitmqctl set_policy mirroring '^(?!(?:amq\.)).+$' '{"""ha-mode""":"""all"""}'

It outputs the following:

Setting policy "mirroring" for pattern "(?!(?:amq\\.)).+$" to "{\"ha-mode\":\"all\"}" with priority "0" ...

I've tried escaping it in different ways without any success. Interestingly, setting it from the web management plugin works fine.

UPDATE

The issue seems to be related to the ^ being a reserved character in batch files (I think), and not being quoted as far as the batch file is concerned (i.e. PS needs to pass in a quoted string for the pattern, but it was passing in the regex as-is). Any of the following work, based on beatcracker's answer:

rabbitmqctl set_policy mirroring '"^(?!(?:amq\.)).+$"' '{\"ha-mode\":\"all\"}'

rabbitmqctl --% set_policy mirroring "^(?!(?:amq\.)).+$" {\"ha-mode\":\"all\"}

rabbitmqctl @('set_policy','mirroring','"^(?!(?:amq\.)).+$"','{\"ha-mode\":\"all\"}')

Start-Process -FilePath 'rabbitmqctl' -ArgumentList 'set_policy mirroring "^(?!(?:amq\.)).+$" {\"ha-mode\":\"all\"}'
2

PS 2.0 and higher:

If you specify an array of values, it will automatically expand them into separate parameters.

& rabbitmqctl @('set_policy mirroring', '^(?!(?:amq\.)).+$', '{ha-mode:all}')

Using Start-Process cmdlet.

Start-Process -FilePath 'rabbitmqctl' -ArgumentList 'set_policy mirroring ^(?!(?:amq\.)).+$ {ha-mode:all}'

# Same as above, shortened using aliases and positional parameters

start rabbitmqctl 'set_policy mirroring ^(?!(?:amq\.)).+$ {ha-mode:all}'
saps rabbitmqctl 'set_policy mirroring ^(?!(?:amq\.)).+$ {ha-mode:all}'

PS 3.0 and higher:

...you can use the --% operator which tells PowerShell to stop parsing from that point onward until the end of the line. Everything from that operator onwards is parsed by the parser used by the program.

rabbitmqctl --% set_policy mirroring ^(?!(?:amq\.)).+$ {ha-mode:all}

Reference:

  • These didn't work as-is, but the issue was a result of how PS was passing in the args to rabbitmqctl so I'll mark it as the answer. I edited my question with the commands that worked for me. It is actually possible to run the command without passing an array, using Start-Process, or using --%. – Andrew Oct 29 '15 at 21:54
  • @Andrew Thanks. Unfortunately, I can't test it with rabbitmqctl, but echoargs.exe shows arguments as they should be. It probably has something to do with the way rabbitmqctl parses command line, because cmd should not be involved when PS passes arguments. – beatcracker Oct 29 '15 at 22:00
  • No problem. It got me on the right track. I updated my question with all the versions that worked for me. – Andrew Oct 29 '15 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.