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
classCreateRunspace
方法中,因为我正在探索一种自己管理运行空间的解决方法。查看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。