ftp/Simple.ps1

#########################################################
# 
# Summary: 
# This script demonstrates the following features:
# - Connect
# - Download of file
# - Read drectory (enumeration)
# - Disconnect
#
# Notes:
# If you get the following error:
# "cannot be loaded because the execution of scripts is disabled on this system."
# Enable the execution of scripts:
# Set-ExecutionPolicy RemoteSigned
#
# Technical support: support@smartftp.com
#
# Copyright (c) by SmartSoft Ltd.
#########################################################

# abort on first error
$ErrorActionPreference = "Stop"

# to use the interop assembly, powershell 5.0 (CLR 4) or higher is required
if([Environment]::Is64BitProcess)
{
	[void][Reflection.Assembly]::LoadFrom(${env:ProgramFiles(x86)} + '\SmartFTP FTP Library\x64\Interop.sfFTPLib.dll')
}
else
{
	[void][Reflection.Assembly]::LoadFrom(${env:ProgramFiles} + '\SmartFTP FTP Library\Interop.sfFTPLib.dll')
}

$global = New-Object sfFTPLib.GlobalClass;
# If LoadLicense is not called a trial license is automatically obtained from the activation server. The FTP Library uses WinHTTP to access
# the activation server at www.smartftp.com (TLS, port 443). Ensure that your application is not blocked by any firewall.
# TODO: insert the provided serial after the purchase of a license
# $global.LoadLicense("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

$ftp = New-Object sfFTPLib.FTPConnectionMTAClass;
$ftp.Host = "ftp.smartftp.com";
# For an unencrypted connection, comment out the next line
$ftp.Protocol = [sfFTPLib.Protocol]::ftpProtocolRequireTLS;
#$ftp.Port = 21; # 21 is the default port
$ftp.Username = "anonymous";
$ftp.Password = "test@test.com";
#$ftp.Passive = $true; # enabled by default

# client certificate
# If you have a certificate file, you can use SmartFTP Client to import it into the personal Windows certificate store "My"
# 20 byte certificate thumbprint which identifies the certificate
[Byte[]] $certificateThumbprint = 0x12, 0x13, 0x14, 0x15, 0x16, 0x12, 0x13, 0x14, 0x15, 0x16, 0x12, 0x13, 0x14, 0x15, 0x16, 0x12, 0x13, 0x14, 0x15, 0x16;
#$ftp.SSLSocketLayer.ClientCertThumbprint = $certificateThumbprint;

# $fileLogger = $ftp.SetFileLogger();
# $fileLogger.File = "C:\test.log";

$ftp.Connect();
Write-Host "Connected";
$ftp.ChangeDirectory("/");
foreach ($item in $ftp.ReadDirectory())
{
	# if you do not use the interop, the ModifyTimeAsDate must be converted with:
	# [System.DateTime]::FromOADate($item.ModifyTimeAsDate).ToString("o")
	Write-Host ("Name={0}, Type={1}, Size={2}, ModifyTime={3}" -f $item.Name, $item.Type, $item.Size, $item.ModifyTimeAsDate)
}
		
$ftp.DownloadFile("This is a DEMO server.txt", "This is a DEMO server.txt", 0,0);
Write-Host "DownloadFile succeeded"
$ftp.Disconnect();