Previously I showed you how to use PowerShell to talk to SuperOffice via the COM object. In this post I’ll show you how to do the same using the SuperOffice NetServer API. But let me warn you up front – I’m not a PowerShell guy, I’m just a newbie here. It’s not my fault if this code kills a kitten.
It actually took me a while to get this working, so I’m going to show you the entire process from blank .ps1 file to finished result with all the hurdles I encountered on the way.
So, first of all we need to load up the NetServer .dlls in our script. Initially I tried to load them directly from the path they are installed on my computer with
[Reflection.Assembly]::LoadFile(“E:\Program Files\SuperOffice\NetServer 3.1 SDK\v3.1.3268\bin\SOCore.dll”)
[Reflection.Assembly]::LoadFile(“E:\Program Files\SuperOffice\NetServer 3.1 SDK\v3.1.3268\bin\SODataBase.dll”)
[Reflection.Assembly]::LoadFile(“E:\Program Files\SuperOffice\NetServer 3.1 SDK\v3.1.3268\bin\SuperOffice.Legacy.DLL”)
$session = [SuperOffice.SoSession]::Authenticate(“FL”, “”)
However when running that piece of code you’ll get an exception somewhat like this:

Exception when trying to Authenticate
At this point there are actually multiple problems with our setup, and PowerShell isn’t really helping us as it hides the inner exceptions.
Lets take a small detour away from NetServer related code, and include a little piece of PowerShell code to drill down into the actual problem behind the Exception we see here.
At the top of the .ps1 codefile, just above the calls to [Reflection.Assembly], we add this following snippet:

Add an Exception trap
The code, beginning at trap, ending before “[Reflection.." will catch any Exception thrown later on.
It's my understanding that the trap behaves like a try-catch (Exception) in C#.
Inside the trap I'm drilling down into the InnerExceptions and printing the Message for each Exception found.
Note also the continue keyword. This causes the script to continue with the rest of the powershell script whenever it reaches an Exception. This might not be what you want in a production environment.
Because it continues I also added printing of a -> for each Exception, with ->-> for the inner exception etc. It makes it easier to difference between two unrelated Exceptions.
Ok, so when we drill down into the innermost Exception we see that it is a SoException that reads "Configuration file "E:\Program Files\PowerGUI\ScriptEditor.exe.config" is missing".
Ok, so why is it complaining about a .config file for my PowerShell editor? Well, this is NetServer's fault. It loads up a .config file which matches the current process. And since PowerGUI is running the script NetServer expects there to be a ScriptEditor.exe.config in the current directory with all the settings for SuperOffice. When running a script from the PowerShell commandline the current process is typically C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe so that will require there to be a powershell.exe.config file in that directory.
So, I added a NetServer .config file into the E:\Program Files\PowerGUI directory.
Now, when running the script it loads up the .dll's, but fails with a NullReferenceException when calling .Authenticate() method. It's not very obvious, but the problem here is that the .dll files are in another directory.
You need to copy the NetServer .dll's into the current directory. I copied SOCore.dll, SODataBase.dll and SuperOffice.Legacy.dll into the E:\Program Files\PowerGUI directory, and changed the LoadFile statements in the .ps1 file to point to the new path.

Loading NetServer assemblies from the current directory.
When we now run the script there are no errors popping up. Cool!
Let us see if we can do some work as well.
The first, simple example, is to get a contact. Let’s just hardcode a contact_id and get it.

Get a contact using NetServer Entities layer
Let’s just go through the last three lines.
Line 22; Starts with a #, and is a comment.
Line 23; The class is wrapped in square brackets, and there are two :: before the actual method that gets the Contact.
Line 24; Printing to screen is deadeasy in powershell. Just type, and it’ll be echoed to screen. There is a .Name property on the $contact object we created on the previous line.
When you need to look at which properties, methods etc are available on an object in powershell just do something like this: “$contact | Get-Member” and they will all be listed for you.
Lastly I’ll show an example where we create a new Contact, and add a URL to it.

Create a new contact using NetServer
Here we create a new Contact object, calls a methods and set a property, and also create a URLRow class which we hook onto the Contact. Finally it is all Saved to the SuperOffice database.
So to wrap things up.
This isn’t exactly self-explanatory. Specially the part with placing the .config file and NetServer .dll files. But where there is a will there is a way.
Hopefully someone will have use for this. I know I have. Now I know a bit more PowerShell than I did a few hours ago. Goal reached.