Help with SFTP Download Script
I need help. I am trying to build a script that downloads text files via SFTP from a remote site. I am using Powershell 3.0. I've tinkered with Powershell and SFTP add-on, but I'm not getting anywhere with that either. There are always going to be multiple text files and I only want it to grab and pull down those text files. It connects, but it does not download the files where I need them. Script is below.
try { $logpath = "D:\Report-Copy-Move\Logs\CDCFH-demo_download_results.txt" #Source Folders, use absolute path #These folders must only have files that are to be downloaded $CDCFH_IN = "/username/RDA/IN/CC/MS4/DEM/" #Target Folders, use absolute path $CDCFH_OUT = "\\transferserver\dignity-billing\agch_fhmc\" #Archive Folder Location, use absolute path #$CDCFH_Archive = "D:\ininbound\ftp-archive\cdc-archive\" #Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" #Setup session options $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::sftp $sessionOptions.HostName = "mft.dignityhealth.org" $sessionOptions.UserName = "username" $sessionOptions.Password = "password" $sessionOptions.PortNumber = "xxxx" #Use the following line with the SSH HostKey Fingerprint by replacing the xx:xx:xx... $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 97:83:f1:ec:8c:c4:3b:78:29:d9:c5:10:c8:fe:93:08" #ONLY FOR TEST PURPOSES, DO NOT USE AS IS #$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true #$sessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate = true $session = New-Object WinSCP.Session $session.SessionLogPath = "D:\Report-Copy-Move\Logs\CDCFH-demo_download-session-log.txt" try { #Connect $session.Open($sessionOptions) #Force binary mode transfer $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary #$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off $transferOptions.FilePermissions = $Null # This is default $transferOptions.PreserveTimestamp = $False #Upload file to the SFTP server directory #Note use of absolute path $transferResult_CDCFH = $session.GetFiles($CDCFH_IN, $CDCFH_OUT, $False, $transferOptions) #Throw on any error to emulate "option batch abort" $transferResult_CDCFH.Check() #Printing output for CDC and FH $cdcfh_counter = 0 foreach ($transfer in $transferResult_CDCFH.Transfers) { #Success or error? if ($transfer.Error -eq $Null) { #Print results Add-content -Path $logpath -value "Download of {0} succeeded $($transfer.FileName)" $cdcfh_counter++ #Upload succeeded, delete source files Remove-Item $transfer.FileName $CDCFH_IN } else { #Upload Failed, throw error Add-content -Path $logpath -value "Upload failed: {1} $($transfer.FileName) $($transfer.Error.Message)" } } Add-content -Path $logPath -value "$($cdcfh_counter) files successfully downloaded from DIGNITY." } finally { #Disconnect, clean up $session.Dispose() Add-content -Path $logpath -value "Download of Dignity demographics completed" } exit 0 } catch [Exception] { Add-content -path $logpath -value $_.Exception.Message exit 1 }