ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


Powershell FTP Client Sample Source Code

ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket features the following: ICMP, HTTP and HTTPs with support for proxy servers and secure web sites, Telnet, NTP time protocol, RSH remote shell script interface, SNMP (Simple Network Management Protcol), SNMP Traps, Sockets, WOL (Wake-On-LAN), and more.

ActiveSocket can be well integrated into ASP environments. This document describes how the ActiveSocket FtpServer object can be integrated into Powershell projects.

The most important functions of the FtpServer object are:

  • Connect - connect to the (remote) FTP server on port 21 or any alternate port;
  • Disconnect - to diconnect after a connect call;
  • GetCurrentDir - retrieve the current directory;
  • ChangeDir - change the current directory;
  • CreateDir - create a new directory;
  • RenameDir - rename a directory;
  • DeleteDir - delete a directory;
  • FindFile - find a specific file in the current directory;
  • FindFirstFile - iterate over all files in the current directory; find the first file;
  • FindNextFile - iterate over all files in the current directory; find the next file;
  • RenameFile - rename a file in the current directory;
  • DeleteFile - delete a file in the current directory;
  • GetFile - get (download) a file, either using binary transfer or ASCII transfer;
  • PutFile - put (upload) a file, either using binary transfer or ASCII transfer;

Step 1: Download and install the ActiveSocket Toolkit

Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new script

Create a new script using your favorite editor. You can simply use notepad. However, a Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced script to communicate using the ActiveSocket Toolkit.

Step 3: Create the ActiveSocket object in Powershell

Create a new Powershell file called DEMO.PS1.

This statement requires that all variable names be defined (with the Dim statement), to avoid simple typos that can cause incredible headaches and long debugging sessions for something that should have never happened.

Create the ActiveSocket object(s) like this:

$objFtpServer = new-object -comobject ActiveXperts.FtpServer

Now, add the following lines to the file to have your fist ActiveSocket Powershell program:

WScript.Echo "Version: " & objFtpServer.Version
WScript.Echo "Expiration Date: " & objFtpServer.Expiration Date

Step 4: Connect to a remote FTP server, change directory and list all files

You can now connect to a remote FTP server, logon, change directory and perform file operations.

The following Powershell code shows how to list files in a specific directory on a remote FTP server:

#################################################################################
# ActiveSocket - Powershell script
# © 1999-2009, ActiveXperts Software B.V.
#
# For more information about ActiveSocket, please
# visit the online ActiveSocket page at:
# http://www.activexperts.com
#################################################################################
# DNS Sample: simple sample to show how to lookup a hostname on a dns 
# ActiveSocket sample - FTPGET
#  FTP Sample: Connect to an FTP server, and show all files in a specific directory
#################################################################################

cls

#################################################################################
# THE SCRIPT ITSELF ------------------------------------------------------------#
#################################################################################

$objFtpServer = new-object -comobject ActiveXperts.FtpServer

# Write some information to console
Write-Host "ActiveSocket Version " $objFtpServer.Version "; Build " $objFtpServer.Build "; Module " $objFtpServer.Module
Write-Host "Expiration date: " $objFtpServer.ExpirationDate

# Log all FTP operations
$objFtpServer.LogFile = "C:\ftpget.txt"

# Connect to the remote FTP server
$objFtpServer.Connect("ftp.activexperts-labs.com", "anonymous", "me@myself.dom")
Write-Host "Connect, result: " $objFtpServer.LastError " (" $objFtpServer.GetErrorDescription($objFtpServer.LastError) ")"

if( $objFtpServer.LastError -ne 0 ) 
	{
		Write-Host "Ready."
		exit
	}	

# Show last response of FTP server
Write-Host "Last response: " $objFtpServer.LastResponse

# Use binary transfer for GetFile calls
$objFtpServer.BinaryTransfer = $true

# Change directory
$objFtpServer.ChangeDir("/Samples/ASocket" )
Write-Host "ChangeDir, result: " $objFtpServer.LastError " (" $objFtpServer.GetErrorDescription($objFtpServer.LastError) ")"
if( $objFtpServer.LastError -ne 0 )
	{
		$objFtpServer.Disconnect
		Write-Host "Disconnected."
		Write-Host "Ready."
		exit
	}	

# Iterate over all files
$objFtpFile =$objFtpServer.FindFirstFile()
Write-Host "Find file, result: " $objFtpServer.LastError " (" $objFtpServer.GetErrorDescription($objFtpServer.LastError) ")"
Write-Host "Last response: " $objFtpServer.LastResponse()

while($objFtpServer.LastError -eq 0)
	{
		Write-Host "Name: " $objFtpFile.Name 
		Write-Host "   IsDirectory: " $objFtpFile.IsDirectory
		Write-Host "   Size (bytes): " $objFtpFile.Size
		Write-Host "   Creation date (seconds): " $objFtpFile.DateSeconds
		Write-Host "   Creation date: " $objFtpFile.Date

		# To save the file, call the GetFile function. 
		# $strSaveAs = "C:\temp\" $objFtpFile.Name
		# $objFtpServer.GetFile($objFtpFile.Name, $strSaveAs )
		# Write-Host "Save file as " $strSaveAs ", result: " $objFtpServer.LastError " (" $objFtpServer.GetErrorDescription($objFtpServer.LastError) ")"

		# To delete a file, call the DeleteFile function. 
		# $objFtpServer.DeleteFile $objFtpFile.Name
		# Write-Host "Delete file, result: " $objFtpServer.LastError " (" $objFtpServer.GetErrorDescription($objFtpServer.LastError) ")"

		$objFtpFile = $objFtpServer.FindNextFile()
		Write-Host "Find file, result: " $objFtpServer.LastError " (" $objFtpServer.GetErrorDescription($objFtpServer.LastError) ")"
	}
	
$objFtpServer.Disconnect()
Write-Host "Disconnected."
Write-Host "Ready."

To run the code, start Powershell and browse to the location of the file you just created. Enter .\Demo.ps1 to run the code. Notice that if the script is not working, you have to change the execution policy; you can do that with the following command:

Set-ExecutionPolicy -unrestricted

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/asocket.