37

I am not sure if this is possible or not with PowerShell.

But basically I have a Windows Forms program that configures a program called EO Server. The EO Server has an API, and I make a reference to EOServerAPI.dll to make the following code run.

using EOserverAPI;
...
private void myButton_Click(object sender, EventArgs e)
{
    String MDSConnString="Data Source=MSI;Initial Catalog=EOMDS;Integrated Security=True;";

    //Create the connection
    IEOMDSAPI myEOMDSAPI = EOMDSAPI.Create(MDSConnString);

    //Get JobID
    Guid myMasterJobID = myEOMDSAPI.GetJobID("myJobRocks");
}

Is it possible to interact with an API DLL file and make the same types of calls as you would in a Windows Forms application?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MicroSumol
  • 1,434
  • 7
  • 22
  • 32

4 Answers4

42

Yes, you can:

Add-Type -Path $customDll
$a = new-object custom.type

You call a static method like so:

[custom.type]::method()

Instead of Add-Type, you can also use reflection:

[Reflection.Assembly]::LoadFile($customDll)

(Note that even the above is calling the Reflection library and the LoadFile static method.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • I am lost with the second statement. $a= new-object custom.type I don't know how to call my custom.type Could you help me? – MicroSumol Nov 02 '11 at 15:02
  • @MicroSumol The type is fully qualified name (namespace+classname)of your class. Eg. A.B.C.ClassName – GiriB Apr 16 '18 at 11:17
12

Take a look at the blog post Load a Custom DLL from PowerShell. If you can interact with an object in .NET, you can probably do it in PowerShell too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris N
  • 7,239
  • 1
  • 24
  • 27
2

Actually the other offered solutions don't work for me, here it's an alternative that works perfectly for me:

$AssemblyPath = "C:\SomePath\SomeLIB.dll"
$bytes = [System.IO.File]::ReadAllBytes($AssemblyPath)
[System.Reflection.Assembly]::Load($bytes)
f4d0
  • 1,182
  • 11
  • 21
  • Do you know why Add-Type doesn't work with your dll? What kind of error message do you get? Or is there something unusual about the dll? – Blaisem Sep 28 '22 at 09:33
  • 1
    Hi @Blaisem, my post is from 3 years and an half ago. I don't remember anymore, sorry for that. – f4d0 Oct 26 '22 at 13:37
2

c# dll

Add-Type -Path $dllPath
(new-object namespace.class)::Main() #Where namespace=dllnamespace, class=dllclass, Main()=dllstartvoid

info. get namespace&classes

$types = Add-Type -Path $dllPath -PassThru
$types | ft fullname
$types

if it's not "executable" dll (something get/set dll) then this is the best that i know (not needed vs for sample dll creating):

https://kazunposh.wordpress.com/2012/03/19/проверка-корректного-ввода-distinguished-name-в-скри/

Garric
  • 591
  • 3
  • 10