sftp/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");

$ssh = New-Object sfFTPLib.SSHConnectionClass;
$ssh.Host = "host";
$ssh.Port = 22; 
$ssh.Username = "user";
$ssh.Password = "password";

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

$ssh.Connect();
Write-Host "Connected";
	
$sftp  = $ssh.CreateSFTPConnection();	
# $fileLogger = $sftp.SetFileLogger();
# $fileLogger.File = "C:\sftp.log";

$sftp.Connect();

Write-Host "SFTP channel opened";

# gets current directory
$home = $sftp.RealPath(".");
Write-Host ("RealPath succeeded. Home={0}" -f $home)
	
foreach($item in $sftp.ReadDirectory($home))
{
	# 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)
}
			
# Use the absolute path to the source file
$sftp.DownloadFileEx("/License.txt", "License.txt", [sfFTPLib.DataTransferType]::ftpDataTransferTypeImage, 0, 0, $null);
Write-Host "DownloadFile succeeded"
$ssh.Disconnect();