0

I'm currently running into a bog-standard Bobby Tables problem, but the environment is Chef + Ruby + powershell.

All of the solutions I've seen so far appear inadequate: they surround the arguments with quotes, but do not fully escape the arguments. Shellwords and shellescape look promising, but they appear to be bash-specific.

For example, I may want to construct in Chef this windows shell command:

.\foo.exe BAR="#{node['baz']}"

Generalizing from the SQL dev world I'd naively anticipate an interface something like this:

cmd = "foo.exe BAR=?"
args = (node['baz'])
run-command(cmd, args)

Where run-command would handle escaping any arguments. Instead I see interfaces that remind me of the bad old SQL days when developers had to construct SQL as a string, and escape any arguments "by hand".

Any pointers to best practices on how to proceed? Use system? Thanks!

Edit: to be clear, the argument baz above can be expected to include arbitrary text, including possibly any combination of characters. Think of it as being identical to the Bobby Tables problem.

3
  • Most likely you don't need to try too hard. See if this article helps you. Commented Dec 8, 2015 at 18:40
  • Thanks for the link, @Bill_Stewart. Unfortunately that article seems to deal entirely with spaces in arguments, and its solution is to enclose them in double quotes. Commented Dec 8, 2015 at 19:51
  • You can use the showargs.exe command-line utility in the download to see the actual command line that PowerShell constructs. Commented Dec 8, 2015 at 21:44

1 Answer 1

1

The easiest generic solution is to use the array form for the execute resource's command property. This avoids all shell parsing an runs the command verbatim.

execute 'whatever' do
  command ['foo.exe', "BAR=#{node['baz']}"]
end

This doesn't work for script style resources though, which take a string for the script chunk to run, including powershell_script. There you would need something more tailored to PowerShell and I don't know its rules well enough to say if Shellwords would match.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.