Powershell create scriptblock for Func instead of Action

Question:

I have a class I’m attempting to use from Powershell. This class has a method, called Execute(), with two overloads: one which takes a Func<T> and one which takes an Action<T>. I can call the overload that takes an Action<T> using a scriptblock as the delegate, but I cannot figure out how to call the overload that takes a Func<T>:

How do I call the overload that takes a Func<T>? I assume this would require creating a ScriptBlock that has a return type, but I don’t know how to do this; the Powershell parser apparently isn’t smart enough to do this automatically.

Answer:

You need to cast ScriptBlock to right delegate type by yourself:

Source:

Powershell create scriptblock for Func instead of Action by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply