添加参考资料:PowerShellStandard.Library
默认.net-core项目中的Repro:
// ...
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库中常见的问题吗?有什么办法解决我的问题吗?
更多信息,这也发生在RunspaceFactory类CreateRunspace方法中,当时我正在探索如何自己管理运行空间。
发布于 2018-08-07 20:34:52
通过查看PowerShellStandard库的源代码,我注意到以下几行:
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也是如此。
发布于 2018-08-16 16:14:17
以下是最近发布的Microsoft.PowerShell.SDK 6.0.4。
看起来它是为PowerShell托管和相关任务而设计的。至少,像代码这样的问题在我的.NET核心应用程序中运行得很好。这个包参考就够了。
https://stackoverflow.com/questions/51733481
复制相似问题