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?

1

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
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?
[Custom.NameSpace.AppCfgHelper+ChoiceOfLanguages]::English but a lot times PowerShell will just accept a string e.g. 'English'.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.