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
#
# Copyright (c) by SmartSoft Ltd.
#########################################################
# If you want to use the interop assembly (you need to create it first) use:
# [Reflection.Assembly]::LoadWithPartialname("Interop.sfFTPLib") | out-null
# For more information: http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan09/hey0126.mspx
# Creates new COM object
$ftp = new-object -com sfFTPLib.FTPConnectionMTA;
$ftp.Host = "ftp.smartftp.com";
$ftp.Port = 21;
$ftp.Username = "anonymous";
$ftp.Password = "test@test.com";
$ftp.Passive = 1;
# $ftp.LogFile = "C:\test.log";
#/*sfFTPLib.enumError.ftpErrorSuccess*/
if($ftp.Connect() -eq 0)
{
Write-Host "Connected";
$ret = $ftp.ChangeDirectory("/SmartFTP");
if($ret -eq 0)
{
$ret = $ftp.ReadDirectory();
if($ret -eq 0)
{
Write-Host "ReadDirectory succeeded"
foreach($item in $ftp.Items)
{
Write-Host ("Name={0}, Type={1}, Size={2}, ModifyTime={3}" -f $item.Name, $item.Type, $item.Size, [System.DateTime]::FromOADate($item.ModifyTimeAsDate).ToString("o"))
}
$ret = $ftp.DownloadFile("License.txt", "License.txt", 0,0);
if($ret -eq 0)
{
Write-Host "DownloadFile succeeded"
}
}
}
$ftp.Disconnect();
}
else
{
Write-Host ("Connection failed. LastError={0}" -f $ftp.LastError)
}