【问题标题】:Can I add a static overload to a standard PowerShell type?我可以向标准 PowerShell 类型添加静态重载吗?
【发布时间】:2021-12-25 18:40:23
【问题描述】:

我正在尝试为我的最新脚本配置运行空间池。我想将一些函数传递给这个运行空间池。我从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,但目前它给了我以下语法错误:

如果我回到最小/最大运行空间重载并在脚本块中定义我的所有函数,此错误就会消失。

根据 Google 的说法,当我实例化运行空间以覆盖此行为时,似乎无法显式设置语言模式,因此我的目标是避免提供 $Host。

问题

有谁知道我是否以及如何将我自己的构造函数添加到不包含 $Host 的 CreateRunspacePool 方法中?或者也许有其他方法可以在这里实现我的目标?

如果相关,我的 PowerShell 会在 FullLanguage 模式下运行:

PS>$ExecutionContext.SessionState.LanguageMode
FullLanguage

PS(5.1.19041)

Edit1:添加了无语言模式和更多上下文的错误图片。

【问题讨论】:

  • $RunspacePool = [runspacefactory]::CreateRunspacePool($initialsessionstate); $RunspacePool.SetMaxRunspaces(5)

标签: powershell powershell-5.1


【解决方案1】:

问题是我不得不添加 $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)

【讨论】:

  • 哦,非常好的解决方案,感谢 Mathias!您知道一般情况下是否可以添加自己的重载?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2016-05-25
相关资源
最近更新 更多