Problem
You want to access the functionality exposed by a .NET DLL, but that DLL is packaged as part of a developeroriented Software Development Kit (SDK).
Solution
To create objects contained in a DLL, use the [System.Reflection.Assembly]:: LoadFile() method to load the DLL, and the NewObject cmdlet to create objects contained in it. Example 159 illustrates this technique.
Example 159. Interacting with classes from the SharpZipLib SDK DLL
[Reflection.Assembly]::LoadFile("d:\bin\ICSharpCode.SharpZipLib.dll") $namespace = "ICSharpCode.SharpZipLib.Zip.{0}"
$zipName = JoinPath (GetLocation) "PowerShell_TDG_Scripts.zip" $zipFile = NewObject ($namespace f "ZipOutputStream") ([IO.File]::Create($zipName))
foreach($file in dir *.ps1)
{
$zipEntry = NewObject ($namespace f "ZipEntry") $file.Name
$zipFile.PutNextEntry($zipEntry) }
$zipFile.Close()
Discussion
While C# and VB.Net developers are usually the consumers of SDKs created for the .NET Framework, PowerShell lets you access the SDK features just as easily. To do this, use the [Reflection.Assembly]::LoadFile() method to load the SDK assembly, and then work with the classes from that assembly as you would work with other classes in the .NET Framework.
Although PowerShell lets you access developeroriented SDKs easily, it can’t change the fact that these SDKs are developeroriented. SDKs and programming interfaces are rarely designed with the administra
tor in mind, so be prepared to work with programming models that require multiple steps to accomplish your task.
One thing you will notice when working with classes from an SDK is that it quickly becomes tiresome to specify their fully qualified type names. For example, ziprelated classes from the SharpZipLib all start with ICSharpCode.SharpZipLib.Zip. This is called the namespace of that class. Most programming languages solve this problem with a using statement that lets you specify a list of namespaces for that language to search when you type a plain class name such as ZipEntry. PowerShell lacks a using statement, but the solution demonstrates one of several ways to get the benefits of one.
Prepackaged SDKs aren’t the only DLLs you can load this way, either. An SDK library is simply a DLL that somebody wrote, compiled, packaged, and released. If you are comfortable with any of the .NET languages, you can also create your own DLL, compile it, and use it exactly the same way.
Take, for example, the simple math library given in Example 1510. It provides a static Sum method and an instance Product method.
Example 1510. A simple C# math library
namespace MyMathLib
{ public class Methods {
public Methods() { }
public static int Sum(int a, int b) { return a + b; }
public int Product(int a, int b) { return a * b; } } }
Example 1511 demonstrates everything required to get that working in your Power Shell system.
Example 1511. Compiling, loading, and using a simple C# library
PS >notepad MyMathLib.cs <add the above code to MyMathLib.cs>
PS >SetAlias csc $env:WINDIR\Microsoft.NET\Framework\v2.0.50727\csc.exe PS >csc /target:library MyMathLib.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42 for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727 Copyright (C) Microsoft Corporation 20012005. All rights reserved.
PS >[Reflection.Assembly]::LoadFile("c:\temp\MyMathLib.dll")
GAC
Version
Location
False
v2.0.50727
c:\temp\MyMathLib.dll
PS >[MyMathLib.Methods]::Sum(10, 2)
Example 1511. Compiling, loading, and using a simple C# library (continued)
PS >$mathInstance = NewObject MyMathLib.Methods PS >$mathInstance.Product(10, 2)