Powershell: Public NewProcessConnectionInfo

Created on 14 Aug 2020  路  6Comments  路  Source: PowerShell/PowerShell

The class NewProcessConnectionInfo (https://github.com/PowerShell/PowerShell/blob/4b9b0788ed28ea6d463ce857d1ed81bd4a977a59/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs#L1496 ) must be public (with public creator). Actualy we cannot create out-of-process runspacepool.


Workaround :

         private static RunspaceConnectionInfo CreateNewProcessConnectionInfo(PSCredential credential, PowerShellProcessInstance process)
        { 
            Assembly automation = Assembly.Load(new AssemblyName("System.Management.Automation"));
            Type t = automation.GetType("System.Management.Automation.Runspaces.NewProcessConnectionInfo");
            RunspaceConnectionInfo obj = (RunspaceConnectionInfo)Activator.CreateInstance(t, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { credential }, null);
            foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic))
            {
                if (propertyInfo.Name == "Process")
                    propertyInfo.SetValue(obj, process);
            }

            return obj;
        }

And

RunspaceFactory.CreateRunspacePool(1, 8, CreateNewProcessConnectionInfo(null, new PowerShellProcessInstance(new Version(5, 1), null, null, false)));
Issue-Question WG-DevEx-SDK

Most helpful comment

Thanks for the clarification, I missed that. I don't see any reason not to make the type public, it probably wasn't done so originally because there was no compelling reason to. Reflection, per above is a reasonable workaround.

All 6 comments

/cc @PaulHigin to comment.

RunspaceFactory.CreateOutOfProcessRunspace()

@PaulHigin They're asking about runspace pools.

Thanks for the clarification, I missed that. I don't see any reason not to make the type public, it probably wasn't done so originally because there was no compelling reason to. Reflection, per above is a reasonable workaround.

Seems like PowerShellProcessInstance it was excluded from PowerShell Standard (PowerShell/PowerShellStandard#75).

Should it be included in PowerShell Standard? Honestly I've never figured out the use case for this API. Was it a workflow thing?

/cc @JamesWTruher for PowerShell Standard

Yes, I believe this was exposed for workflows. Nowadays it is just used for background jobs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Michal-Ziemba picture Michal-Ziemba  路  3Comments

rkeithhill picture rkeithhill  路  3Comments

HumanEquivalentUnit picture HumanEquivalentUnit  路  3Comments

alx9r picture alx9r  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments