Calling Powershell functions from C#
Asked Answered
A

2

18

I have a PS1 file with multiple Powershell functions in it. I need to create a static DLL that reads all the functions and their definitions in memory. It then invokes one of these functions when a user calls the DLL and passes in the function name as well as the parameters for the function.

My question is, is it possible to do this. ie call a function that has been read and stored in memory?

Thanks

Adust answered 14/11, 2010 at 19:53 Comment(1)
If you'd like to execute powershell in .NET Core, look at https://mcmap.net/q/388082/-running-powershell-from-net-coreAlgonkian
M
19

Here's the equivalent C# code for the code mentioned above

string script = "function Test-Me($param1, $param2) { \"Hello from Test-Me with $param1, $param2\" }";

using (var powershell = PowerShell.Create())
{
    powershell.AddScript(script, false);

    powershell.Invoke();

    powershell.Commands.Clear();

    powershell.AddCommand("Test-Me").AddParameter("param1", 42).AddParameter("param2", "foo");

    var results = powershell.Invoke();
}
Maiocco answered 22/4, 2014 at 11:13 Comment(1)
I tried implementing this, but am having trouble. I am using WinUI 3. I posted a separate question here about it with details: #70844136Bonny
H
5

It is possible and in more than one ways. Here is probably the simplest one.

Given our functions are in the MyFunctions.ps1 script (just one for this demo):

# MyFunctions.ps1 contains one or more functions

function Test-Me($param1, $param2)
{
    "Hello from Test-Me with $param1, $param2"
}

Then use the code below. It is in PowerShell but it is literally translatable to C# (you should do that):

# create the engine
$ps = [System.Management.Automation.PowerShell]::Create()

# "dot-source my functions"
$null = $ps.AddScript(". .\MyFunctions.ps1", $false)
$ps.Invoke()

# clear the commands
$ps.Commands.Clear()

# call one of that functions
$null = $ps.AddCommand('Test-Me').AddParameter('param1', 42).AddParameter('param2', 'foo')
$results = $ps.Invoke()

# just in case, check for errors
$ps.Streams.Error

# process $results (just output in this demo)
$results

Output:

Hello from Test-Me with 42, foo

For more details of the PowerShell class see:

http://msdn.microsoft.com/en-us/library/system.management.automation.powershell

Heffner answered 15/11, 2010 at 7:57 Comment(4)
The question is how to do it in c# and you answer how to do it in powershell and tell him to translate it himself into c#? I get that it's not teribly difficult, but really?Vitiate
@Eric Brown - Cal - this is your understanding of the question. My understanding is different - what PowerShell API methods should be called from C#, VB.NET, F#, any .NET language.Heffner
Am I confused? is the title not "Calling Powershell functions from C#". Did I miss something?Vitiate
Is this a question in the title? The question is: "My question is, is it possible to do this. ie call a function that has been read and stored in memory?".Heffner

© 2022 - 2024 — McMap. All rights reserved.