Home

Error when using Microsoft.PowerShell.SDK 7.x from LINQPad

edited September 2021

I'm using LINQPad to execute powershell commands. I've installed the 7.x version of the package (https://www.nuget.org/packages/Microsoft.PowerShell.SDK) and using this query (C# .NET 5.0)

using (PowerShell ps = PowerShell.Create())
{
    ps.AddScript("Import-Module Disk"); // This should not be necessary, because the module is loaded by default. 
    ps.AddScript("Get-Disk");
    var result = ps.Invoke();

    Console.WriteLine(string.Join(";", result));        
    Console.WriteLine(string.Join(", ", ps.Streams.Error));
}

I'm getting this error.

Cannot process Cmdlet Definition XML for the following file: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Storage\Disk.cdxml. Cannot convert the "Microsoft.Management.Infrastructure.CimInstance" value of type "System.String" to type "System.Type".

I don't know what's going on, because this very same code works in a .NET 5 Console application.
Thanks in advance!

Comments

  • It looks like it's using the wrong PS version. In that case, why? I'm lost.

  • Did you end up resolving this?

    I tried it and it gives me this error
    CmdletInvocationException: Cannot process Cmdlet Definition XML for the following file: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Storage\Disk.cdxml. Cannot process the element. Duplicate type name within an assembly.

  • edited March 13

    I cannot get that sample to run - even in Visual Studio. It displays the following error:

    The specified module 'Disk' was not loaded because no valid module file was found in any module directory., The 'Get-Disk' command was found in the module 'Storage', but the module could not be loaded. For more information, run 'Import-Module Storage'.

  • Don't have a solution for this, but I am wondering if this could possibly be something similar to this issue

    When you use Powershell.SDK nuget package in VS, it downloads what seems to be hundreds of dlls for various runtimes, whereas presuming Linqpad is extracting the ones it thinks it needs and so may end up using different dlls.

    PS. Not really relevant to the issue, but

    ps.AddScript("Import-Module Disk"); // This should not be necessary, because the module is loaded by default.

    I don't think there is a module called Disk and Get-Disk is in the Storage module.

Sign In or Register to comment.