0

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

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Daniel
  • 605
  • 1
  • 8
  • 19
  • Step 1: look up `Add-Type` instead of using reflection directly. – Eris Jun 22 '16 at 04:47
  • Possible duplicate of [Calling Visual Studio method using powershell](http://stackoverflow.com/questions/11570011/calling-visual-studio-method-using-powershell) – Ash Jun 22 '16 at 05:34
  • @Eris, care to explain why would that help in your opinion? – Andrew Savinykh Jun 22 '16 at 05:48
  • 1
    Nate, if your method is not static, which it is not, you need an instance of the class to call the method on. The syntax you are trying to use only works for static methods. – Andrew Savinykh Jun 22 '16 at 05:51
  • He needs to instantiate it @zespri – Ash Jun 22 '16 at 05:53
  • 1
    Possible duplicate of [How to reference .NET assemblies using PowerShell](http://stackoverflow.com/questions/3079346/how-to-reference-net-assemblies-using-powershell) – Eris Jun 22 '16 at 06:51
  • @Zespri, your comment helped me to resolve the issue. Thanks Nate – Daniel Jun 27 '16 at 12:24

1 Answers1

1

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()
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206