Reputation: 19654
Added reference: PowerShellStandard.Library
Repro inside a default .net-core
project:
// ...
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();
}
// ...
I've tried it with or without the using
block, but I end up with the same result: the Create
method is not creating a PowerShell
object, but it's also not throwing an exception.
Is this a common issue with the PowerShell
.net-standard
library? Is there a workaround or another way to solve my problem?
Additional info, this is also happening with the RunspaceFactory
class CreateRunspace
method as I was exploring a workaround with managing runspaces myself.
Upvotes: 7
Views: 4186
Reputation: 4173
PowerShellStandard is a reference library, it's intended for projects that will be loaded into the same AppDomain
as PowerShell. In those scenarios the required assemblies are already loaded, so it would be a waste to pull down the entire SDK.
In order to host PowerShell (or otherwise use PowerShell API's from outside a PowerShell session) you need to reference the PowerShell SDK (for PowerShell Core) or the GAC assemblies (for Windows PowerShell).
To reference the SDK, you need a nuget.config
file in the base directory of your project that adds the PowerShell myget as a source. Here's an example
<?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>
And add a reference to the SDK in your csproj
<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.3" />
</ItemGroup>
Update 04/15/2021
You don't need the nuget config anymore, the SDK is now available on nuget.
Upvotes: 7
Reputation: 42035
Here is the recently released Microsoft.PowerShell.SDK 6.0.4.
It looks like it is designed for PowerShell hosting and related tasks. At least the question like code worked fine in my .NET Core application. This package reference is enough.
Upvotes: 1
Reputation: 24385
Looking through the source of the PowerShellStandard Library I noticed the following lines:
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); }
Those will always return the default
of the sealed PowerShell class and that will always be null
.
This makes it that PowerShell isn't (fully) supported in PowerShellStandard Library 5.1.
Same is for RunspaceFactory.CreateRunspace
.
Upvotes: 4