powershell - 未为 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”也会发生这种情况。这里需要特定的语法吗?
解决方案
推荐阅读
- python - 将 .shp 文件查询到 Excel 时如何解决错误?
- powershell - 在 If 条件下验证字符串不为空/null
- d3.js - 使用 D3 和 D3Plus 更改文本中的一种单词颜色
- kubernetes - 集群资源管理器中集群应用程序部分中的牧场主链接自定义服务
- design-patterns - REST API 客户端设计 - 最佳实践
- google-bigquery - 删除括号 bigquery 之间的所有内容
- css - 使用 CSS 在 Z 轴上创建平滑倾斜的边界(“午餐托盘”效果)
- colors - 我对 kibana 饼图的调色板有疑问
- javascript - React.js:我使用了 setstate 但它仍然不能同时重新渲染
- c++ - 我需要一些 Makefile 魔法解释