summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/plugins')
-rw-r--r--lib/ansible/plugins/connections/winrm.py2
-rw-r--r--lib/ansible/plugins/shell/powershell.py45
2 files changed, 8 insertions, 39 deletions
diff --git a/lib/ansible/plugins/connections/winrm.py b/lib/ansible/plugins/connections/winrm.py
index 28bac3285a..0e19b93ac2 100644
--- a/lib/ansible/plugins/connections/winrm.py
+++ b/lib/ansible/plugins/connections/winrm.py
@@ -167,7 +167,7 @@ class Connection(ConnectionBase):
elif '-EncodedCommand' not in cmd_parts:
script = ' '.join(cmd_parts)
if script:
- cmd_parts = self._shell._encode_script(script, as_list=True, strict_mode=False)
+ cmd_parts = self._shell._encode_script(script, as_list=True)
if '-EncodedCommand' in cmd_parts:
encoded_cmd = cmd_parts[cmd_parts.index('-EncodedCommand') + 1]
decoded_cmd = to_unicode(base64.b64decode(encoded_cmd))
diff --git a/lib/ansible/plugins/shell/powershell.py b/lib/ansible/plugins/shell/powershell.py
index aba3183e76..0e16d34e16 100644
--- a/lib/ansible/plugins/shell/powershell.py
+++ b/lib/ansible/plugins/shell/powershell.py
@@ -112,41 +112,12 @@ class ShellModule(object):
cmd_parts.insert(0, '&')
elif shebang and shebang.startswith('#!'):
cmd_parts.insert(0, shebang[2:])
- script = '''
- Try
- {
- %s
- }
- Catch
- {
- $_obj = @{ failed = $true }
- If ($_.Exception.GetType)
- {
- $_obj.Add('msg', $_.Exception.Message)
- }
- Else
- {
- $_obj.Add('msg', $_.ToString())
- }
- If ($_.InvocationInfo.PositionMessage)
- {
- $_obj.Add('exception', $_.InvocationInfo.PositionMessage)
- }
- ElseIf ($_.ScriptStackTrace)
- {
- $_obj.Add('exception', $_.ScriptStackTrace)
- }
- Try
- {
- $_obj.Add('error_record', ($_ | ConvertTo-Json | ConvertFrom-Json))
- }
- Catch
- {
- }
- Echo $_obj | ConvertTo-Json -Compress -Depth 99
- Exit 1
- }
- ''' % (' '.join(cmd_parts))
+ catch = '''
+ $_obj = @{ failed = $true; $msg = $_ }
+ echo $_obj | ConvertTo-Json -Compress -Depth 99
+ Exit 1
+ '''
+ script = 'Try { %s }\nCatch { %s }' % (' '.join(cmd_parts), 'throw')
if rm_tmp:
rm_tmp = self._escape(self._unquote(rm_tmp))
rm_cmd = 'Remove-Item "%s" -Force -Recurse -ErrorAction SilentlyContinue' % rm_tmp
@@ -178,11 +149,9 @@ class ShellModule(object):
replace = lambda m: substs[m.lastindex - 1]
return re.sub(pattern, replace, value)
- def _encode_script(self, script, as_list=False, strict_mode=True):
+ def _encode_script(self, script, as_list=False):
'''Convert a PowerShell script to a single base64-encoded command.'''
script = to_unicode(script)
- if strict_mode:
- script = u'Set-StrictMode -Version Latest\r\n%s' % script
script = '\n'.join([x.strip() for x in script.splitlines() if x.strip()])
encoded_script = base64.b64encode(script.encode('utf-16-le'))
cmd_parts = _common_args + ['-EncodedCommand', encoded_script]