3

I've searched and searched for info on how to load a custom static class in PowerShell but up to now no avail. I'm googled out. I've seen enougth info and samples on how to load custom classes that need to be instantiated or how to load .Net framework classes but not exactly what I'm looking for.

I'm trying to use a custom dll, written in C# with following structure:

namespace Custom.NameSpace
{
   public static class AppCfgHelper
   {
      public static XmlNode SomeXmlNodeFunction( XmlNode xmlRoot )
      {
       ...
       }
   }
}

Can anybody help please?

CB.
  • 58,865
  • 9
  • 159
  • 159
VagaBond
  • 31
  • 1
  • 3

1 Answers1

5

There are two steps. First load the assembly containing your static class e.g.:

Add-Type -Path <path-to-dll>

Then use invoke the static method using PowerShell's static method syntax [typename]::membername e.g.:

$returnedNode = [Custom.NameSpace.AppCfgHelper]::SomeXmlNodeFunction($rootNode)
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Was that so simple. I've tried a lot of s... but not the ones above. Thanks a million Keith. You've saved me at least a couple of hours. – VagaBond Jan 28 '13 at 14:12
  • Returning to the same static class I asked above, how do I use the enumerated type in the static class? I'm able to load the class, use the static methods within, but I have no idea how to use the enumerated properties in it. I need to pass it as a parameter to the methods within the class. – VagaBond Jan 29 '13 at 13:37
  • To clarify my question, have the following C# source (dll assy is used in PowerShell) & not source itself. namespace Custom.NameSpace { public static class AppCfgHelper { public enum ChoiceOfLanguages { English = 0, Nederlands, Deutsch, France, Espanol }; public static string Date2Word( DateTime inputDate, ChoiceOfLanguages lang ) { ... } } } Loading the class with Add-Type works as a charm. But how do I call the static method 'Date2Word' passing one of the values in the enumerated data types? – VagaBond Jan 29 '13 at 14:52
  • `[Custom.NameSpace.AppCfgHelper+ChoiceOfLanguages]::English` but a lot times PowerShell will just accept a string e.g. `'English'`. – Keith Hill Jan 29 '13 at 15:12
  • I've tried the following: $result = [string] Date2Word( $testDate, [Custom.NameSpace.AppCfgHlper+ChoiceOfLanguages]::English ) $result = [string] Date2Word( $testDate, 'English' ) $result = [string] Date2Word( $testDate, 0 ) All generated same error: Unexpected token 'Date2Word' in expression or statement. Assign result to a variable & write variable out, it works: $eng = [Custom.NameSpace.AppCfgHlper+ChoiceOfLanguages]::English $eng Result is English I can see values: PS C:\Windows\system32> [enum]::GetValues([...AppCfgHlper+ChoiceOfLanguages]) English Nederlands Deutsch France Espanol – VagaBond Jan 30 '13 at 11:17
  • If Date2Word is in your C# code you need to either call it on an object instance $obj.Date2Word() or as a static method e.g. [somenamespaces.classname]::Date2Word(). – Keith Hill Jan 30 '13 at 14:57
  • I apologize, I should've known that, yet I forgot to add [Custom.NameSpace.AppCfgHlper] in front of the method. Once again, thanks for patience. – VagaBond Jan 31 '13 at 08:38