Powershell: Microsoft.Management.Infrastructure.CimCmdlets nuget package should be published

Created on 25 Oct 2018  路  19Comments  路  Source: PowerShell/PowerShell

I am currently developing a .NET Core 2.1 Console application that should be able to execute PowerShell Scripts.

My starting point was the example provided in PowerShell-6.1.0docshost-powershellsample-dotnet2.0-powershell.beta.3, but depending on the 6.1 release of PowerShell Core. So when I started implementing more and extending my test powershell script, I ran into an issue concerning Microsoft.Management.Infrastructure:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I am assuming Microsoft.Management.Infrastructure provides CimCmdlets?

I do have the NuGet Package "Microsoft.Management.Infrastructure" in my Project, but the DLLs are not published...

So I had a look at the PowerShell Release zip:

there is a
Microsoft.Management.Infrastructure,
Microsoft.Management.Infrastructure.Native,
Microsoft.Management.Infrastructure.Unmananged and a Microsoft.Management.Infrastructure.CimCmdlets,
which have different versions than the DLLs provided in the NuGet Package "Microsoft.Management.Infrastructure".
Also, the NuGet Package only contains Microsoft.Management.Infrastructure and Microsoft.Management.Infrastructure.Native.

So, is there something wrong with the Nuget Package? Am I using it wrong? What would I need to do if I want to publish a win-x64 Console App hosting PS scripts that supports CimCmdlets, and a linux-x64 Console App hosting PS scripts that do not support CimCmdlets?

I also realized that Get-Modules -ListAvailable did not work (returned nothing) in my test script because I was missing the psd files, which are available in the "Modules" subdir of the PowerShell Release zip.
Are these available as a nuget package? Are they even meant to be redistributed as part of a PowerShell Core Host ".exe"? Also, when I copy that folder manually to my publish dir, some CmdLets complain about a missing ref dir, which is indeed in the PowerShell zip. Same question, is this something specific to pwsh.exe gets redistributed and if I "need" it, I am doing something wrong?

So I guess my question boilds down to this:

What is the correct way to implement and publish a self-contained .exe that supports (potentially) everything, that pwsh.exe can do, but where I have the possibility to control what is executed, and how (runspace, some additional checks, and so on...). Is these even the "correct" way to use PowerShell Core hosting?

Issue-Question Resolution-Fixed WG-DevEx-SDK

Most helpful comment

@bergersn Thanks for the feedback. You do not directly need a reference for Microsoft.Management.Infrastructure for using the CimCmdlets. Microsoft.Management.Infrastructure.CimCmdlets is missing from the package and we will be working on adding that.

All 19 comments

Stack Trace of the Exception mentioned above:

at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
   at System.Reflection.RuntimeAssembly.GetExportedTypes()
   at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
   at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers)
   at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers, String& helpFile)
   at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()

The CimCmdlets are in Microsoft.Management.Infrastructure.CimCmdlets.dll but depends on those other dlls. The nupkg has the right assemblies as that same nupkg is used to build PSCore6. However, you do need to specify the runtime when publishing your project so the right assemblies get populated. The missing .psd1 files has been brought up before, but I can't find the existing issue that was discussing this.

@SteveL-MSFT

Relevant entries from my csproj:

<PropertyGroup Label="Globals">
    <Platforms>x64;x86</Platforms>
  </PropertyGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeIdentifiers>win-x64;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
    <PreserveCompliationContext>true</PreserveCompliationContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Management.Infrastructure" Version="1.0.0" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.ConsoleHost" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.MarkdownRender" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.Native" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.0" />
    <PackageReference Include="Microsoft.PowerShell.Security" Version="6.1.0" />
    <PackageReference Include="Microsoft.WSMan.Management" Version="6.1.0" />
  </ItemGroup>

No Microsoft.Management.* in my publish for win-x64.

What am I doing wrong?

