Quicklinks
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: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
ActiveSocket can be well integrated into ASP environments. This document describes how the ActiveSocket SFtp object can be integrated into Powershell projects.
The most important functions of the SFtp object are:
Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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.
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:
$objSFtp = new-object -comobject ActiveXperts.SFtp
Now, add the following lines to the file to have your fist ActiveSocket Powershell program:
Write-Host "Version: " $objSFtp.Version Write-Host "Expiration Date: " $objSFtp.ExpirationDate
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 # © Copyright 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 ------------------------------------------------------------# ################################################################################# $objSFtp = new-object -comobject ActiveXperts.SFtp # Write some information to console Write-Host "ActiveSocket Version " $objSFtp.Version "; Build " $objSFtp.Build "; Module " $objSFtp.Module Write-Host "Expiration date: " $objSFtp.ExpirationDate # Log all FTP operations $objSFtp.LogFile = "C:\ftpget.txt" # Connect to the remote FTP server $objSFtp.Host = "192.168.1.10" $objSFtp.UserName = "demo" $objSFtp.Password = "topsecret" $objSFtp.Connect() Write-Host "Connect, result: " $objSFtp.LastError " (" $objSFtp.GetErrorDescription($objSFtp.LastError) ")" if( $objSFtp.LastError -ne 0 ) { Write-Host "Ready." exit } # Show last response of FTP server Write-Host "Last response: " $objSFtp.LastResponse # Change directory $objSFtp.ChangeDir("/samples/network-component" ) Write-Host "ChangeDir, result: " $objSFtp.LastError " (" $objSFtp.GetErrorDescription($objSFtp.LastError) ")" if( $objSFtp.LastError -ne 0 ) { $objSFtp.Disconnect Write-Host "Disconnected." Write-Host "Ready." exit } # Iterate over all files $objFtpFile =$objSFtp.FindFirstFile(".") Write-Host "Find file, result: " $objSFtp.LastError " (" $objSFtp.GetErrorDescription($objSFtp.LastError) ")" Write-Host "Last response: " $objSFtp.LastResponse() while($objSFtp.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 # $objSFtp.GetFile($objFtpFile.Name, $strSaveAs ) # Write-Host "Save file as " $strSaveAs ", result: " $objSFtp.LastError " (" $objSFtp.GetErrorDescription($objSFtp.LastError) ")" # To delete a file, call the DeleteFile function. # $objSFtp.DeleteFile $objFtpFile.Name # Write-Host "Delete file, result: " $objSFtp.LastError " (" $objSFtp.GetErrorDescription($objSFtp.LastError) ")" $objFtpFile = $objSFtp.FindNextFile() Write-Host "Find file, result: " $objSFtp.LastError " (" $objSFtp.GetErrorDescription($objSFtp.LastError) ")" } $objSFtp.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/network-component.