ComponentPro would like to illustrate how to use Ultimate FTP in PowerShell in this topic. PowerShell is the new command line and scripting language developed by Microsoft to helps IT professionals complete their tasks more efficiently with greater control and productivity on Microsoft Windows Platforms. The PowerShell contains more than 130 command line tools especially designed for administrators.
Windows PowerShell is built on top of the .NET Framework, so it gives administrators a wide-range of methods and extensions. With Ultimate FTP, now you can upload and download files to FTP server easily with a few lines of code. Since PowerShell accepts and returns .NET objects, FTP classes are accessible to the PowerShell, in which you can use the component to build a complex application like a C# or VB.NET application. This component can be downloaded from our website.
To get start, we begin with uploading several files with a single line of code:
Unlike many other FTP components, Ultimate FTP enables you to upload and download multiple files with different extensions with a single line of code. We take advantages of this feature with the following PowerShell sample:
# Load DLL
Add-Type -Path C:\InstalledDir\ComponentPro.Ftp.dll
# Set source path and files to upload
$sourcePath = "C:\Temp\*.ps1;*.dat;*.zip"
# Set destination path
$destinationPath = "/my dir"
# Create a new instance of the Ftp class
$ftp = New-Object ComponentPro.Net.Ftp
$ftp.Connect("myserver")
$ftp.Authenticate("user", "pass")
# Upload files
$ftp.UploadFiles($sourcePath, $destinationPath)
$ftp.Close()
$ftp.Dispose()
Now we will download files from the FTP server to the local disk:
We gives you the ease-of-use of the FTP component not only in the upload methods, but also in the download methods. The following PowerShell code example demonstrates how to load the ComponentPro.Ftp.DLL assembly and download files from the FTP server:
# Load DLL
Add-Type -Path C:\InstalledDir\ComponentPro.Ftp.dll
# Set source path and files to upload
$sourcePath = "/my dir/*.ps1;*.dat;*.zip"
# Set destination path
$destinationPath = "C:\Temp2"
# Create a new instance of the Ftp class
$ftp = New-Object ComponentPro.Net.Ftp
$ftp.Connect("myserver")
$ftp.Authenticate("user", "pass")
# Download files
$ftp.DownloadFiles($sourcePath, $destinationPath)
$ftp.Close()
$ftp.Dispose()
How about FTP/SSL?
Basically to connect to an FTP/SSL server you only have to specify the SecurityMode as the third parameter of the Connect method as shown below:
C#:
ftp.Connect("myserver", 991, SecurityMode.Explicit);
VB.NET:
ftp.Connect("myserver", 991, SecurityMode.Explicit)
However PowerShell does not understand enums defined in our .NET assemblies. We must use an “Integer” as the third parameter of the Connect method with SecurityMode.None = 0, SecurityMode.Implicit = 1, SecurityMode.Explicit = 2, and SecurityMode.TumbleweedTunnel = 3.
$ftp.Connect("myserver", 991, 2)
Now you see with Ultimate FTP Component and a little knowledge of PowerShell language, you can build a comprehensive file transfer application running on the Windows PowerShell.
Click here to download the Ultimate FTP Component for .NET, or here to download the .NET CF version.