Concerning the missing Modules and ref dir; would simply copying from the PowerShell Release zip be a valid solution (untile the issue(?)) is fixed? Or are those files not meant to be available?

Had a look at packages\microsoft.management.infrastructure\1.0.0\runtimes:

win-arm
win-arm64
win10-x64
win10-x86
win7-x64
win7-x86
win8-x64
win8-x86
win81-x64
win81-x86

So... no win-x64, no linux-x64. The exception for my win-x64 publish:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
   at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
   at System.Reflection.RuntimeAssembly.GetExportedTypes()
   at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
   at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers)
   at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers, String& helpFile)
   at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()

Tries to load the Assembly even though I is not available for the runtime. How am I supposed to solve this? Do I need to target every win*-x* seperately and publish seperately?

Ok, so I had a look at how PowerShell Core 6.1 is build. It looks like you "target" win7-x64, which I guess can be considered as the smallest common denominator for Microsoft.Management.Infrastructure.

What I am missing are the CimCmdlets. I would like to make them available when I am "targetin" win7-x64 and win7-x32. It seems Microsoft.Management.Infrastructure.CimCmdlets is not available via nuget.

Are there any plans for doing this? Will the Modules at some point be available as some sort of package? Or will I have to do a conditional Include and have the PowerShell binary distribution available somehwere else than nuget?

cc @adityapatwardhan

@bergersn Thanks for the feedback. You do not directly need a reference for Microsoft.Management.Infrastructure for using the CimCmdlets. Microsoft.Management.Infrastructure.CimCmdlets is missing from the package and we will be working on adding that.

Thanks you @adityapatwardhan @SteveL-MSFT

@SteveL-MSFT @adityapatwardhan
anything new? cimcmdlets seem to be not available via nuget still.

@bergersn sorry for the delay on this, I believe @adityapatwardhan is concurrently working on this along with other things

@SteveL-MSFT @adityapatwardhan
Ping-ing again for news. Microsoft.Powershell.SDK 6.2.1 NuGet Package still does not containt reference to CimCmdlets. We really need those. Maybe in 7.0?

Also checking in if there has been any development on this as yet?

We are also creating a .NET Core app using powerShell scripts and are facing the exact same issue.

I posted on StackOverflow about this issue but noone has been able to provide a working solution. I am not looking for using commandlets directly but just to run a script.
https://stackoverflow.com/questions/56352672/running-powershell-from-net-core-could-not-load-file-or-assembly-microsoft-ma
There is a 50points bounty but just one day left ;)

@bergersn Thanks for the feedback. You do not directly need a reference for Microsoft.Management.Infrastructure for using the CimCmdlets. Microsoft.Management.Infrastructure.CimCmdlets is missing from the package and we will be working on adding that.

@SteveL-MSFT @adityapatwardhan What exactly is the action item for this ticket? I can't tell.

I want Microsoft.Management.Infrastructure.CimCmdlets as a PowerShell package. I'm going to create an explicit ticket for this, because I don't know what specifically the original post is asking for, and I don't see this as a huge lift to publish Microsoft.Management.Infrastructure.CimCmdlets as a PowerShell package.

@bergersn I think your confusion may be over how .NET Core does publishing. Can you post a full csproj and code to GitHub so I fully understand what problem you are having? Microsoft.Management.Infrastructure should not be published - IMHO, it should merely be referenced by your deps.json file. You can read more about it in this deep-dive by ASP.NET Core developer Nate McMaster (excellent, excellent engineer): https://natemcmaster.com/blog/2017/12/21/netcore-primitives/

there is a nuget package (https://github.com/PowerShell/MMI) that exposes the c# cim interfaces (https://docs.microsoft.com/en-us/dotnet/api/microsoft.management.infrastructure.cimsession?view=pscore-6.2.0)

is there an advantage to a c# dev in going through powershell?

GitHub
Contribute to PowerShell/MMI development by creating an account on GitHub.

@TravisEz13 Sweet! Thank you very much.

This issue has been marked as fixed and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings