PowerShell Error Transferring File - PutFiles?
Hi, New to PowerShell and WinSCP but trying to setup a script to upload files to an FTP server. I can connect and read the remote folder contents but when I try to send a file with
Here's my code: (Maybe I'm missing something?)
Any help is greatly appreciated.
Thanks in advance,
Linn
Update: I was able to figure out how to use the
Thanks!
PutFiles
method I get a rather generic error. Is there any way to get more detail as to what exactly the error is? Like I said I can read the file names of the remote folder using"Error transferring file"
ListDirectory
method so I'm sure I'm connecting with the FTP server all right. Just can't figure out why the transfer is failing.
Here's my code: (Maybe I'm missing something?)
try { # Load WinSCP .NET assembly Add-Type -Path "C:\Scripts\WinSCPnet.dll" # Setup variables $localPath = "C:\Scripts" $logFilePath = "C:\Scripts\Log Files" $remotePath = "/Orders" # Setup session options $sessionOptions = New-Object winscp.sessionoptions $sessionOptions.protocol = [WinSCP.Protocol]::ftp $sessionOptions.HostName = "ftp_server" $sessionOptions.username = "ftp_user" $sessionOptions.password = "password" $session = New-Object WinSCP.Session # Clear the host screen. Write-Host | Clear-Host try { # Connect $session.Open($sessionOptions) # Upload files. $transferOptions = New-Object winscp.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary $transferResult = $session.PutFiles("C:\Scripts\MyFile.CSV", "Orders", $false, $transferOptions) # Thow an error. $transferResult.Check() # Print results of transfer. foreach ($transfer in $transferResult.Transfers) { Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) } # No new orders to send or finished sending. # Check remote site for return files. $directory = $session.ListDirectory($remotePath) # Select files matching wildcard. $remoteFiles = $directory.Files | Where-Object { $_.Name -like "*.csv" } if ($remoteFiles) { # Download each return file. foreach ($file in $remoteFiles) { Write-Host ("{0} with size {1} and last modification at {2}`n" -f $file.Name, $file.length, $file.LastWriteTime) } } } finally { # Disconnect, clean up $session.Dispose() } Write-Host "Done, no errors!" exit 0 } catch [Exception] { Write-Host $_.Exception.Message Write-Host "Complete with Error!" exit 1 }
Any help is greatly appreciated.
Thanks in advance,
Linn
Update: I was able to figure out how to use the
$TransferResult.Failures
to see more details and it says access denied on the remote folder. But I can use WinSCP on the same computer to manually transfer files to that folder with the same user credentials. Any thoughts about the rights differences?
Thanks!