首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >`PowerShell.Create()`返回null

`PowerShell.Create()`返回null
EN

Stack Overflow用户
提问于 2018-08-07 18:44:36
回答 2查看 2.5K关注 0票数 7

添加参考资料:PowerShellStandard.Library

默认.net-core项目中的Repro:

代码语言:javascript
复制
// ...

using System.Management.Automation;
using System.Collections.ObjectModel;

// ...

public static void Main(string[] args)
{
    Collection<PSObject> output;
    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddScript("$test = Get-Date; $test");
        output = ps.Invoke();
    }

// ...

我尝试过使用或不使用using块,但结果是相同的:Create方法没有创建PowerShell对象,但也没有抛出异常。

这是PowerShell .net-standard库中常见的问题吗?有什么办法解决我的问题吗?

更多信息,这也发生在RunspaceFactoryCreateRunspace方法中,当时我正在探索如何自己管理运行空间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-07 20:34:52

通过查看PowerShellStandard库的源代码,我注意到以下几行:

代码语言:javascript
复制
public static System.Management.Automation.PowerShell Create ( System.Management.Automation.Runspaces.InitialSessionState initialSessionState ) { return default(System.Management.Automation.PowerShell); }
public static System.Management.Automation.PowerShell Create (  ) { return default(System.Management.Automation.PowerShell); }
public static System.Management.Automation.PowerShell Create ( System.Management.Automation.RunspaceMode runspace ) { return default(System.Management.Automation.PowerShell); }

这些总是返回密封PowerShell类的default,而总是null

这使得PowerShell在PowerShellStandard库5.1中(完全)不受支持。

RunspaceFactory.CreateRunspace也是如此。

票数 3
EN

Stack Overflow用户

发布于 2018-08-16 16:14:17

以下是最近发布的Microsoft.PowerShell.SDK 6.0.4

看起来它是为PowerShell托管和相关任务而设计的。至少,像代码这样的问题在我的.NET核心应用程序中运行得很好。这个包参考就够了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51733481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档