Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# `PowerShell.Create()`返回null_C#_Powershell_.net Core_.net Standard - Fatal编程技术网

C# `PowerShell.Create()`返回null

C# `PowerShell.Create()`返回null,c#,powershell,.net-core,.net-standard,C#,Powershell,.net Core,.net Standard,添加参考:PowerShellStandard.Library 在默认的.net核心项目中重新编程: // ... using System.Management.Automation; using System.Collections.ObjectModel; // ... public static void Main(string[] args) { Collection<PSObject> output; using (PowerShell ps = P

添加参考:
PowerShellStandard.Library

在默认的
.net核心
项目中重新编程:

// ...

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();
    }

// ...
/。。。
使用系统、管理、自动化;
使用System.Collections.ObjectModel;
// ...
公共静态void Main(字符串[]args)
{
采集输出;
使用(PowerShell ps=PowerShell.Create())
{
ps.AddScript(“$test=Get Date;$test”);
输出=ps.Invoke();
}
// ...
我尝试过使用
或不使用
块,但最终得到了相同的结果:
Create
方法没有创建
PowerShell
对象,但也没有引发异常

这是PowerShell
.net标准
库的常见问题吗?是否有解决方法或其他方法来解决我的问题


其他信息,这也发生在
RunspaceFactory
class
CreateRunspace
方法中,因为我正在探索一种自己管理运行空间的解决方法。

查看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类的
默认值
,并且始终

这使得PowerShell标准库5.1不(完全)支持PowerShell


同样适用于
RunspaceFactory。CreateRunspace

PowerShellStandard是一个参考库,它适用于将加载到与PowerShell相同的
AppDomain
中的项目。在这些情况下,所需的程序集已经加载,因此拆下整个SDK将是一种浪费

为了承载PowerShell(或从PowerShell会话外部使用PowerShell API),您需要引用PowerShell SDK(用于PowerShell核心)或GAC程序集(用于Windows PowerShell)

要引用SDK,您需要在项目的基本目录中有一个
nuget.config
文件,该文件将PowerShell myget添加为源

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="dotnet-core" value="https://www.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="powershell-core" value="https://powershell.myget.org/F/powershell-core/api/v3/index.json" />
  </packageSources>
</configuration>

并在csproj中添加对SDK的引用

<ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.3" />
</ItemGroup>


更新日期:2021年4月15日


您不再需要nuget配置,SDK现在可以在nuget上使用。

这是最近发布的

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

如果您以普通用户的身份从命令行运行相同的代码部分(为net core编译),会发生什么情况?@ZorgoZ在构建.net core控制台应用程序时会得到相同的结果(如果这是您的意思?)。这就是我的意思。然后,我对权限的假设似乎是错误的。对不起…
PowerShellStandard.Library
是一个针对
.net-standard2
的库,因此它应该适用于所有.net实现(适用于powershell 5和6)。请给我一点时间来测试您的建议。意识到您正在尝试重新定位到powershell 6;这不是我问题的答案。.net core不需要这样做,因为实现是.net标准的,所以它是受支持的。.可以使用库创建多平台cmdlet,但无法使用其他功能(例如
PowerShell
RunspaceFactory
)这是一个可怕的消息。看起来我需要寻求另一种选择(比如使用核心API)。谢谢你向团队发布了一个问题。只是一个说明,我最终发现了这一点,MyGet repo仍然存在漏洞;缺少作为依赖项的wsman和diagnostics repo。