Open ianharrier opened 8 months ago
Have your tried using the ConvertTo-WinSCPEscapedString cmdlet to escape the character? https://github.com/tomohulk/WinSCP/wiki/ConvertTo-WinSCPEscapedString
Thank you for your reply! No, I was not aware of the ConvertTo-WinSCPEscapedString
cmdlet.
ConvertTo-WinSCPEscapedString
does seem to fix the issue I was having with Receive-WinSCPItem
. These commands both work correctly now:
Receive-WinSCPItem -WinSCPSession $Session -RemotePath $(ConvertTo-WinSCPEscapedString -FileMask "/home/test[1234.txt") -LocalPath "C:\Files"
Receive-WinSCPItem -WinSCPSession $Session -RemotePath $(ConvertTo-WinSCPEscapedString -FileMask "/home/test[1234].txt") -LocalPath "C:\Files"
However, I'm still having trouble with Send-WinSCPItem
. Both of these commands fail to transfer the file:
Send-WinSCPItem -WinSCPSession $Session -LocalPath $(ConvertTo-WinSCPEscapedString -FileMask "C:\Files\test[1234.txt") -RemotePath "/home"
<#
Convert-Path : The specified wildcard character pattern is not valid: test[1234.txt
At C:\Program Files\WindowsPowerShell\Modules\WinSCP\6.1.2.0\Public\Send-WinSCPItem.ps1:58 char:38
+ ... lPathValue in (Convert-Path -Path (Resolve-Path -Path $LocalPath))) {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Convert-Path], WildcardPatternException
+ FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.ConvertPathCommand
#>
Send-WinSCPItem -WinSCPSession $Session -LocalPath $(ConvertTo-WinSCPEscapedString -FileMask "C:\Files\test[1234].txt") -RemotePath "/home"
<#
No output was produced
#>
It seems like the paths are being escaped correctly in each case:
ConvertTo-WinSCPEscapedString -FileMask "/home/test[1234.txt"
<#
/home/test[[]1234.txt
#>
ConvertTo-WinSCPEscapedString -FileMask "/home/test[1234].txt"
<#
/home/test[[]1234].txt
#>
ConvertTo-WinSCPEscapedString -FileMask "C:\Files\test[1234.txt"
<#
C:\Files\test[[]1234.txt
#>
ConvertTo-WinSCPEscapedString -FileMask "C:\Files\test[1234].txt"
<#
C:\Files\test[[]1234].txt
#>
Thanks again!
I have to think about this. Basically it trying to resolve the local file path and that will now fail because that local file doesn't technically exist with those escape characters. I think really you should use the File mask on the -RemotePath, because don't need to escape that on the windows local path.
Issue Description
When the open square bracket (
[
) character appears in a file name, theReceive-WinSCPItem
andSend-WinSCPItem
cmdlets fail to transfer the file.Example
Expected Output
The
Receive-WinSCPItem
andSend-WinSCPItem
cmdlets should be able to transfer a file containing the open square bracket ([
) character.Actual Output
See code block in Example section above.
WinSCP-PowerShell Version
Environment
OS: Windows Server 2019 (x86_64 / AMD64)
PowerShell Version:
Protocol: SFTP
Server: OpenSSH (Debian 12)