Search code examples
.netpowershellclass-library

How do I call a public function in a .Net class library from powershell


I have .Net c# class library (C:\path1\path2\Proj1.Web\bin\Proj1.Web.dll). The object browser shows the public function I want to call as follows

public void RunMe()
Member of Proj1.Web.Services.RunCustomServices

I am loading the assembly in powershell using

$asm=[Reflection.Assembly]::LoadFile("C:\path1\path2\Proj1.Web\bin\Proj1.Web.dll")
[Proj1.Web.Services.RunCustomServices]::RunMe()

Method invocation failed because [Proj1.Web.Services.RunCustomServices] doesn't contain a method named 'Runme'. At line:1 char:1 + [Proj1.Web.Services.RunCustomServices]::RunMe() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFoundPS C:\users\home> [Proj1.Web.Services.RunCustomServices]::RunMe() Method invocation failed because [Proj1.Web.Services.RunCustomServices] doesn't contain a method named 'RunMe'. At line:1 char:1 + [Proj1.Web.Services.RunCustomServices]::RunMe() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound


Solution

  • Since the function is not static (or Shared in VB.Net terminology), you'll need to create an instance of the class to call the method on:

    $RCSInstance = New-Object Proj1.Web.Services.RunCustomServices
    $RCSInstance.RunMe()