mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Refactor common args used for building PowerShell commands.
This commit is contained in:
parent
0c938562a7
commit
7e8cc65829
2 changed files with 9 additions and 4 deletions
|
@ -127,7 +127,7 @@ class Connection(object):
|
||||||
vvvv("WINRM PARTS %r" % cmd_parts, host=self.host)
|
vvvv("WINRM PARTS %r" % cmd_parts, host=self.host)
|
||||||
# For script/raw support.
|
# For script/raw support.
|
||||||
if cmd_parts and cmd_parts[0].lower().endswith('.ps1'):
|
if cmd_parts and cmd_parts[0].lower().endswith('.ps1'):
|
||||||
script = 'PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File ' + ' '.join(['"%s"' % x for x in cmd_parts])
|
script = powershell._build_file_cmd(cmd_parts)
|
||||||
cmd_parts = powershell._encode_script(script, as_list=True)
|
cmd_parts = powershell._encode_script(script, as_list=True)
|
||||||
try:
|
try:
|
||||||
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
||||||
|
|
|
@ -22,6 +22,8 @@ import random
|
||||||
import shlex
|
import shlex
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
_common_args = ['PowerShell', '-NoProfile', '-NonInteractive']
|
||||||
|
|
||||||
def _escape(value, include_vars=False):
|
def _escape(value, include_vars=False):
|
||||||
'''Return value escaped for use in PowerShell command.'''
|
'''Return value escaped for use in PowerShell command.'''
|
||||||
# http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
|
# http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
|
||||||
|
@ -39,11 +41,15 @@ def _escape(value, include_vars=False):
|
||||||
def _encode_script(script, as_list=False):
|
def _encode_script(script, as_list=False):
|
||||||
'''Convert a PowerShell script to a single base64-encoded command.'''
|
'''Convert a PowerShell script to a single base64-encoded command.'''
|
||||||
encoded_script = base64.b64encode(script.encode('utf-16-le'))
|
encoded_script = base64.b64encode(script.encode('utf-16-le'))
|
||||||
cmd_parts = ['PowerShell', '-NoProfile', '-NonInteractive', '-EncodedCommand', encoded_script]
|
cmd_parts = _common_args + ['-EncodedCommand', encoded_script]
|
||||||
if as_list:
|
if as_list:
|
||||||
return cmd_parts
|
return cmd_parts
|
||||||
return ' '.join(cmd_parts)
|
return ' '.join(cmd_parts)
|
||||||
|
|
||||||
|
def _build_file_cmd(cmd_parts):
|
||||||
|
'''Build command line to run a file, given list of file name plus args.'''
|
||||||
|
return ' '.join(_common_args + ['-ExecutionPolicy', 'Unrestricted', '-File'] + ['"%s"' % x for x in cmd_parts])
|
||||||
|
|
||||||
class ShellModule(object):
|
class ShellModule(object):
|
||||||
|
|
||||||
def env_prefix(self, **kwargs):
|
def env_prefix(self, **kwargs):
|
||||||
|
@ -75,8 +81,7 @@ class ShellModule(object):
|
||||||
cmd_parts = shlex.split(cmd, posix=False)
|
cmd_parts = shlex.split(cmd, posix=False)
|
||||||
if not cmd_parts[0].lower().endswith('.ps1'):
|
if not cmd_parts[0].lower().endswith('.ps1'):
|
||||||
cmd_parts[0] = '%s.ps1' % cmd_parts[0]
|
cmd_parts[0] = '%s.ps1' % cmd_parts[0]
|
||||||
cmd_parts = ['PowerShell', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-File'] + ['"%s"' % x for x in cmd_parts]
|
script = _build_file_cmd(cmd_parts)
|
||||||
script = ' '.join(cmd_parts)
|
|
||||||
if rm_tmp:
|
if rm_tmp:
|
||||||
rm_tmp = _escape(rm_tmp)
|
rm_tmp = _escape(rm_tmp)
|
||||||
script = '%s; Remove-Item "%s" -Force -Recurse;' % (script, rm_tmp)
|
script = '%s; Remove-Item "%s" -Force -Recurse;' % (script, rm_tmp)
|
||||||
|
|
Loading…
Reference in a new issue