-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get-SftpItem gives an error when trying to get a file that contains illegal characters: #533
Comments
Dear Max First of all, I'm really grateful because of your help , but unfortunately adding [System.Text.Encoding]::GetEncoding(1251) to my code could solve the problem,since windows could not recognize illegal character in files that are located on the ftp server. filenames have a colon in between the other character. finally I added your help to code, but got 'Get-SftpItem : The given path's format is not supported.'GetEncoding(1251) is in cyrillic , how can it help illegal characters in filenames? I found a list different encoding from UTF8 to cyrillic System.Text.Encoding (abstract class) But how does it help the code?
My code:# Import the Posh-SSH moduleImport-Module -Name Posh-SSH
# Set the remote directory path$RemoteDirectory = "/c/pm_data"$Server = "172...."
# Set the local directory path where the downloaded files will be saved$LocalDirectory = "C:\eric"$User = "..."$PWord = ConvertTo-SecureString -String "rbs" -AsPlainText -Force$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
# Create a new SFTP session$Session = New-SftpSession -ComputerName $Server -Credential $Credential$enc = [System.Text.Encoding]::GetEncoding(1251) $Session.Session.ConnectionInfo.Encoding = $enc# Get the list of files in the remote directory$Files = Get-SftpChildItem -SessionId $Session.SessionId -Path $RemoteDirectory -Recurse | Where-Object { $_.FullName -like '*.gz' }
# Download each file$Files | ForEach-Object { $RemoteFilePath = $_.FullName # Download the file Get-SftpItem -SessionId $Session.SessionId -path $RemoteFilePath -destination c:\eric }
# Close the SFTP sessionRemove-SftpSession -SessionId $Session.SessionId
|
|
| |
System.Text.Encoding (abstract class)
|
|
|
On Thursday, June 22, 2023 at 01:16:19 PM GMT+3:30, Max Kozlov ***@***.***> wrote:
you can try to change connection encoding like this
$enc = [System.Text.Encoding]::GetEncoding(1251)
$Session.Session.ConnectionInfo.Encoding = $enc
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
@mehranr81, That's why I deleted my comment :) |
Dear Max could u plz check my code? cause I even can't rename files r the sever my code: $User = "..." $PWord = ConvertTo-SecureString -String "rbs" -AsPlainText -Force $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord $Session = New-SftpSession -ComputerName 18.29.2 -Credential $Credential Rename-SFTPFile -SessionId $session.SessionId -Path /c/pm_data/A20230622.2245-2300:1.xml.gz -Newname A20230622.2245-2300_1.xml.gz -Verbose Get-SFTPItem -SessionId 0 -Path /c/pm_data/A20230622.2245-2300_1.xml.gz -Destination c:\ericsson -Verbose
Error:
VERBOSE: Renaming /c/pm_data/A20230622.2245-2300:1.xml.gz to A20230622.2245-2300_1.xml.gzException calling "RenameFile" with "2" argument(s): "No such file"At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\3.0.8\Posh-SSH.psm1:1600 char:73+ ... $session.Session.RenameFile($Path, "$($ContainerPath)/$($NewName)")+ ~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SftpPathNotFoundException VERBOSE: File renamedGet-SFTPItem : /c/pm_data/A20230622.2245-2300_1.xml.gz does not exist.At line:6 char:5+ Get-SFTPItem -SessionId 0 -Path /c/pm_data/A20230622.2245-2300_1. ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (SSH.SftpSession:SftpSession) [Get-SFTPItem], SftpPathNotFoundException + FullyQualifiedErrorId : /c/pm_data/A20230622.2245-2300_1.xml.gz does not exist.,SSH.GetSftpItem
Best Regards
On Friday, June 23, 2023 at 09:26:05 AM GMT+3:30, Max Kozlov ***@***.***> wrote:
@mehranr81, That's why I deleted my comment :)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
just checked how it works $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
... |
Dear MaxIt was not working , while it has illegal character, windows couldn't have access to it!
I checked FileZilla FTP client, filename has colon in it, but when you download it by a rightclick, program will change : to _ !!
On Friday, June 23, 2023 at 01:01:37 PM GMT+3:30, Max Kozlov ***@***.***> wrote:
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/mkozlov
...
FullName : /home/username/test_file
LastAccessTime : 23.06.2023 12:24:26
LastWriteTime : 23.06.2023 12:24:32
Length : 9
UserId : 2004
...
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@mehranr81, filezilla has own functions :) but Posh-SSH keep original 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 |
Dear MaxMany Thanks for your kind support.In my thought, 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,these files can be renamed using it's customized linux but we need to dl files and extract some values every 15 minutes to monitor some counters therefore automated downloading is the first matter for me.Could I install linux PC ,powershell and posh-ssh?It may help!
Best Regards
On Friday, June 23, 2023 at 03:21:08 PM GMT+3:30, Max Kozlov ***@***.***> wrote:
@mehranr81, filezilla has own functions :)
but Posh-SSH keep original name
https://github.com/darkoperator/Posh-SSH/blob/f00564d72ff566d446b9e858e89bff5c1a0a3013/Source/PoshSSH/PoshSSH/GetSftpItem.cs#L180
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
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Dear Max I remembered I had found ftp client code and it was working fine, but in this case it faces with almost same problem :
#FTP Server Information - SET VARIABLES$ftp = "ftp://12.18.29.2" $user = '...' $pass = '...'$folder = '/c/pm_data'$target = "c:\eric2\"
#SET CREDENTIALS$credentials = new-object System.Net.NetworkCredential($user, $pass)
function Get-FtpDir ($url,$credentials) { $request = [Net.WebRequest]::Create($url) $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory if ($credentials) { $request.Credentials = $credentials } $response = $request.GetResponse() $reader = New-Object IO.StreamReader $response.GetResponseStream() $reader.ReadToEnd() $reader.Close() $response.Close()}
#SET FOLDER PATH$folderPath= $ftp + $folder + "/"
$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials$files = ($Allfiles -split "`r`n")
$files
$webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) $counter = 0
foreach ($file in ($files | where {$_ -like "A*.gz"})){ $source=$folderPath + $file $destination = $target + $file $webclient.DownloadFile($source, $target+$file)
#PRINT FILE NAME AND COUNTER $counter++ $counter $source}-----------------
197ftp://172.18.29.2/c/pm_data/A20230622.2200-2215:1.xml.gzException calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."At C:\Users\Administrator\Desktop\Download.ps1:40 char:5+ $webclient.DownloadFile($source, $target+$file)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException 198ftp://172.18.29.2/c/pm_data/A20230622.2215-2230:1.xml.gzException calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."At C:\Users\Administrator\Desktop\Download.ps1:40 char:5+ $webclient.DownloadFile($source, $target+$file)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
Best Regards
On Friday, June 23, 2023 at 03:21:08 PM GMT+3:30, Max Kozlov ***@***.***> wrote:
@mehranr81, filezilla has own functions :)
but Posh-SSH keep original name
https://github.com/darkoperator/Posh-SSH/blob/f00564d72ff566d446b9e858e89bff5c1a0a3013/Source/PoshSSH/PoshSSH/GetSftpItem.cs#L180
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
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@mehranr81, you wrote
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 |
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.
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 {
}
Close the SFTP session
Remove-SftpSession -SessionId $Session.SessionId
The text was updated successfully, but these errors were encountered: