using Rebex.Net;

Welcome to using Rebex.Net; Sign in | Join | Help
in Search

Using Rebex.Net

News and announcements about Rebex.NET components

HOWTO: Using FTP or SFTP in PowerShell

PowerShell - a powerful new scripting language and command shell from Microsoft - has many built-in commands. However, it lacks support for transferring files over FTP, FTP/SSL or SFTP. Luckily, PowerShell is based on .NET and can invoke methods of .NET classes. Let's see how to upload or download files to an FTP or SFTP server from a PowerShell script using either Rebex FTP (/SSL) or Rebex SFTP component.

Step 1 - Loading FTP or SFTP assembly

First, we have to load Rebex assemblies via [Reflection.Assembly]::LoadFrom command. This will load Rebex.Net.Ftp.dll for FTP or FTP/SSL and Rebex.Net.Sftp.dll for SFTP. Other assemblies needed by the two will be loaded as well.

PS C:\> [Reflection.Assembly]::LoadFrom("C:\Program Files\Rebex\FTP for .NET 2.0\bin\release\Rebex.Net.Ftp.dll")

GAC Version Location
--- ------- --------
False v2.0.50727 C:\Program Files\Rebex\FTP for .NET 2.0\bin\release\Rebex.Net.Ftp.dll

Step 2 - Connecting, logging in, uploading, downloading and deleting files

Now, you can call the FTP methods as described in the FTP Tutorial:
PS C:\> [Reflection.Assembly]::LoadFrom("C:\Program Files\Rebex\FTP for .NET 2.0\bin\release\Rebex.Net.Ftp.dll")

GAC Version Location
--- ------- --------
False v2.0.50727 C:\Program Files\Rebex\FTP for .NET 2.0\bin\release\Rebex.Net.Ftp.dll

PS C:\> $ftp = New-Object Rebex.Net.Ftp
PS C:\> $ftp.Connect("ftp.rebex.net")
220 Gene6 FTP Server v3.7.0 (Build 24) ready...

PS C:\> $ftp.Login("username", "password")
230 User logged in.

PS C:\> $ftp.PutFile("C:\temp\test.txt", "test.txt")
89
PS C:\> $ftp.GetFile("test.txt", "C:\temp\test2.txt")
89
PS C:\> $ftp.DeleteFile("test.txt")
PS C:\> $ftp.Disconnect()
221 Goodbye.


PS C:\> $ftp.Dispose()
PS C:\> Remove-Variable ftp
PS C:\>

That's all!

What about GAC?

And in case you would like to load Rebex assemblies from Global Assembly Cache (GAC) instead of from a path, use Assembly.Load method instead:

PS C:\> [Reflection.assembly]::Load("Rebex.Net.Ftp, Version=2.5.3127.0, Culture=neutral, PublicKeyToken=1c4638788972655d")

GAC Version Location
--- ------- --------
True   v2.0.50727     C:\WINDOWS\assembly\GAC\Rebex.Net.Ftp\2.5.3127.0__1c4638788972655d\Rebex.Net.Ftp.dll


Published Thursday, September 25, 2008 4:00 PM by lukas.matyska
Filed under: , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems