Question:
I have searched and read a lot about downloading SSL FTP files in PowerShell… But how do I upload them? I have a filename I need to upload each day and I get errors that I am not logged in.
I’m currently uploading to other FTP sites like this:
1 2 3 4 5 6 7 |
$Filename = "myfile.txt" $FullPath = "\\server\share\$Filename" $ftp = "ftp://user:pass@ftp.domain.com/$Filename" $ftpWebClient = New-Object System.Net.WebClient $ULuri= New-Object System.URI($ftp) $ftpWebClient.UploadFile($ULuri, $FullPath) |
Do I need to create a whole new block of code for the SSL FTP upload, or do I just need to make minor adjustments to this code?
Thanks!
Answer:
I use the WinSCP DLL to do this stuff. Check out some examples here:
http://winscp.net/eng/docs/library#powershell
Here’s some of my sample code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[Reflection.Assembly]::LoadFrom("D:\WinSCP.dll") | Out-Null $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $sessionOptions.HostName = "192.168.1.120" $sessionOptions.UserName = "user" $sessionOptions.Password = "pass" $session = New-Object WinSCP.Session $session.Open($sessionOptions) #upload stuff here, check the link for detail on how, and use powershell to populate your file list! $session.Dispose() |