1

I'm currently porting a library from .NET Framework to .NET Core. The Framework version of the library makes use of some runspaces in Powershell, which I'm not very familiar with (leave aside the merits of using Powershell in Core for the sake of the question). The relevant code is as follows

using System.Management.Automation;
using System.Management.Automation.Runspaces;
...
            RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

            using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                using (RunspaceInvoke invoker = new RunspaceInvoke(runspace))
                {
                    output = invoker.Invoke("Set-ExecutionPolicy Unrestricted");
                    PrintOutput(output);
...

I know that in .NET Core, RunspaceConfiguration has been deprecated in favor of using InitialSessionState. But I'm not certain what to do with RunspaceInvoke, which doesn't exist (to my knowledge) in .NET Core. What would one use to invoke a runspace (or perform some suitable workaround) in .NET Core?

  • It looks to me like it is supported as it is apart of the `System.Management.Automation` NameSpace/Assembly? `dotnet add package System.Management.Automation --version 7.0.2` https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaceinvoke?view=powershellsdk-1.1.0 I don't know much about `.net-core`, so apologies if I'm completely incorrect, but following the documentation it looks like `RunspaceInvoke` should be supported – Riley Carney Jun 18 '20 at 03:16
  • 1
    System.Management.Automation is supported in .NET Core. However, RunspaceInvoke requires installing Microsoft.PowerShell.5.ReferenceAssemblies, which only runs on .NET Framework. In Visual Studio, installing this gives a 'This package may not be fully compatible with your project' warning – Jacob Archambault Jun 18 '20 at 13:33
  • Okay thanks Jacob, I'll look more into this when I'm home to see if there is alternative for `Microsoft.PowerShell.5.ReferenceAssemblies`. Did you try loading your project with the package even though it was giving a compile warning and it didn't work? – Riley Carney Jun 18 '20 at 19:01

1 Answers1

1

A Runspace is simply an instance of Powershell.

To create an instance of PowerShell in .NET Core, use System.Management.Automation's PowerShell class with its PowerShell.Create() method:

            InitialSessionState runspaceConfiguration = InitialSessionState.Create();

            using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                using (PowerShell invoker = PowerShell.Create(runspace))
                {

                    output = invoker.Invoke("Set-ExecutionPolicy Unrestricted");
                    PrintOutput(output);
  • The [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell.create) for the PowerShell.Create() method implies the above identity (runspace = PowerShell instance) but it's not explicit. Under the version of the method that takes an InitialSessionState parameter, it states: ============== "**Parameters** InitialSessionState with which to create the **runspace** Returns PowerShell An instance of PowerShell. ================ – Jacob Archambault Jun 18 '20 at 14:37