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.
Adding -RecreateFolders
:
Get-FTPChildItem -path $ftp_path -Recurse | Get-FTPItem -localpath $local_path -Overwrite -Verbose -RecreateFolders
results in the the same error
Get-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." 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
Get-FTPChildItem -path $ftp_path -Recurse |
Where-Object { $_.Dir -ne "DIR" } |
Get-FTPItem -localpath $local_path -Overwrite -Verbose -RecreateFolders
and again get the same error:
Get-FTPChildItem : Exception calling "GetResponse" with "0" argument(s): "The remote serve
r returned an error: (550) File unavailable (e.g., file not found, no access)."
At line:14 char:1
+ Get-FTPChildItem -path $ftp_path -Recurse |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-FTPCh
ildItem
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