Perfect
You're a champ Martin! I had looked at the constructor yesterday and couldn't figure out why what I had in there looked wrong. Thanks a tonne!
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Ascii
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferResult = $session.PutFiles($PGCSV, $PGFTP, $transferOptions)
Upload of C:\[PATH]\[FILE].csv failed: Transfer was successfully finished,
but temporary transfer file '[FILE].csv.filepart' could not be renamed to
target file name '[FILE].csv'. If the problem persists, you may try to turn off
transfer resume support.
Permission denied.
Error code: 3
Error message from server: Permission denied.
param (
#Out to Dest
$PGCSV = "C:\[PATH]\*.csv",
#In at Dest
$PGFTP = "/Inbox/",
#Transmitted files archive
$PGArc = "C:\[PATH]\"
)
try
{#Connection
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "[HOST]"
UserName = "[USER]"
Password = "[PASS]"
SshHostKeyFingerprint = "[KEY]"
}
$session = New-Object WinSCP.Session
$session.AddRawConfiguration("Interface\LocaleSafe", "1033")
$session.SessionLogPath = "C:\[PATH]\[FILE].log"
Write-Output "-----Start-----"
Get-Date -UFormat "%m/%d/%Y %R:%S"
# Connect
Write-Output "Opening session..."
$session.Open($sessionOptions)
try
{#CSV
# Upload files, collect results
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Ascii
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferResult = $session.PutFiles($PGCSV, $PGFTP, $transferOptions)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Output "Upload of $($transfer.FileName) succeeded, moving to backup..."
# Upload succeeded, move source file to backup
Move-Item -Path $transfer.FileName -Destination $PGArc
}
else
{
Write-Output "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
}
catch
{#CSV
Write-Output "PG Error: $($_.Exception.Message)"
exit 1
}
finally
{
Write-Output "Disconnecting session, cleaning variables..."
# Disconnect, clean up
$session.Dispose()
Get-Date -UFormat "%m/%d/%Y %R:%S"
Write-Output "-----End-----"
}
exit 0
}
catch
{
Write-Output "Error: $($_.Exception.Message)"
Get-Date -UFormat "%m/%d/%Y %R:%S"
Write-Output "-----End-----"
exit 1
}