我正在尝试为我的最新脚本配置运行空间池。我想将一些函数传递给这个运行空间池。我从这个链接中找到了一种潜在的方法:
#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)
在此之后,我$initialSessionSate
为 CreateRunspacePool 方法准备了一个。
问题是我被迫添加 $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,但目前它给了我以下语法错误:
如果我回到最小/最大运行空间重载并在脚本块中定义我的所有函数,此错误就会消失。
根据 Google 的说法,当我实例化运行空间以覆盖此行为时,似乎无法显式设置语言模式,因此我的目标是避免提供 $Host.
问题
有谁知道我是否以及如何将自己的构造函数添加到不包含 $Host 的 CreateRunspacePool 方法中?或者也许是其他方式来实现我的目标?
如果它是相关的,我的 PowerShell 在 FullLanguage 模式下运行:
PS>$ExecutionContext.SessionState.LanguageMode
FullLanguage
PS(5.1.19041)
Edit1:添加了无语言模式的错误图片和更多上下文。