I want to download some folders and files from another server via FTP using PowerShell PSFTP module. I want to download all files and folders from the specified address.
Following this answer I wrote the next script:
$User = "xxxxx"
$Password = ConvertTo-SecureString "xxxx" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $Password)
$ftp_server = "ftp://backup.xxxxx.eu"
$ftp_path = "backup_old/xxx/xx_Config_Backup"
$local_path = "D:\Backup\backup_old\xxx"
Set-FTPConnection -Credentials $Credentials -Server $ftp_server -EnableSsl -ignoreCert -UsePassive
Get-FTPChildItem -path $ftp_path -Recurse | Get-FTPItem -localpath $local_path -Overwrite -Verbose #-RecreateFolders
This script works fine when ftp_path's folder consists of just files.
ContentLength : -1
Headers : {}
SupportsHeaders : True
ResponseUri : ftp://backup.xxxxx.eu/
StatusCode : ClosingData
StatusDescription : 226 Transfer complete.
LastModified : 01.01.0001 0:00:00
BannerMessage : 220 Microsoft FTP Service
WelcomeMessage : 230 User logged in.
ExitMessage : 221 Goodbye.
IsFromCache : False
IsMutuallyAuthenticated : False
ContentType :
VERBOSE: Performing the operation "Download item: 'ftp://backup.xxxxx.eu
/backup_old/xxx/QlikView/xxx.qvw'" on target "".
226 Transfer complete.
VERBOSE: Performing the operation "Download item: 'ftp://backup.xxxxx.eu
/backup_old/xxx/QlikView/xxxFulFillment.qvw'" on target "D:\Backup\backu
p_old\xxx\xxx.qvw".
226 Transfer complete.
But when it consists of additional folders with files, it creates a file with zero-size named same as folder and crashes with 550 error.
ContentLength : -1
Headers : {}
SupportsHeaders : True
ResponseUri : ftp://backup.xxxxx.eu/
StatusCode : ClosingData
StatusDescription : 226 Transfer complete.
LastModified : 01.01.0001 0:00:00
BannerMessage : 220 Microsoft FTP Service
WelcomeMessage : 230 User logged in.
ExitMessage : 221 Goodbye.
IsFromCache : False
IsMutuallyAuthenticated : False
ContentType :
VERBOSE: Performing the operation "Download item: 'ftp://backup.xxxxx.eu
/backup_old/xxx/xx_Config_Backup/AlertSender'" on target "".
Get-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote
server returned an error: (550) File unavailable (e.g., file not found, no acc
ess)."
At C:\PS_Scrips\PSFTP_1.ps1:13 char:45
... -Recurse | Get-FTPItem -localpath $local_path -Overwrite -Verbose #- ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
tion
FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
n,Get-FTPItem
Help me please understand the cause of the problem and fix it. Thanks.
Additionally.
- Executing
Get-FTPChildItem -path $ftp_path -Recurse | Get-FTPItem -localpath $local_path -Overwrite -Verbose -RecreateFolders
returned the same errorGet-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no acc ess)." At C:\PS_Scrips\PSFTP_1.ps1:13 char:45 ... -Recurse | Get-FTPItem -localpath $local_path -Overwrite -Verbose -R ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep tion FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio n,Get-FTPItem
- Executing just
Get-FTPChildItem -path $ftp_path -Recurse
returned https://pastebin.com/t9F5Th59
CodePudding user response:
You probably have to filter-out the directory entries:
Get-FTPChildItem -path $ftp_path -Recurse |
Where-Object { $_.Dir -ne "DIR" } |
Get-FTPItem -localpath $local_path -Overwrite -Verbose -RecreateFolders