Tuesday, 29 January 2013

Create Nintex Constants in Powershell

My current project has the requirement of a 'dll free' installation. That means that all the good code that was written in C# to create Nintex Constants  (I used http://jaclynsaito.wordpress.com/2011/09/13/creating-nintex-workflow-constants-programmatically/ as a reference) is not allowed on any servers. Bummer.

So, how was the problem solved? The solution: Powershells impressive 'Add-Type'.

The syntax I used is something like this:

Add-Type -TypeDefinition $source -Language CSharpVersion3 -ReferencedAssemblies $references -passThru  -ErrorAction SilentlyContinue

(the official blurb is here)

The code is quite straightforward, but the real kicker is the ReferenceAssemblies. By default, Add-Type only gives a limited number of references (System.dll and System.Management.Automation.dll.) So, if I want to add a reference to Nintex.Workflow.dll (which is helpful when trying to add Nintex constants), I will need to provide the full path the dll as part of the referenced assemblies parameter. This becomes even more interesting when multiple references are required.

The code I used to solve this problem is as follows:
I created a function to search the GAC for a specific filename (such as Nintex.Workflow.dll).  The code looks something like this:

function GACFinder([string] $name = $(throw "Value cannot be null: name"))
{
return (Get-ChildItem -Path C:\windows\assembly -Filter $name -Recurse | Select-Object -first 1).FullName;
}

I call the code as follows:
$references = (
(GACFinder("Nintex.Workflow.dll")),
(GACFinder("System.Xml.dll"))
)

The code does a recursive search of the GAC and returns the first instance it finds for the requested dll. For example, I get
C:\windows\assembly\GAC_MSIL\Nintex.Workflow\1.0.0.0__913f6bae0ca5ae12\Nintex.W
orkflow.dll as the reference for Nintex.Workflow.dll.

So, the result of the $references is an array of the references that I need for my code to run. Problem solved. I can now create my 'NintexHelper' C# class and reference it in my Powershell code.

NOTE: If you have multiple versions of a dll in the GAC (perhaps with multiple strong name keys), then this code may cause a problem if the first item it selects is not the one you wanted. It will work in most cases, but I have been burnt before and you have been warned.

No comments:

Post a comment