PowerShell – Run Assembly that is not registered in the GAC

As demonstrated in previous posts you can easily compile your own .Net Assemblies.

Using AutoIT to create a Wrapper for compiling an EXE or DLL using .NET.

image

Or  your own COM objects

But now it is time to see once we compiled the Assembly.dll, and how we can run it in PowerShell. Without registering it the GAC !

Here is some example code to create a .Net assembly.dll

Public module HelloWorld

 Sub main()
 Console.WriteLine ("Hello World using Visual Basic Code!")
 Msgbox("Hello World from My DotNET Assembly !")
 End sub
End module

Save this code as a Hello World.vb file.

If you compile it using the above Wrapper, choose the Class Library button to generate the DLL.

Once this is successfully processed you will see the Assembly.dll that is created.

image

Using general .NET practice you would need to register the DLL to the GAC in order to access the Methods and Properties. But for scripters like us this is not practical Winking smile

So we want to distribute the DLL across PC’s and Servers by just copying the file around.

So this example shows you have to access the DLL from within PowerShell.

It pops up a message box and wrote something to the console

image

By the way since PS v3.0 the IDE you can use the intellisense feature on you newly created Assembly.dll

image

 

CLS
# Notice the double backslash !
$filename = "C:\\_\\Apps\\Hello World.dll"
$assembly = [Reflection.Assembly]::Loadfile($filename)

echo '--'
echo $assembly.gettype()
echo '--'
echo $assembly.fullName
echo '--'
echo $assembly.gettypes() | ?{$_.IsPublic} # List Public Functions
echo '--'

# Call the Class / Methods
[HelloWorld]::Main()

# Clean-up
rv assembly, filename

As simple as that, isn’t that great ! 🙂

So one more missing link to make the circle round. What if we could access the .NET Assembly from within Autoit running this PS code that would be just great.

Coming soon stay tuned !

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: