Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Get-SftpItem gives an error when trying to get a file that contains illegal characters: #533

Open
mehranr81 opened this issue Jun 22, 2023 · 9 comments

Comments

@mehranr81
Copy link

Dear
I need to download from ericsson telecom modules: but the files on ftp server includes illegal character, help me to obviate the problem or teach me how to use move-sftpitem to rename files on the server and then download it.
image
I will be thankful if you help me.

$Server = "172.18..."
$RemoteDirectory = "/c/pm_data"
$LocalDirectory = "C:\eric"
$User = " ... "
$PWord = ConvertTo-SecureString -String " ... " -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
$Session = New-SftpSession -ComputerName $Server -Credential $Credential
$Files = Get-SftpChildItem -SessionId $Session.SessionId -Path $RemoteDirectory -Recurse |
Where-Object { $.FullName -like '*.gz' } |
Select-Object -Property @{Name="NewFullName"; Expression={$
.FullName -replace ":" , "_"}}

Download each file

$Files | ForEach-Object {

$RemoteFilePath = $_.NewFullName


# Download the file
move-SftpItem -SessionId $Session.SessionId -Destination c:\eric -Path $RemoteFilePath

}

Close the SFTP session

Remove-SftpSession -SessionId $Session.SessionId

@mehranr81
Copy link
Author

mehranr81 commented Jun 23, 2023 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Jun 23, 2023

@mehranr81, That's why I deleted my comment :)
I didn't notice at first that your "illegal" character is a colon

@mehranr81
Copy link
Author

mehranr81 commented Jun 23, 2023 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Jun 23, 2023

just checked how it works
You should use full path while rename

$sftp = New-SFTPSession -ComputerName $computer -Credential $Credential -KeyString $KeyData
Get-SFTPChildItem -SessionId 0 -Path /home/username

...
FullName       : /home/username/test:file
LastAccessTime : 23.06.2023 12:24:26
LastWriteTime  : 23.06.2023 12:24:32
Length         : 9
UserId         : 2004
...

Rename-SFTPFile -SessionId 0 -Path /home/username/test:file -NewName test_file
MethodInvocationException: Exception calling "RenameFile" with "2" argument(s): "Permission denied"

Rename-SFTPFile -SessionId 0 -Path /home/username/test:file -NewName /home/username/test_file
Get-SFTPChildItem -SessionId 0 -Path /home/username
...
FullName       : /home/username/test_file
LastAccessTime : 23.06.2023 12:24:26
LastWriteTime  : 23.06.2023 12:24:32
Length         : 9
UserId         : 2004
...

@mehranr81
Copy link
Author

mehranr81 commented Jun 23, 2023 via email

@MVKozlov
Copy link
Contributor

@mehranr81, filezilla has own functions :)

but Posh-SSH keep original name

var fileName = new FileInfo(remotepath).Name;

so if you want to download file with ':' you should rename it on remote server (my example)
download it and rename back if it need

may be v4 will change something, but it will decide @darkoperator

@mehranr81
Copy link
Author

mehranr81 commented Jun 23, 2023 via email

@mehranr81
Copy link
Author

mehranr81 commented Jun 23, 2023 via email

@MVKozlov
Copy link
Contributor

@mehranr81, you wrote

I wanted to rename each file and then download it , that could be even great, but as i sent the code in picture to u, server is not allowing rename task remotely from outside, probably this has been done for security

You rename it without full path

Rename-SFTPFile -SessionId $session.SessionId -Path /c/pm_data/A20230622.2245-2300:1.xml.gz -Newname A20230622.2245-2300_1.xml.gz

did you test it like this ?

Rename-SFTPFile -SessionId $session.SessionId -Path /c/pm_data/A20230622.2245-2300:1.xml.gz -Newname /c/pm_data/A20230622.2245-2300_1.xml.gz

and if it can not be done, You may try to use Get-SFTPContent instead of Get-SFTPFile

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants