1

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()
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

1

I am using the PowerShell wrapper and I had to pass the transfer options during upload and download commands.

Here is what I am using:

$TransferOptions = New-WinSCPTransferOption -ResumeSupport $(New-WinSCPTransferResumeSupport -State Off)

Send-WinSCPItem -WinSCPSession $Session -TransferOptions $TransferOptions -Path "C:\test.txt" -Destination "/test/path" -Verbose

Without the -TransferOptions I see .filepart is created but with -TransferOptions no .filepart.

Try passing the transfer option when you call the .PutFiles. Something like this:

$session.PutFiles($MCRline, $remotePath, $False, $transferOptions).Check()

Check this link out: https://winscp.net/forum/viewtopic.php?t=23217

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
LT-
  • 673
  • 2
  • 7
  • 19