Powershell: Powershell.SDK nuget dependencies not complete

Created on 29 Nov 2017  路  10Comments  路  Source: PowerShell/PowerShell

I mention PowerShell.SDK, but it is maybe related to System.Management.Automation dependencies.
Let's see how to reproduce.

Steps to reproduce

  • create a new dotnetcore 2.0 project
  • install PowerShell.SDK nuget package
dotnet add package Microsoft.PowerShell.SDK --version 6.0.0-rc --source https://powershell.myget.org/F/powershell-core/api/v3/index.json 

```Program.cs
using System;
using System.Management.Automation.Runspaces;

namespace test_powershell
{
class Program
{
static void Main(string[] args)
{
var runspace = RunspaceFactory.CreateRunspace();
}
}
}

This code throws an exception of type: *System.Management.Automation.Runspaces.PSSnapInException*

Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly 'E:\dev\perso\git\test_powershell\Microsoft.PowerShell.Commands'. The system cannot find the file specified.
at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
at test_powershell.Program.Main(String[] args) in E:\dev\perso\git\test_powershell\Program.cs:line 10} System.Management.Automation.Runspaces.PSSnapInException

Expected behavior
-----------------
Should not throw any exception.

How-to fix it
---------------

Install manually 2 another packages:
 * Microsoft.PowerShell.Commands.Diagnostics
 * Microsoft.WSMan.Management

Regarding the nuspec definition in Microsoft.PowerShell.SDK, those 2 packages are not listed as a dependency. Neither in System.Management.Automation.

```xml
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>Microsoft.PowerShell.SDK</id>
    <version>6.0.0-rc</version>
    <authors>Microsoft.PowerShell.SDK</authors>
    <owners>Microsoft.PowerShell.SDK</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>PowerShell SDK metapackage</description>
    <copyright>(c) Microsoft Corporation. All rights reserved.</copyright>
    <dependencies>
      <group targetFramework=".NETCoreApp2.0">
        <dependency id="Microsoft.PowerShell.Commands.Management" version="6.0.0-rc" exclude="Build,Analyzers" />
        <dependency id="Microsoft.PowerShell.Commands.Utility" version="6.0.0-rc" exclude="Build,Analyzers" />
        <dependency id="Microsoft.PowerShell.ConsoleHost" version="6.0.0-rc" exclude="Build,Analyzers" />
        <dependency id="Microsoft.PowerShell.Security" version="6.0.0-rc" exclude="Build,Analyzers" />
        <dependency id="System.Management.Automation" version="6.0.0-rc" exclude="Build,Analyzers" />
        <dependency id="System.Data.SqlClient" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.IO.Packaging" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Net.Http.WinHttpHandler" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.ServiceModel.Duplex" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.ServiceModel.Http" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.ServiceModel.NetTcp" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.ServiceModel.Primitives" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.ServiceModel.Security" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Text.Encodings.Web" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Threading.AccessControl" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Private.ServiceModel" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.NETCore.Windows.ApiSets" version="1.0.1" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

Maybe those 2 packages should be listed as dependencies of system.Management.Automation

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>System.Management.Automation</id>
    <version>6.0.0-rc</version>
    <authors>System.Management.Automation</authors>
    <owners>System.Management.Automation</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>PowerShell Core's System.Management.Automation project</description>
    <copyright>(c) Microsoft Corporation. All rights reserved.</copyright>
    <dependencies>
      <group targetFramework=".NETCoreApp2.0">
        <dependency id="Microsoft.PowerShell.CoreCLR.Eventing" version="6.0.0-rc" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Win32.Registry.AccessControl" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="Newtonsoft.Json" version="10.0.3" exclude="Build,Analyzers" />
        <dependency id="System.IO.FileSystem.AccessControl" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Security.AccessControl" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Security.Cryptography.Pkcs" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Security.Permissions" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Text.Encoding.CodePages" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Management.Infrastructure" version="1.0.0-alpha07" exclude="Build,Analyzers" />
        <dependency id="PowerShell.Core.Instrumentation" version="6.0.0-beta.10" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>
Issue-Discussion Resolution-Answered

Most helpful comment

I just tested with 6.0.4 look to be fixed indeed.

All 10 comments

Have you tried using the PowerShellStandard.Library? I think that's the preferred package to use for compat with .NET Core 2.0.

See these instructions https://github.com/PowerShell/PowerShell/blob/master/docs/cmdlet-example/command-line-simple-example.md

@rkeithhill yeah but the point is to hav c# applications hosting powershell runtime.
In my current company we have a library running PS in .net processes. And the point is to migrate to the .netCore version

PowerShellStandard.Library is specifically for libraries (like netstandard2.0 is). Although I've not tried the hosting scenario with PSL, I'd be surprised if the hosting APIs were missing. A library compiled in such a manner should then load into C# apps that target netcore2.0 as well as net461 and higher.

Hi. I'm having a similar problem. When I try to call the Invoke method on an instance of PowerShell class, the following exception is thrown:

System.Management.Automation.Runspaces.PSSnapInException: 'Cannot load PowerShell snap-in Microsoft.PowerShell.Host because of the following error: Could not load file or assembly 'D:\Projects\PwshNetCore\bin\Debug\netcoreapp2.0\Microsoft.PowerShell'. The system cannot find the file specified.'

I assume I have all the necessary dependencies referenced. Here what my project file looks like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
  <!--Source for packages: https://powershell.myget.org/gallery/powershell-core-->
    <PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.0.0" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="6.0.0" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="6.0.0" />
    <PackageReference Include="microsoft.wsman.management" Version="6.0.0" />
    <PackageReference Include="System.Management.Automation" Version="6.0.0" />
  </ItemGroup>

</Project>

And the code I have is pretty simple:

PowerShell ps = PowerShell.Create().AddCommand("Get-Date");
ps.Invoke();

Here is the full exception stack trace:

   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at PwshNetCore.Program.Main(String[] args) in D:\Projects\PwshNetCore\Program.cs:line 10

Is there a workaround available? Thanks!

@rkeithhill even after compiling against PS Standard, the application picking up the APIs still needs a real, runtime assembly to invoke. If you're on Windows, you can pick up a FullCLR System.Management.Automation out of the GAC, but if you specifically want PowerShell Core 6.0 (which you'll have to do if you're on non-Windows), you have to deliver it with your application.

@adityapatwardhan is this the bug you fixed in the SDK last week?

6167 should address this issue.

@adityapatwardhan Has this been addressed? Still experiencing this on 6.0.3.

@TheIncorrigible1 We had a regression on v6.0.3. We are soon releasing a new package with fixes.

In the meantime, can you try v6.0.2?

I just tested with 6.0.4 look to be fixed indeed.

@Bhaal22 Thanks for confirming.

Was this page helpful?
0 / 5 - 0 ratings