SocketTools and PowerShell

PowerShell is Microsoft's command line shell and scripting development environment for the Windows platforms. It primarily serves as both a replacement for the classic command line (cmd.exe) and as an administrative tool that integrates much of the functionality that was previously found in other tools like Windows Scripting Host (WSH) and the Network Shell (netsh).

The PowerShell scripting language is similar to C# and provides many of the typical statements in a scripting language (conditional statements, looping control, switch statements and so on) as well as support for regular expressions, array slicing and hash tables. It also supports scoping, allowing you to declare variables to be global or local scope. In addition to many of its features, PowerShell can also make use of .NET assemblies, enabling you to take advantage of the expansive .NET Framework in your own scripts. This opens a lot of possibilities, both for the typical administrative tasks one could perform, but also allows developers to use PowerShell as a test-harness and prototyping tool for their own projects.

With the SocketTools .NET Edition, you can create scripts using the same classes that you use when building your applications. If you are already familiar with SocketTools, then there is virtually no learning curve aside from some minor syntactical differences. Note that even if you're not familiar with C#, you'll find the PowerShell scripting language easy to work with.

Getting Started
To demonstrate how to use SocketTools with PowerShell, the first step is to install the SocketTools .NET Edition on your local system. If you don't already have a copy of the SocketTools .NET Edition, you can download and install it with a free evaluation license. Once you have everything installed, fire up your copy of PowerShell.

To use one of the SocketTools assemblies, you need to load it. This can be done using the LoadFile method in the System.Reflection.Assembly class. Simply pass the full file name of the assembly as its argument. The current version of the SocketTools assemblies are found in the following folder:

C:\Program Files (x86)\Common Files\SocketTools\9.5\Assemblies\v4.5

So let's say that you want to use the FTP class to download a file to the local system. You would enter the following:

[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Common Files\SocketTools\9.5\Assemblies\v4.5\SocketTools.FtpClient.dll")

If you've entered the path correctly, you'll get a message that shows the version and location of the assembly that's been loaded. The next step is to create an instance of the SocketTools.FtpClient class:

$FtpClient = New-Object SocketTools.FtpClient

Using the SocketTools Class
The $FtpClient variable now references an instance of the SocketTools.FtpClient class. With that done, you now have access to all of the various properties and methods. To see a list of the current property values, you can simply enter:

$FtpClient

To download a file, you can use the GetFile method. For example, you could enter:

$FtpClient.GetFile("c:rfc959.txt", "ftp://ftp.ietf.org/rfc/rfc959.txt")

The first argument is the name of the file on the local system, and the second argument is the FTP URL that specifies the remote location of the file. An important point to keep in mind is that whenever you specify a path to a method in a .NET class (whether in SocketTools or some other class) you should always specify a full path. This is important because PowerShell keeps track of its current working directory independently of what the assembly considers to be the current working directory. To avoid any possible confusion, it's always best to be explicit, otherwise you can get some unexpected results.

You've downloaded a file and stored it on the local system, but what if you want to manipulate the contents of the file in memory, instead of writing it to disk? Instead of using the GetFile method, use the GetData method instead. It accepts two arguments, the URL of the file that you want to download, and a buffer to store the contents of the file in. To do this, first we need to create a string variable that will contain the file data when the method returns:

$buffer = ""

This is important because the second argument to the GetData method must be passed by reference. To do this, the variable must already exist and the [ref] attribute must be specified for the parameter. Here's an example:

$FtpClient.GetData("ftp://ftp.ietf.org/rfc/rfc959.txt", [ref] $buffer)

When the GetData method returns, the $buffer variable will contain the contents of the file and you can manipulate it as you would any string type.

More Information
Hopefully will give you an idea of how you can use SocketTools with PowerShell as a means to accomplish administrative tasks, as well as help you with your development projects. Here's some more links to help you get started:

Shopping Cart
Scroll to Top