darkoperator / Posh-SSH

PowerShell Module for automating tasks on remote systems using SSH
BSD 3-Clause "New" or "Revised" License
985 stars 227 forks source link

Get-SFTPItem failing if Path has en dash #524

Open artagame opened 1 year ago

artagame commented 1 year ago

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 commented 1 year ago

@darkoperator please help. thank you!

MVKozlov commented 1 year ago

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

artagame commented 1 year ago

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 :(

MVKozlov commented 1 year ago

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.

darkoperator commented 1 year ago
╭─ 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.

artagame commented 1 year ago

@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

darkoperator commented 1 year ago

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

artagame commented 1 year ago

@darkoperator its an en dash

darkoperator commented 1 year ago

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.

artagame commented 1 year ago

@darkoperator using WinSCP here is the folder name image

MVKozlov commented 1 year ago

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

artagame commented 1 year ago

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

MVKozlov commented 1 year ago

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 ?

MikeMM70 commented 1 year ago

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

artagame commented 1 year ago

@MikeMM70 tried it and I got 65533 image

artagame commented 1 year ago

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

MVKozlov commented 1 year ago

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