Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Get-SFTPItem failing if Path has en dash

Open artagame opened this issue 1 year ago • 17 comments

Hi I have a folder from our SFTP server who has en dash in its path and whenever we tried to use the command to download the folder in our local machine it is failing saying that the folder is not existing. Any suggestions or workaround for this?

artagame avatar Apr 17 '23 02:04 artagame

@darkoperator please help. thank you!

artagame avatar Apr 17 '23 04:04 artagame

You may try to play with Session.ConnectionInfo.Encoding setting

$sftp = New-SftpSession .....
$sftp.Session.ConnectionInfo.Encoding = [System.Text.Encoding]::GetEncoding(1252)

with your code page of course

MVKozlov avatar Apr 17 '23 06:04 MVKozlov

You may try to play with Session.ConnectionInfo.Encoding setting

$sftp = New-SftpSession .....
$sftp.Session.ConnectionInfo.Encoding = [System.Text.Encoding]::GetEncoding(1252)

with your code page of course

tried this one even changing the encoding but still not working :(

artagame avatar Apr 17 '23 07:04 artagame

did a test (with standard dash and unicode from U+2012 to U+2015) no problem with ubuntu (default utf-8) Try outputting this file through an sftp session using Get-SFTPChildItem to check for the correct encoding.

MVKozlov avatar Apr 17 '23 09:04 MVKozlov

╭─ pwsh  ~                                                                                              06:12:52 
╰─❯ Get-SFTPChildItem -SessionId 0 -Path "/tmp"

FullName       : /tmp/this-is-a-test
LastAccessTime : 4/17/2023 6:11:06 AM
LastWriteTime  : 4/17/2023 6:11:39 AM
Length         : 4096
UserId         : 1000

FullName       : /tmp/klippy_uds
LastAccessTime : 4/16/2023 7:38:02 PM
LastWriteTime  : 4/16/2023 7:38:02 PM
Length         : 0
UserId         : 1000

FullName       : /tmp/.ICE-unix
LastAccessTime : 4/15/2023 9:17:04 PM
LastWriteTime  : 4/15/2023 9:17:04 PM
Length         : 4096
UserId         : 0

FullName       : /tmp/.Test-unix
LastAccessTime : 4/15/2023 9:17:04 PM
LastWriteTime  : 4/15/2023 9:17:04 PM
Length         : 4096
UserId         : 0

FullName       : /tmp/printer
LastAccessTime : 4/15/2023 9:24:18 PM
LastWriteTime  : 4/15/2023 9:24:18 PM
Length         : 10
UserId         : 1000

FullName       : /tmp/.font-unix
LastAccessTime : 4/15/2023 9:17:04 PM
LastWriteTime  : 4/15/2023 9:17:04 PM
Length         : 4096
UserId         : 0

FullName       : /tmp/systemd-private-88a2db33291e4d6abfdad0fead8d28b3-systemd-timesyncd.service-16ATfW
LastAccessTime : 4/15/2023 9:17:04 PM
LastWriteTime  : 4/15/2023 9:17:04 PM
Length         : 4096
UserId         : 0

FullName       : /tmp/klipper_host_mcu
LastAccessTime : 4/16/2023 7:38:01 PM
LastWriteTime  : 4/16/2023 7:38:01 PM
Length         : 10
UserId         : 0

FullName       : /tmp/.XIM-unix
LastAccessTime : 4/15/2023 9:17:04 PM
LastWriteTime  : 4/15/2023 9:17:04 PM
Length         : 4096
UserId         : 0

FullName       : /tmp/.X11-unix
LastAccessTime : 4/15/2023 9:17:04 PM
LastWriteTime  : 4/15/2023 9:24:17 PM
Length         : 4096
UserId         : 0

FullName       : /tmp/.X0-lock
LastAccessTime : 4/15/2023 9:24:17 PM
LastWriteTime  : 4/15/2023 9:24:17 PM
Length         : 11
UserId         : 0


╭─ pwsh  ~                                                                                              06:13:25 
╰─❯ Get-SFTPItem -SessionId 0 -Path '/tmp/this-is-a-test' -Destination ./ -Verbose
VERBOSE: Downloading /tmp/this-is-a-test/test.txt
╭─ pwsh  ~                                                                                              06:14:11 
╰─❯ ls .\this-is-a-test\

        Directory: C:\Users\Carlos Perez\this-is-a-test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a---         4/17/2023   6:14 AM              0   test.txt

How are you running the command so I can replicate.

darkoperator avatar Apr 17 '23 10:04 darkoperator

@darkoperator thank you so much for responding

` $sftpSession = New-SFTPSession -ComputerName $ctnIP -Credential $pscred -KeyFile $keyFile -OperationTimeout -1 -AcceptKey Get-SFTPItem -SessionId $sftpSession.SessionId -Path $value -Destination $localTargetDirectory -Force

`

I executing this using PowerShell and the value for Path is a parameter

I am getting this error: image

and when I tried to list the folders I got this image

folder name: 20230414 - Spark-ONYX – Dress Rehearsal - ReDrop

artagame avatar Apr 18 '23 00:04 artagame

There is a weird character there between Onyx and Dress that PS does not know how to interpret it

darkoperator avatar Apr 18 '23 00:04 darkoperator

@darkoperator its an en dash

artagame avatar Apr 18 '23 00:04 artagame

It is a different hash from the other ones, I can see it is longer in one screenshot and a weird question mark in the other.

darkoperator avatar Apr 18 '23 00:04 darkoperator

@darkoperator using WinSCP here is the folder name image

artagame avatar Apr 18 '23 00:04 artagame

I think encoding of "en dash" ( U+2013 ?) in $value not correct for test you may try to list contents with Get-SFtpChildItem and pass result to Get-SFTPItem. This may work

MVKozlov avatar Apr 18 '23 07:04 MVKozlov

I think encoding of "en dash" ( U+2013 ?) in $value not correct for test you may try to list contents with Get-SFtpChildItem and pass result to Get-SFTPItem. This may work

Hi @MVKozlov tried this but it's not working, it's just getting this so it still returns an error image

artagame avatar Apr 18 '23 07:04 artagame

I don't like the "-1" in your UserId and Length outputs, it seems like something is wrong with the access or it could be an OS issue. I still think it's an encoding problem.

Did you tried Get-SFTPChildItem .... | Foreach-Object { Get-SFTPItem .... -Path $_.Fullname } or your code was different ?

MVKozlov avatar Apr 18 '23 08:04 MVKozlov

That looks like a hyphen to me, not a dash. Have you tried converting the filename to Unicode character values to see what that character really is?

$strng[0..$strng.length] | foreach { [double][char]($_) }

*Edited because I should have started at 0 on the string, not 1

MikeMM70 avatar Apr 18 '23 17:04 MikeMM70

@MikeMM70 tried it and I got 65533 image

artagame avatar Apr 20 '23 07:04 artagame

I don't like the "-1" in your UserId and Length outputs, it seems like something is wrong with the access or it could be an OS issue. I still think it's an encoding problem.

Did you tried Get-SFTPChildItem .... | Foreach-Object { Get-SFTPItem .... -Path $_.Fullname } or your code was different ? yes I got the full name by using Get-SFTPChildItem

image

artagame avatar Apr 20 '23 07:04 artagame

There is something really wrong with encoding 65533 - 0xfffd - unicode replacement character https://www.fileformat.info/info/unicode/char/0fffd/index.htm that is, it means that the symbol on the server cannot be displayed on the client

MVKozlov avatar Apr 20 '23 08:04 MVKozlov