首页 > 解决方案 > 未为 PS Core RunspacePool 调用 InitialSessionState 中的 StartupScripts

问题描述

我正在使用 RunspacePool 为 PS 获取预初始化的运行空间:

using (RunspacePool RsPool = RunspaceFactory.CreateRunspacePool(iss))
{
  RsPool.Open();
  using (var ps = PowerShell.Create())
  {

我的初始会话状态看起来(简化)如下:

var iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
iss.Variables.Add(new SessionStateVariableEntry("testVar", myVar, "no description"));
iss.StartupScripts.Add("some PS script here");

变量可以正常使用(稍后在 PS 中可用;导入模块也可以)。但是,启动脚本不会运行。有什么特别之处吗?因为至少源代码https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/InitialSessionState.cs#L2558暗示它应该运行。

这是在安装了 PS SDK NuGet 的 .NET Core 3.1 控制台中运行的。手动创建运行空间,在其管道上调用命令,然后在其上构建 PS 工作正常。但我想有一个预先初始化的运行空间池。

编辑

我添加了

defaultSessionState.ThrowOnRunspaceOpenError = true;

现在这至少告诉我出了点问题:

The term 'some PS script here' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again..'

即使使用简单的导入模块“modulexyz”也会发生这种情况。这里需要特定的语法吗?

标签: powershell

解决方案


推荐阅读