# Buggy code
def _winrm_exec(self, command, args=(), from_exec=False, stdin_iterator=None):
    if not self.protocol:
        self.protocol = self._winrm_connect()
        self._connected = True
    if from_exec:
        display.vvvvv("WINRM EXEC %r %r" % (command, args), host=self._winrm_host)
    else:
        display.vvvvvv("WINRM EXEC %r %r" % (command, args), host=self._winrm_host)
    command_id = None
    try:
        stdin_push_failed = False
        command_id = self.protocol.run_command(
            self.shell_id,
            to_bytes(command),
            map(to_bytes, args),
            console_mode_stdin=(stdin_iterator is None),
        )

        # TODO: try/except around this, so we can get/return the command result on a broken pipe or other failure (probably more useful than the 500 that
        # comes from this)
        try:
            if stdin_iterator:
                for data, is_last in stdin_iterator:
                    self._winrm_send_input(
                        self.protocol, self.shell_id, command_id, data, eof=is_last
                    )

        except Exception as ex:
            from traceback import format_exc

            display.warning("FATAL ERROR DURING FILE TRANSFER: %s" % format_exc())
            stdin_push_failed = True

        if stdin_push_failed:
            raise AnsibleError("winrm send_input failed")

        # NB: this can hang if the receiver is still running (eg, network failed a Send request but the server's still happy).
        # FUTURE: Consider adding pywinrm status check/abort operations to see if the target is still running after a failure.
        resptuple = self.protocol.get_command_output(self.shell_id, command_id)
        # ensure stdout/stderr are text for py3
        # FUTURE: this should probably be done internally by pywinrm
        response = Response(
            tuple(to_text(v) if isinstance(v, binary_type) else v for v in resptuple)
        )

        # TODO: check result from response and set stdin_push_failed if we have nonzero
        if from_exec:
            display.vvvvv("WINRM RESULT %r" % to_text(response), host=self._winrm_host)
        else:
            display.vvvvvv("WINRM RESULT %r" % to_text(response), host=self._winrm_host)

        display.vvvvvv(
            "WINRM STDOUT %s" % to_text(response.std_out), host=self._winrm_host
        )
        display.vvvvvv(
            "WINRM STDERR %s" % to_text(response.std_err), host=self._winrm_host
        )

        if stdin_push_failed:
            raise AnsibleError(
                "winrm send_input failed; \nstdout: %s\nstderr %s"
                % (response.std_out, response.std_err)
            )

        return response
    finally:
        if command_id:
            self.protocol.cleanup_command(self.shell_id, command_id)


