I have written a PowerShell script to upload a file to a remote FTP server. I successfully connect to the server but during the upload, it creates a temporary file using the filepart
in the name which is the exact size of my local file but never renames the temporary file back to the original local file name. Since the file name is not what the server expected to see, it gets quarantined. I would like to know if there is a way to avoid creating this temporary file during the upload? I'm using the latest version of PowerShell ISE to run the script.
Here is my code:
Add-Type -Path ("C:\Program Files (x86)\WinSCP\WinSCPnet.dll")
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "***.***.***.***"
PortNumber = ****
UserName = "***********"
Password = '*******'
SshHostKeyFingerprint = "ssh-rsa 2048 9c:cb:ea:c2:25:50:cf:4e:eb:97:6c:ad:9b:28:a9:25"
GiveUpSecurityAndAcceptAnySshHostKey = "True"
}
$session = New-Object WinSCP.Session
$session.SessionLogPath = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\UploadLog.log"
# Logging Configuration
$date = Get-Date -Format MM-dd-yy
$timestamp = Get-Date -Format "MM/dd/yyyy h:mm:s"
$logFile = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\moveMRDF_$($date).log"
Add-Content -Path $logFile "Start of logging"
Add-Content -Path $logFile "MRDF File Processing Log"
Add-Content -Path $logFile $timestamp
$date = Get-Date -Format MM-dd-yy
# Setup File Paths
$prodPath = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\Reports"
$remoteProdPath = "/MRDF/"
Add-Content -Path $logfile $prodPath
Add-Content -Path $logfile $remoteProdPath
$transferFile = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\Reports\12_20_2019_Report.zip"
Add-Content -Path $logfile $session
Add-Content -Path $logfile $transferFile
# Connect & Transfer to FTP Server
try
{
#echo $sessionOptions
$session.Open($sessionOptions)
#echo 'after open'
Add-Content -Path $logfile $session
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$session.PutFiles($transferFile, $remoteProdPath)
}
finally
{
$session.Dispose()
}