# Buggy code
def _create_raw_wrapper_payload(self, cmd, environment=None):
    environment = {} if environment is None else environment

    payload = {
        "module_entry": to_text(base64.b64encode(to_bytes(cmd))),
        "powershell_modules": {},
        "actions": ["exec"],
        "exec": to_text(base64.b64encode(to_bytes(leaf_exec))),
        "environment": environment,
    }

    return json.dumps(payload)


# Traceback (WinRMTransportError)
ASK [replace_credentials : replace special strings with their corresponding credentials] ****************************************************************************************************
task path: /tmp/deployment/roles/replace_credentials/tasks/main.yml:49
<i89666.sbb.ch> ESTABLISH WINRM CONNECTION FOR USER: fs45087@SBB.CH on PORT 5986 TO i89666.sbb.ch
<i89666.sbb.ch> WINRM CONNECT: transport=credssp endpoint=https://i89666.sbb.ch:5986/wsman
<i89666.sbb.ch> WINRM OPEN SHELL: 0F4A65B5-2D6A-454A-BD7F-289C0F330046
EXEC (via pipeline wrapper)
<i89666.sbb.ch> WINRM EXEC 'PowerShell' ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-EncodedCommand', 'UABvAHcAZQByAFMAaABlAGwAbAAgAC0ATgBvAFAAcgBvAGYAaQBsAGUAIAAtAE4AbwBuAEkAbgB0AGUAcgBhAGMAdABpAHYAZQAgAC0ARQB4AGUAYwB1AHQAaQBvAG4AUABvAGwAaQBjAHkAIABVAG4AcgBlAHMAdAByAGkAYwB0AGUAZAAgAC0ARQBuAGMAbwBkAGUAZABDAG8AbQBtAGEAbgBkACAAVQB3AEIAbABBAEgAUQBBAEwAUQBCAFQAQQBIAFEAQQBjAGcAQgBwAEEARwBNAEEAZABBAEIATgBBAEcAOABBAFoAQQBCAGwAQQBDAEEAQQBMAFEAQgBXAEEARwBVAEEAYwBnAEIAegBBAEcAawBBAGIAdwBCAHUAQQBDAEEAQQBUAEEAQgBoAEEASABRAEEAWgBRAEIAegBBAEgAUQBBAEMAZwBCAFgAQQBIAEkAQQBhAFEAQgAwAEEARwBVAEEATABRAEIASQBBAEcAOABBAGMAdwBCADAAQQBDAEEAQQBLAEEAQ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QBCAGwAQQBIAGcAQQBhAFEAQgAwAEEAQwBBAEEATQBRAEEAZwBBAEgAMABBAEkAQQBCADkAQQBBAD0APQA=']
<i89666.sbb.ch> WINRM RESULT u'<Response code 0, out "C:\\Users\\fs45087\\App", err "">'
looking for "files/Test.ps1" at "/tmp/deployment/roles/replace_credentials/files/Test.ps1"
<i89666.sbb.ch> PUT "/tmp/deployment/roles/replace_credentials/files/Test.ps1" TO "C:\Users\fs45087\AppData\Local\Temp\ansible-tmp-1510323347.24-3741944900007\Test.ps1"
<i89666.sbb.ch> WINRM PUT "/tmp/deployment/roles/replace_credentials/files/Test.ps1" to "C:\Users\fs45087\AppData\Local\Temp\ansible-tmp-1510323347.24-3741944900007\Test.ps1" (offset=2337 size=2337)
EXEC (via pipeline wrapper)
<i89666.sbb.ch> WINRM EXEC 'PowerShell' ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-EncodedCommand', 'JgAgACAAJwBDADoAXABVAHMAZQByAHMAXABmAHMANAA1ADAAOAA3AFwAQQBwAHAARABhAHQAYQBcAEwAbwBjAGEAbABcAFQAZQBtAHAAXABhAG4AcwBpAGIAbABlAC0AdABtAHAALQAxADUAMQAwADMAMgAzADMANAA3AC4AMgA0AC0AMwA3ADQAMQA5ADQANAA5ADAAMAAwADAANwBcAFQAZQBzAHQALgBwAHMAMQAnACAALQBzAHQAYQBnAGUAQwByAGUAZABlAG4AdABpAGEAbABzAEoAcwBvAG4AIAAnAHsAIgBjAGUAcgB0AGkAZgBpAGMAYQB0AGUAIgA6ACAAewAiAHAAYQBzAHMAdwBvAHIAZAAiADoAIAAiAHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAAiAH0ALAAgACIAZgB1AG4AYwB0AGkAbwBuAGEAbABVAHMAZQByACIAOgAgAHsAIgBkAG8AbQBhAGkAbgAiADoAIAAiAG4AdAAtAHMAYgBiADEAIgAsACAAIgBwAGEAcwBzAHcAbwByAGQAIgA6ACAAIg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4AHgAeAAiACwAIAAiAG4AYQBtAGUAIgA6ACAAIgBjAG4APQBnAGQAaQBSAGUAYQBkAGUAcgAsAG8AdQA9AEEAZABtAGkAbgBpAHMAdAByAGEAdABvAHIAcwAsAGQAYwA9AHMAYgBiACwAZABjAD0AQwBIACIAfQB9ACwAIAAiAGUAQgBpAHoAUAByAG8AZAB1AGsAdABpAG8AbgAiADoAIAB7ACIAbAB0AHAAYQBWAGEAbAB2AGUAIgA6ACAAewAiAHAAYQBzAHMAdwBvAHIAZAAiADoAIAAiAHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAAiAH0ALAAgACIAZwBkAGkAUgBlAGEAZABlAHIAIgA6ACAAewAiAHAAYQBzAHMAdwBvAHIAZAAiADoAIAAiAHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAIgAsACAAIgBuAGEAbQBlACIAOgAgACIAYwBuAD0AZwBkAGkAUgBlAGEAZABlAHIALABvAHUAPQBBAGQAbQBpAG4AaQBzAHQAcgBhAHQAbwByAHMALABkAGMAPQBzAGIAYgAsAGQAYwA9AEMASAAiAH0AfQB9ACcA']
[WARNING]: FATAL ERROR DURING FILE TRANSFER: Traceback (most recent call last):   File "/var/lib/awx/venv/ansible/local/lib/python2.7/site-packages/ansible/plugins/connection/winrm.py",
line 267, in _winrm_exec     self._winrm_send_input(self.protocol, self.shell_id, command_id, data, eof=is_last)   File "/var/lib/awx/venv/ansible/local/lib/python2.7/site-
packages/ansible/plugins/connection/winrm.py", line 248, in _winrm_send_input     protocol.send_message(xmltodict.unparse(rq))   File "/var/lib/awx/venv/ansible/local/lib/python2.7/site-
packages/winrm/protocol.py", line 207, in send_message     return self.transport.send_message(message)   File "/var/lib/awx/venv/ansible/local/lib/python2.7/site-
packages/winrm/transport.py", line 202, in send_message     raise WinRMTransportError('http', error_message) WinRMTransportError: (u'http', u'Bad HTTP response returned from server. Code
500')

<i89666.sbb.ch> WINRM CLOSE SHELL: 0F4A65B5-2D6A-454A-BD7F-289C0F330046
fatal: [i89666.sbb.ch]: FAILED! => {
"failed": true,
"msg": "winrm send_input failed"
}

# Fixed code
def _create_raw_wrapper_payload(self, cmd, environment=None):
    environment = {} if environment is None else environment

    payload = {
        "module_entry": to_text(base64.b64encode(to_bytes(cmd))),
        "powershell_modules": {},
        "actions": ["exec"],
        "exec": to_text(base64.b64encode(to_bytes(leaf_exec))),
        "environment": environment,
        "min_ps_version": None,
        "min_os_version": None,
    }

    return json.dumps(payload)