7

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.

3
  • What happens if you run the same code portion (compiled for net core) from command line as regular user?
    – ZorgoZ
    Aug 7, 2018 at 18:50
  • @ZorgoZ The same result when building a .net core console application (if that's what you meant?). Aug 7, 2018 at 18:59
  • That was what I meant. Then my presumption related to permissions seems wrong. Sorry...
    – ZorgoZ
    Aug 7, 2018 at 19:52

3 Answers 3

7

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.

1
  • Just a note, I eventually figured this out and the MyGet repo is still broken; missing the wsman and diagnostics repos as dependencies. Aug 8, 2018 at 4:08
3

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.

5
  • PowerShellStandard.Library is a library targeting .net-standard2 so it should work across all .net implementations (for powershell 5 and 6). Give me a bit to test your suggestion. Aug 7, 2018 at 20:40
  • Realized you're trying to re-target to powershell 6; this is not an answer to my problem case. Aug 7, 2018 at 20:47
  • .net core doesn't need to, because the implementation is .net standard, so it is supported. Here's a related article from the official powershell github. It's possible to create multi-platform cmdlets using the library, but unable to use the other features (such as PowerShell and RunspaceFactory) Aug 7, 2018 at 20:55
  • 1
    That is some terrible news. Looks like I will need to pursue another option (like using core apis). Thank you Aug 7, 2018 at 21:12
  • 1
    Posted a question to the team github.com/PowerShell/PowerShellStandard/issues/46 Aug 7, 2018 at 22:05
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.