I'm trying to create a RunspacePool
with an initial session state and a count of min/max runspaces. However, the only constructor on RunspaceFactory
that supports all those parameters also requires a PSHost
:
public static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool (
int minRunspaces,
int maxRunspaces,
System.Management.Automation.Runspaces.RunspaceConnectionInfo connectionInfo,
System.Management.Automation.Host.PSHost host);
Passing null
for host
crashes at runtime:
PSArgumentNullException: Cannot process argument because the value of argument "host" is null. Change the value of argument "host" to a non-null value.
What default implementation of PSHost can I pass here? It's an abstract class.
C# configuration:
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>MyRoot</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
<Nullable>enable</Nullable>
</PropertyGroup>
There is no default implementation of PSHost
, but it is straightforward to make a "bare" one:
private class BareHost : PSHost
{
public override string Name { get; } = "BareHost";
public override Version Version { get; } = new Version(1, 0);
public override Guid InstanceId { get; } = Guid.NewGuid();
public override PSHostUserInterface UI { get; } = null;
public override CultureInfo CurrentCulture { get; } = CultureInfo.CurrentCulture;
public override CultureInfo CurrentUICulture { get; } = CultureInfo.CurrentUICulture;
public override void EnterNestedPrompt()
{
throw new NotImplementedException();
}
public override void ExitNestedPrompt()
{
throw new NotImplementedException();
}
public override void NotifyBeginApplication()
{
// no-op
}
public override void NotifyEndApplication()
{
// no-op
}
public override void SetShouldExit(int exitCode)
{
// no-op
}
}
Usage:
var initialSessionState = InitialSessionState.CreateDefault();
// Customize your session state...
// initialSessionState.ImportPSModule(new[] { "MyModule" });
using var pool = RunspaceFactory.CreateRunspacePool(
minRunspaces: 1,
maxRunspaces: 3,
initialSessionState: initialSessionState,
host: new BareHost());