# 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', 'UABvAHcAZQByAFMAaABlAGwAbAAgAC0ATgBvAFAAcgBvAGYAaQBsAGUAIAAtAE4AbwBuAEkAbgB0AGUAcgBhAGMAdABpAHYAZQAgAC0ARQB4AGUAYwB1AHQAaQBvAG4AUABvAGwAaQBjAHkAIABVAG4AcgBlAHMAdAByAGkAYwB0AGUAZAAgAC0ARQBuAGMAbwBkAGUAZABDAG8AbQBtAGEAbgBkACAAVQB3AEIAbABBAEgAUQBBAEwAUQBCAFQAQQBIAFEAQQBjAGcAQgBwAEEARwBNAEEAZABBAEIATgBBAEcAOABBAFoAQQBCAGwAQQBDAEEAQQBMAFEAQgBXAEEARwBVAEEAYwBnAEIAegBBAEcAawBBAGIAdwBCAHUAQQBDAEEAQQBUAEEAQgBoAEEASABRAEEAWgBRAEIAegBBAEgAUQBBAEMAZwBCAFgAQQBIAEkAQQBhAFEAQgAwAEEARwBVAEEATABRAEIASQBBAEcAOABBAGMAdwBCADAAQQBDAEEAQQBLAEEAQgBIAEEARwBVAEEAZABBAEEAdABBAEUAdwBBAGIAdwBCAGoAQQBHAEUAQQBkAEEAQgBwAEEARwA4AEEAYgBnAEEAcABBAEMANABBAFUAQQBCAGgAQQBIAFEAQQBhAEEAQQBLAEEARQBrAEEAWgBnAEEAZwBBAEMAZwBBAEwAUQBCAHUAQQBHADgAQQBkAEEAQQBnAEEAQwBRAEEAUAB3AEEAcABBAEMAQQBBAGUAdwBBAGcAQQBFAGsAQQBaAGcAQQBnAEEAQwBnAEEAUgB3AEIAbABBAEgAUQBBAEwAUQBCAFcAQQBHAEUAQQBjAGcAQgBwAEEARwBFAEEAWQBnAEIAcwBBAEcAVQBBAEkAQQBCAE0AQQBFAEUAQQBVAHcAQgBVAEEARQBVAEEAVwBBAEIASgBBAEYAUQBBAFEAdwBCAFAAQQBFAFEAQQBSAFEAQQBnAEEAQwAwAEEAUgBRAEIAeQBBAEgASQBBAGIAdwBCAHkAQQBFAEUAQQBZAHcAQgAwAEEARwBrAEEAYgB3AEIAdQBBAEMAQQBBAFUAdwBCAHAAQQBHAHcAQQBaAFEAQgB1AEEASABRAEEAYgBBAEIANQBBAEUATQBBAGIAdwBCAHUAQQBIAFEAQQBhAFEAQgB1AEEASABVAEEAWgBRAEEAcABBAEMAQQBBAGUAdwBBAGcAQQBHAFUAQQBlAEEAQgBwAEEASABRAEEASQBBAEEAawBBAEUAdwBBAFEAUQBCAFQAQQBGAFEAQQBSAFEAQgBZAEEARQBrAEEAVgBBAEIARABBAEUAOABBAFIAQQBCAEYAQQBDAEEAQQBmAFEAQQBnAEEARQBVAEEAYgBBAEIAegBBAEcAVQBBAEkAQQBCADcAQQBDAEEAQQBaAFEAQgA0AEEARwBrAEEAZABBAEEAZwBBAEQARQBBAEkAQQBCADkAQQBDAEEAQQBmAFEAQQA9AA==']
<i89666.sbb.ch> WINRM RESULT u'<Response code 0, out "C:\\Users\\fs45087\n", err "">'
EXEC (via pipeline wrapper)
<i89666.sbb.ch> WINRM EXEC 'PowerShell' ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-EncodedCommand', 'UABvAHcAZQByAFMAaABlAGwAbAAgAC0ATgBvAFAAcgBvAGYAaQBsAGUAIAAtAE4AbwBuAEkAbgB0AGUAcgBhAGMAdABpAHYAZQAgAC0ARQB4AGUAYwB1AHQAaQBvAG4AU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kAQQBHADgAQQBjAHcAQgAwAEEAQwBBAEEATABRAEIAVABBAEcAVQBBAGMAQQBCAGgAQQBIAEkAQQBZAFEAQgAwAEEARwA4AEEAYwBnAEEAZwBBAEMAYwBBAEoAdwBBADcAQQBBAG8AQQBTAFEAQgBtAEEAQwBBAEEASwBBAEEAdABBAEcANABBAGIAdwBCADAAQQBDAEEAQQBKAEEAQQAvAEEAQwBrAEEASQBBAEIANwBBAEMAQQBBAFMAUQBCAG0AQQBDAEEAQQBLAEEAQgBIAEEARwBVAEEAZABBAEEAdABBAEYAWQBBAFkAUQBCAHkAQQBHAGsAQQBZAFEAQgBpAEEARwB3AEEAWgBRAEEAZwBBAEUAdwBBAFEAUQBCAFQAQQBGAFEAQQBSAFEAQgBZAEEARQBrAEEAVgBBAEIARABBAEUAOABBAFIAQQBCAEYAQQBDAEEAQQBMAFEAQgBGAEEASABJAEEAYwBnAEIAdgBBAEgASQBBAFEAUQBCAGoAQQBIAFEAQQBhAFEAQgB2AEEARwA0AEEASQBBAEIAVABBAEcAawBBAGIAQQBCAGwAQQBHADQAQQBkAEEAQgBzAEEASABrAEEAUQB3AEIAdgBBAEcANABBAGQAQQBCAHAAQQBHADQAQQBkAFEAQgBsAEEAQwBrAEEASQBBAEIANwBBAEMAQQBBAFoAUQBCADQAQQBHAGsAQQBkAEEAQQBnAEEAQwBRAEEAVABBAEIAQgBBAEYATQBBAFYAQQBCAEYAQQBGAGcAQQBTAFEAQgBVAEEARQBNAEEAVAB3AEIARQBBAEUAVQBBAEkAQQBCADkAQQBDAEEAQQBSAFEAQgBzAEEASABNAEEAWgBRAEEAZwBBAEgAcwBBAEkAQQBCAGwAQQBIAGcAQQBhAFEAQgAwAEEAQwBBAEEATQBRAEEAZwBBAEgAMABBAEkAQQBCADkAQQBBAD0APQA=']
<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', 'JgAgACAAJwBDADoAXABVAHMAZQByAHMAXABmAHMANAA1ADAAOAA3AFwAQQBwAHAARABhAHQAYQBcAEwAbwBjAGEAbABcAFQAZQBtAHAAXABhAG4AcwBpAGIAbABlAC0AdABtAHAALQAxADUAMQAwADMAMgAzADMANAA3AC4AMgA0AC0AMwA3ADQAMQA5ADQANAA5ADAAMAAwADAANwBcAFQAZQBzAHQALgBwAHMAMQAnACAALQBzAHQAYQBnAGUAQwByAGUAZABlAG4AdABpAGEAbABzAEoAcwBvAG4AIAAnAHsAIgBjAGUAcgB0AGkAZgBpAGMAYQB0AGUAIgA6ACAAewAiAHAAYQBzAHMAdwBvAHIAZAAiADoAIAAiAHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAAiAH0ALAAgACIAZgB1AG4AYwB0AGkAbwBuAGEAbABVAHMAZQByACIAOgAgAHsAIgBkAG8AbQBhAGkAbgAiADoAIAAiAG4AdAAtAHMAYgBiADEAIgAsACAAIgBwAGEAcwBzAHcAbwByAGQAIgA6ACAAIgB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAIgAsACAAIgBuAGEAbQBlACIAOgAgACIARgBTADQANQAwADUANwAiAH0ALAAgACIAcABvAHIAdABhAGwAYQBkAG0AaQBuACIAOgAgAHsAIgBwAGEAcwBzAHcAbwByAGQAIgA6ACAAIgB4AHgA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G8AZAB1AGsAdABpAG8AbgAiADoAIAB7ACIAbAB0AHAAYQBWAGEAbAB2AGUAIgA6ACAAewAiAHAAYQBzAHMAdwBvAHIAZAAiADoAIAAiAHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAAiAH0ALAAgACIAZwBkAGkAUgBlAGEAZABlAHIAIgA6ACAAewAiAHAAYQBzAHMAdwBvAHIAZAAiADoAIAAiAHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAeAB4AHgAIgAsACAAIgBuAGEAbQBlACIAOgAgACIAYwBuAD0AZwBkAGkAUgBlAGEAZABlAHIALABvAHUAPQBBAGQAbQBpAG4AaQBzAHQAcgBhAHQAbwByAHMALABkAGMAPQBzAGIAYgAsAGQAYwA9AEMASAAiAH0AfQB9ACcA']
[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 _winrm_exec(self, command, args=(), from_exec=False, stdin_iterator=None):
    if not self.protocol:
        self.protocol = self._winrm_connect()
        self._connected = True
    if from_exec:
        display.vvvvv("WINRM EXEC %r %r" % (command, args), host=self._winrm_host)
    else:
        display.vvvvvv("WINRM EXEC %r %r" % (command, args), host=self._winrm_host)
    command_id = None
    try:
        stdin_push_failed = False
        command_id = self.protocol.run_command(
            self.shell_id,
            to_bytes(command),
            map(to_bytes, args),
            console_mode_stdin=(stdin_iterator is None),
        )

        try:
            if stdin_iterator:
                for data, is_last in stdin_iterator:
                    self._winrm_send_input(
                        self.protocol, self.shell_id, command_id, data, eof=is_last
                    )

        except Exception as ex:
            from traceback import format_exc

            display.warning("FATAL ERROR DURING FILE TRANSFER: %s" % to_text(ex))
            stdin_push_failed = True

        # NB: this can hang if the receiver is still running (eg, network failed a Send request but the server's still happy).
        # FUTURE: Consider adding pywinrm status check/abort operations to see if the target is still running after a failure.
        resptuple = self.protocol.get_command_output(self.shell_id, command_id)
        # ensure stdout/stderr are text for py3
        # FUTURE: this should probably be done internally by pywinrm
        response = Response(
            tuple(to_text(v) if isinstance(v, binary_type) else v for v in resptuple)
        )

        # TODO: check result from response and set stdin_push_failed if we have nonzero
        if from_exec:
            display.vvvvv("WINRM RESULT %r" % to_text(response), host=self._winrm_host)
        else:
            display.vvvvvv("WINRM RESULT %r" % to_text(response), host=self._winrm_host)

        display.vvvvvv(
            "WINRM STDOUT %s" % to_text(response.std_out), host=self._winrm_host
        )
        display.vvvvvv(
            "WINRM STDERR %s" % to_text(response.std_err), host=self._winrm_host
        )

        if stdin_push_failed:
            stderr = to_bytes(response.std_err, encoding="utf-8")
            if self.is_clixml(stderr):
                stderr = self.parse_clixml_stream(stderr)

            raise AnsibleError(
                "winrm send_input failed; \nstdout: %s\nstderr %s"
                % (response.std_out, stderr)
            )

        return response
    finally:
        if command_id:
            self.protocol.cleanup_command(self.shell_id, command_id)