# Buggy code
def expand_user(self, user_home_path, username=""):
    # PowerShell only supports "~" (not "~username").  Resolve-Path ~ does
    # not seem to work remotely, though by default we are always starting
    # in the user's home directory.
    user_home_path = self._unquote(user_home_path)
    if user_home_path == "~":
        script = "Write-Output (Get-Location).Path"
    elif user_home_path.startswith("~\\"):
        script = 'Write-Output ((Get-Location).Path + "%s")' % self._escape(
            user_home_path[1:]
        )
    else:
        script = 'Write-Output "%s"' % self._escape(user_home_path)
    return self._encode_script(script)


# Traceback (OSError)
TASK [fetch log files] ***********************************************************************************************************************************************************************
task path: /home/testuser/ansible/test.yml:12
Using module file /usr/local/ansible/2.7/lib/python3.6/site-packages/ansible/modules/windows/win_stat.ps1
<testclient> ESTABLISH WINRM CONNECTION FOR USER: testuser@DOMAIN on PORT 5985 TO testclient
checking if winrm_host testclient is an IPv6 address
EXEC (via pipeline wrapper)
<testclient> FETCH "C:\Temp\testfile_with_$dollar.txt" TO "/home/testuser/ansible/testfile_with_$dollar.txt"
Traceback (most recent call last):
File "/usr/local/ansible/2.7/lib/python3.6/site-packages/ansible/plugins/connection/winrm.py", line 681, in fetch_file
raise IOError(to_native(result.std_err))
OSError: #< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">The variable '$dollar' cannot be retrieved because it has not been set._x000D__x000A_</S><S S="Error">At line:2 char:32_x000D__x000A_</S><S S="Error">+ $path = "C:\Temp\testfile_with_$dollar.txt"_x000D__x000A_</S><S S="Error">+                                ~~~~~~~_x000D__x000A_</S><S S="Error">    + CategoryInfo          : InvalidOperation: (dollar:String) [], RuntimeExc _x000D__x000A_</S><S S="Error">   eption_x000D__x000A_</S><S S="Error">    + FullyQualifiedErrorId : VariableIsUndefined_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S><S S="Error">The variable '$path' cannot be retrieved because it has not been set._x000D__x000A_</S><S S="Error">At line:3 char:21_x000D__x000A_</S><S S="Error">+ If (Test-Path -Path $path -PathType Leaf)_x000D__x000A_</S><S S="Error">+                     ~~~~~_x000D__x000A_</S><S S="Error">    + CategoryInfo          : InvalidOperation: (path:String) [], RuntimeExcep _x000D__x000A_</S><S S="Error">   tion_x000D__x000A_</S><S S="Error">    + FullyQualifiedErrorId : VariableIsUndefined_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>
fatal: [testclient]: FAILED! => {}

# Fixed code
def expand_user(self, user_home_path, username=""):
    # PowerShell only supports "~" (not "~username").  Resolve-Path ~ does
    # not seem to work remotely, though by default we are always starting
    # in the user's home directory.
    user_home_path = self._unquote(user_home_path)
    if user_home_path == "~":
        script = "Write-Output (Get-Location).Path"
    elif user_home_path.startswith("~\\"):
        script = "Write-Output ((Get-Location).Path + '%s')" % self._escape(
            user_home_path[1:]
        )
    else:
        script = "Write-Output '%s'" % self._escape(user_home_path)
    return self._encode_script(script)