Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Powershell functions from C#

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

like image 662
Sean Avatar asked Nov 14 '10 19:11

Sean


People also ask

How do you call a function in PowerShell?

A function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces. The function shown is a simple example that returns the version of PowerShell.

How do I run a PowerShell script from a function?

For this, open your Windows PowerShell ISE and create a new file. The function keyword is used to declare a function in PowerShell, followed by the function name and curly braces. The function's code or body is within those curly braces { }. We will execute this “Get-Version” function at run time.

How do I call a PowerShell script from C# with parameters?

AddCommand() method, directly available on a PowerShell instance, expects only a name or file path, whereas it is the distinct . AddScript() method that expects source-code text. Therefore, try the following instead: Command scriptCommand = new Command(@"C:\Code\PathToScript\CreateUser.


2 Answers

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();
}
like image 55
Amr Reda Avatar answered Oct 02 '22 09:10

Amr Reda


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

like image 31
Roman Kuzmin Avatar answered Oct 02 '22 11:10

Roman Kuzmin