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

Set-SCPItem for a directory does not support unregular characters

Open Klaboe opened this issue 1 year ago • 7 comments

Trying to send a whole directory using Set-SCPItem that contains unregluar characters in its name leads to what seems like a locked terminal and nothing happens. I haven't investigated this deeply (maybe the same goes for regular files as well) but a command like this:

>> Set-SCPItem -ComputerName computername -Path .\Folder(Name)\ -Destination /Upload/ -Verbose

VERBOSE: Using SSH Username and Password authentication for connection.
VERBOSE: ssh-ed25519 Fingerprint for computername: <...mac-adress-or-something>
VERBOSE: Fingerprint matched trusted ssh-ed25519 fingerprint for host computername
VERBOSE: Connection successful
VERBOSE: Uploading: C:\Users\User\Folder(Name)\
VERBOSE: Destination: /MUSIC/Interplay/Folder(Name)

This will just hang forever (at least over night 😅) and cant be interupted with ctrl+c. I'm gussing it has to do with some path-parsing on the destination side as ( needs to be escaped 🤔 ( on a *unix machine

As a sidenote; if one tries to send a directory, and a directory with the same name doesn't exist on the Destination, one would get a

>> Set-SCPItem -ComputerName computername -Path .\SomeDir\ -Destination /Upload/ -Verbose
(...)
Set-SCPItem: scp: /Upload/SomeDir: No such file or directory

Which is a little counter-intuitive when one tries to send a complete directory, but not a huge problem. Just annoyance 😇

 >>  Get-Module Posh-SSH

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   3.0.8                 Posh-SSH                            {Get-SCPItem, Get-SFTPItem, Get-SSHHostKey, Get-SSHJsonKnownHost…}`

Klaboe avatar Jul 09 '23 09:07 Klaboe

you may try to use -PathTransformation parameter

MVKozlov avatar Jul 11 '23 12:07 MVKozlov

For which part? The sending of directory with unregular characters, or the handling of "no such file or directory"?

(Both?)

Klaboe avatar Jul 12 '23 10:07 Klaboe

the single command you use in your example - Set-SCPItem :)

MVKozlov avatar Jul 12 '23 10:07 MVKozlov

I still have this problem and not fix it yet, and I'm trying tying to use Set-SCPItem with for loop to looping on each file on my folder and then copy it into my server.

$files = Get-ChildItem -Path $MyPath -File
foreach ($file in $files) {
    $src = Join-Path -Path $MyPath -ChildPath $file.Name

    Set-SCPItem -ComputerName $ComputerName -Credential $Credentials -path $src -Destination $Destination -Verbose
}

bero20007 avatar Apr 15 '24 00:04 bero20007

What special characters are in your case causing it to fail?

darkoperator avatar Apr 15 '24 11:04 darkoperator

What special characters are in your case causing it to fail?

$ComputerName = "MYSERVERIP" $MyPath = "C:\Users\ibrahim\Desktop\ProjectPublish" $Destination = "/var/www/Project/API" Set-SCPItem -ComputerName $ComputerName -Credential $Credentials -path $MyPath -Destination $Destination -Verbose

the connection and handshake work well but the upload start: VERBOSE: Uploading: C:\Users\ibrahim\ProjectPublish
VERBOSE: Destination: /var/www/Project/API/ProjectPublish and that's wrong because ProjectPublish is not exist on my server it's a folder i use to store my project publish result on my device what i want is to copy all files inside ProjectPublish folder into this path: /var/www/Project/API/ on my server the conclusion is why my local folder ProjectPublish concat with the Destination and became like this: /var/www/Project/API/ProjectPublish

bero20007 avatar Apr 15 '24 14:04 bero20007

If you specify a directory it will copy the directory with its name as is to the specified path. It is behaving as expected. If you want to upload each file in the directory individually then you will need a loop to upload each to the root of the target path. SFTP is a better option since you will mot have to establish a tear down a connection for each file

darkoperator avatar Apr 15 '24 14:04 darkoperator