我正在尝试为我的最新脚本配置一个运行空间池。我想把一些函数传递给这个运行空间池。我在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)接下来,我为CreateRunspacePool方法准备了一个$initialSessionSate。
问题是我不得不添加$Host,因为CreateRunspacePool方法没有包含( <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)但是,当我包含$Host (通过Get-Host)时,运行空间是在无语言模式下启动的,这是我不想要的。首先,根据微软的文档,我认为无语言模式排除了在我的运行空间中使用AddScript,但目前它给了我以下语法错误:

如果我回到最小/最大运行空间重载,并在scriptblock中定义我的所有函数,这个错误就会消失。
根据谷歌的说法,当我实例化一个运行空间来覆盖这种行为时,似乎不可能显式地设置语言模式,所以我的目标是避免提供$Host。
问题
有人知道我是否以及如何将自己的构造函数添加到不包含$Host的CreateRunspacePool方法中吗?或者用其他方式来实现我的目标?
如果与之相关,我的PowerShell在FullLanguage模式下运行:
PS>$ExecutionContext.SessionState.LanguageMode
FullLanguagePS(5.1.19041)
Edit1:添加了无语言模式的错误图片和更多上下文。
发布于 2021-11-14 14:21:58
问题是我不得不添加$Host,因为CreateRunspacePool方法没有包含
( <min runspaces>, <max runspaces>, <initial session state> )的构造函数的重载:
幸运的是,最小和最大运行空间数量在实例化后是可配置的,因此您可以这样做:
# 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)https://stackoverflow.com/questions/69959454
复制相似问题