mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
Merge pull request #63591 from garethgreenaway/63590_cmd_run_powershell_fixes
cmd.run powershell fixes
This commit is contained in:
commit
2c1526a634
3 changed files with 86 additions and 13 deletions
1
changelog/63590.fixed
Normal file
1
changelog/63590.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
When the shell is passed as powershell or pwsh, only wrapper the shell in quotes if cmd.run is running on Windows. When quoted on Linux hosts, this results in an error when the keyword arguments are appended.
|
|
@ -256,6 +256,34 @@ def _check_avail(cmd):
|
|||
return bret and wret
|
||||
|
||||
|
||||
def _prep_powershell_cmd(shell, cmd, stack, encoded_cmd):
|
||||
"""
|
||||
Prep cmd when shell is powershell
|
||||
"""
|
||||
|
||||
# If this is running on Windows wrap
|
||||
# the shell in quotes in case there are
|
||||
# spaces in the paths.
|
||||
if salt.utils.platform.is_windows():
|
||||
shell = '"{}"'.format(shell)
|
||||
|
||||
# extract_stack() returns a list of tuples.
|
||||
# The last item in the list [-1] is the current method.
|
||||
# The third item[2] in each tuple is the name of that method.
|
||||
if stack[-2][2] == "script":
|
||||
cmd = (
|
||||
"{} -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command {}".format(
|
||||
shell, cmd
|
||||
)
|
||||
)
|
||||
elif encoded_cmd:
|
||||
cmd = "{} -NonInteractive -NoProfile -EncodedCommand {}".format(shell, cmd)
|
||||
else:
|
||||
cmd = '{} -NonInteractive -NoProfile -Command "{}"'.format(shell, cmd)
|
||||
|
||||
return cmd
|
||||
|
||||
|
||||
def _run(
|
||||
cmd,
|
||||
cwd=None,
|
||||
|
@ -368,19 +396,7 @@ def _run(
|
|||
# Else just run a Powershell command.
|
||||
stack = traceback.extract_stack(limit=2)
|
||||
|
||||
# extract_stack() returns a list of tuples.
|
||||
# The last item in the list [-1] is the current method.
|
||||
# The third item[2] in each tuple is the name of that method.
|
||||
if stack[-2][2] == "script":
|
||||
cmd = '"{}" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command {}'.format(
|
||||
shell, cmd
|
||||
)
|
||||
elif encoded_cmd:
|
||||
cmd = '"{}" -NonInteractive -NoProfile -EncodedCommand {}'.format(
|
||||
shell, cmd
|
||||
)
|
||||
else:
|
||||
cmd = '"{}" -NonInteractive -NoProfile -Command "{}"'.format(shell, cmd)
|
||||
cmd = _prep_powershell_cmd(shell, cmd, stack, encoded_cmd)
|
||||
|
||||
# munge the cmd and cwd through the template
|
||||
(cmd, cwd) = _render_cmd(cmd, cwd, template, saltenv, pillarenv, pillar_override)
|
||||
|
|
|
@ -1056,3 +1056,59 @@ def test_runas_env_sudo_group(bundled):
|
|||
popen_mock.call_args_list[0][0][0]
|
||||
== exp_ret
|
||||
)
|
||||
|
||||
|
||||
def test_prep_powershell_cmd():
|
||||
"""
|
||||
Tests _prep_powershell_cmd returns correct cmd
|
||||
"""
|
||||
with patch("salt.utils.platform.is_windows", MagicMock(return_value=False)):
|
||||
stack = [["", "", ""], ["", "", ""], ["", "", ""]]
|
||||
ret = cmdmod._prep_powershell_cmd(
|
||||
shell="powershell", cmd="$PSVersionTable", stack=stack, encoded_cmd=False
|
||||
)
|
||||
assert ret == 'powershell -NonInteractive -NoProfile -Command "$PSVersionTable"'
|
||||
|
||||
ret = cmdmod._prep_powershell_cmd(
|
||||
shell="powershell", cmd="$PSVersionTable", stack=stack, encoded_cmd=True
|
||||
)
|
||||
assert (
|
||||
ret
|
||||
== "powershell -NonInteractive -NoProfile -EncodedCommand $PSVersionTable"
|
||||
)
|
||||
|
||||
stack = [["", "", ""], ["", "", "script"], ["", "", ""]]
|
||||
ret = cmdmod._prep_powershell_cmd(
|
||||
shell="powershell", cmd="$PSVersionTable", stack=stack, encoded_cmd=False
|
||||
)
|
||||
assert (
|
||||
ret
|
||||
== "powershell -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command $PSVersionTable"
|
||||
)
|
||||
|
||||
with patch("salt.utils.platform.is_windows", MagicMock(return_value=True)):
|
||||
stack = [["", "", ""], ["", "", ""], ["", "", ""]]
|
||||
|
||||
ret = cmdmod._prep_powershell_cmd(
|
||||
shell="powershell", cmd="$PSVersionTable", stack=stack, encoded_cmd=False
|
||||
)
|
||||
assert (
|
||||
ret == '"powershell" -NonInteractive -NoProfile -Command "$PSVersionTable"'
|
||||
)
|
||||
|
||||
ret = cmdmod._prep_powershell_cmd(
|
||||
shell="powershell", cmd="$PSVersionTable", stack=stack, encoded_cmd=True
|
||||
)
|
||||
assert (
|
||||
ret
|
||||
== '"powershell" -NonInteractive -NoProfile -EncodedCommand $PSVersionTable'
|
||||
)
|
||||
|
||||
stack = [["", "", ""], ["", "", "script"], ["", "", ""]]
|
||||
ret = cmdmod._prep_powershell_cmd(
|
||||
shell="powershell", cmd="$PSVersionTable", stack=stack, encoded_cmd=False
|
||||
)
|
||||
assert (
|
||||
ret
|
||||
== '"powershell" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command $PSVersionTable'
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue