views:

101

answers:

2

I am a C# .net developer/architect and I am learning powershell.

I have the basics down packed and understand that it uses objects (.Net objects) and not just streams/text.

I would like to be able to use powershell to call methods on my .net (C# library) assembies.

How do I reference an assembly in powershell and use the assemlbY?

Thanks in advance.

+3  A: 

Take a look at this blog post:

Take, for example, a simple math library. It has a static Sum method, and an instance Product method:

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; 
        } 
    } 
}

Compile and run in Powershell:

> [Reflection.Assembly]::LoadFile("c:\temp\MyMathLib.dll")
> [MyMathLib.Methods]::Sum(10, 2)

> $mathInstance = new-object MyMathLib.Methods
> $mathInstance.Product(10, 2)
Darin Dimitrov
The link is not loading...
Russell
The link is loading here. I will update my post and copy the relevant part from the it.
Darin Dimitrov
Thanks for the text. :) I know now why I couldn't find the answer when I was searching. I'll try it and let you know how I go. :)
Russell
You can also use the `Add-Type` cmdlet.
Joey
@Johannes Rossel - How do I use the Add-Type cmdlet? From the get-help command it appears to be for dynamic assemblies?
Russell
@Russell: `add-type -path .\foo.dll`. You can also use it to directly compile code.
Joey
@Johannes, you should add it as a new answer. I'll vote it up ;) I think if possible standard PowerShell means should be used and pure .NET only if needed.
stej
@stej: Same here. Although `Add-Type` didn't exist prior to v2, if I'm not mistaken, so it deserves a mention, although no separate answer in my humble opinion.
Joey
@Johannes, Darin is basically right. His solution will work. However, if somebody else will come here (e.g. from google), he will see his answer and will not read through the comments. That's why I think there should be another answer with `Add-Type`.
stej
@stej: You can give it, then. You need more rep than me in any case ;-)
Joey
hi guys, thanks the add-type is what I am looking for. :) It would be good to put it in a separate question as Add-type is a nicer solution (more built-into powershell), albeit for V2.0 and above.
Russell
Help -on Add-Typehttp://technet.microsoft.com/en-us/library/dd315241.aspx
Doug Finke
+3  A: 

With PowerShell 2.0, you can use the built in Cmdlet Add-Type.

You would just need to specify the path to the dll.

Add-Type -Path foo.dll

Also, you can use inline C# or VB.NET with Add-Type. The @" syntax is a HERE string.

C:\PS>$source = @"
    public class BasicTest
    {
        public static int Add(int a, int b)
        {
            return (a + b);
        }

        public int Multiply(int a, int b)
        {
            return (a * b);
        }
    }
    "@

    C:\PS> Add-Type -TypeDefinition $source

    C:\PS> [BasicTest]::Add(4, 3)

    C:\PS> $basicTestObject = New-Object BasicTest 
    C:\PS> $basicTestObject.Multiply(5, 2)
Andy Schneider
wow!!..that was incredible, if we can just have intellisense in the console this will be even more powerful
voodoomsr
A couple options for that PowerShell in Visual Studio: http://powerguivsx.codeplex.com/ Free IDE for PowerShell: http://powergui.org PowerShell Plus: IDE http://powershellplus.com/
Andy Schneider