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)));
/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.
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.