0

I'm trying to configure a runspace pool for my latest script. I would like to pass some functions to this runspace pool. I found a potential way to do this from this link:

#Sample function pass to runspace pool (copied from link)

Function ConvertTo-Hex {

    Param([int]$Number)

    ‘0x{0:x}’ -f $Number

}

#Get body of function

$Definition = Get-Content Function:\ConvertTo-Hex -ErrorAction Stop

#Create a sessionstate function entry

$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry
-ArgumentList ‘ConvertTo-Hex’, $Definition

#Create a SessionStateFunction

$InitialSessionState.Commands.Add($SessionStateFunction)

 #Create the runspacepool by adding the sessionstate with the custom function

$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5,$InitialSessionState,$Host)

Following this, I've prepared an $initialSessionSate for the CreateRunspacePool method.

The problem is that I am forced to add $Host, because the CreateRunspacePool method doesn't have an overload for a constructor consisting of ( <min runspaces>, <max runspaces>, <initial session state> ):

PS>([runspacefactory] | gm -Force -Static | select -ExpandProperty definition | sls createrunspacepool`(`) ) -replace '\),',")`n"

static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool()
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(initialsessionstate initialSessionState)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, System.Management.Automation.Host.PSHost host)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, initialsessionstate initialSessionState, System.Management.Automation.Host.PSHost host)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, System.Management.Automation.Runspaces.RunspaceConnectionInfo connectionInfo)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, System.Management.Automation.Runspaces.RunspaceConnectionInfo connectionInfo, System.Management.Automation.Host.PSHost host)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, System.Management.Automation.Runspaces.RunspaceConnectionInfo connectionInfo, System.Management.Automation.Host.PSHost host, System.Management.Automation.Runspaces.TypeTable typeTable)
 static System.Management.Automation.Runspaces.RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, System.Management.Automation.Runspaces.RunspaceConnectionInfo connectionInfo, System.Management.Automation.Host.PSHost host, System.Management.Automation.Runspaces.TypeTable typeTable, psprimitivedictionary applicationArguments)

However, when I include $Host (via Get-Host), the runspace is started up in no-language mode, which I don't want. For one thing, I believe no-language mode precludes the use of AddScript on my runspace according to microsoft docs, but currently it's giving me the following syntax error:

enter image description here

This error disappears if I go back to a min/max runspaces overload and define all my functions within the scriptblock.

According to Google, it doesn't seem possible to explicitly set the language mode when I instantiate a runspace to override this behavior, so my goal instead is to avoid supplying $Host.

Question

Does anyone know whether and how I could add my own constructor to the CreateRunspacePool method that doesn't include $Host? Or maybe some other way to achieve my goal here?

And in case it's relevant, my PowerShell runs in FullLanguage mode:

PS>$ExecutionContext.SessionState.LanguageMode
FullLanguage

PS(5.1.19041)

Edit1: Added a picture of the error with no-language mode and more context to it.

Blaisem
  • 557
  • 8
  • 17

1 Answers1

1

The problem is that I am forced to add $Host, because the CreateRunspacePool method doesn't have an overload for a constructor consisting of ( <min runspaces>, <max runspaces>, <initial session state> ):

Fortunately, minimum and maximum number of runspaces are configurable after instantiation, so you can do:

# Create runspace with desired initial session state
$RunspacePool = [runspacefactory]::CreateRunspacePool($initialsessionstate)

# Set MaxRunspace count after the fact, MinRunspace count defaults to 1
$RunspacePool.SetMaxRunspaces(5)
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Oh, a very nice solution, thanks Mathias! Do you know if in general it's possible to add your own overloads anyways? – Blaisem Nov 14 '21 at 18:31