0

I have library built for .NET Standard 2.0.

I am using this library in

  • a .NET Core project and
  • a .NET Framework project.

I have integrated this PowerShellStandard.Library (Version 5.1.0) nuget into my .NET Standard library. It is used to fetch some date which is connected to PC.

private string RunPowerShellCommand(string paramValue)
{
    using (PowerShell powershell = PowerShell.Create().AddCommand("Get-PnpDeviceProperty").AddParameter("InstanceId", paramValue).AddParameter("KeyName", "DEVPKEY_Device_Parent"))
    {
        var result = powershell.Invoke();
        string deviceId = "";
        foreach (var r in result)
        {
            var properties = r.Properties;

            var device = (string)properties.Where(t => t.Name == "Data").FirstOrDefault().Value;
            deviceId = (string)device;
        }
        return deviceId.Split('\\')[2];
    }
}
  • When I call above code from the .NET Framework project then the PowerShell.Create() returns the proper PowerShell object
  • When I call same code from the .NET Core project then it returns null.

Could anyone me please ?!

6
  • Is Powershell Core installed? Which version? The version you installed is rather old Commented Jul 16, 2020 at 10:45
  • In any case, Powershell Core is a .NET Core shell while Windows PowerShell is .NET Framework. You're trying to load .NET Old assemblies in a .NET Core application. The package's description says Enable same Module to support PowerShell Core and Windows PowerShell, not that you can load Windows PowerShell into a .NET Core applicatino Commented Jul 16, 2020 at 10:47
  • You can solve the 'null return' problem by adding the PowerShell SDK to your project, but you may still get nothing from the PNP cmdlets in .NET Core - some things just aren't supported in the newer PS versions. Commented Jul 16, 2020 at 10:55
  • 1
    What does powershell.HadErrors and the contents of powershell.Streams.Error tell you? :) Commented Jul 16, 2020 at 11:23
  • @PanagiotisKanavos I did not install powershell core in among three projects. could you please let me know Powershell Core nuget package. in which project we need to install?. Commented Jul 16, 2020 at 11:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.