Powershell Core - loading assemblies

%3CLINGO-SUB%20id%3D%22lingo-sub-355539%22%20slang%3D%22en-US%22%3EPowershell%20Core%20-%20loading%20assemblies%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-355539%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20currently%20engaged%20in%20updating%20Powershell%20Code%20to%20work%20with%20Powershell%20Core%20(I%20expect%20a%20lot%20of%20us%20are).%20I've%20been%20able%20to%20overcome%20all%20obstacles%20so%20far%2C%20except%20this%20one!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20existing%20script%20loads%20the%20assemblies%20%3CSTRONG%3ESystem.Windows.Forms%3C%2FSTRONG%3E%20and%20%3CSTRONG%3ESystem.Windows.Forms.DataVisualization%3C%2FSTRONG%3E%20(both%20using%20%3CSTRONG%3E%5BReflection.Assembly%5D%3A%3ALoadWithPartialName()%3C%2FSTRONG%3E)%20and%20creates%20a%20%3CSTRONG%3ESystem.Windows.Forms.DataVisualization.Charting.Chart%3C%2FSTRONG%3E%20object.%20The%20same%20code%2C%20run%20without%20changes%20in%20Powershell%20core%20(and%20on%20the%20same%20machine)%2C%20fails.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ESystem.Windows.Forms%3C%2FSTRONG%3E%20fails%20to%20load%20with%3A%3CBR%20%2F%3E%3CSTRONG%3EException%20calling%20%22LoadWithPartialName%22%20with%20%221%22%20argument(s)%3A%20%22Could%20not%20load%20file%20or%20assembly%20'System.Windows.Forms%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull'.%20Operation%20is%20not%20supported.%20(Exception%20from%20HRESULT%3A%200x80131515)%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3Eand%20%3CSTRONG%3ESystem.Windows.Forms.DataVisualization%3C%2FSTRONG%3E%20appears%20to%20load%20(i.e%20doesn't%20throw%20an%20exception)%20BUT%20the%20console%20display%20starts%20like%20this%3A%3CBR%20%2F%3E%3CSTRONG%3EGAC%26nbsp%3B%20%26nbsp%3BVersion%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E---%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20-------%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EFalse%26nbsp%3B%20%26nbsp%3Bv4.0.30319%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3Eand%20a%20subsequent%20call%20to%20create%20a%20charting%20object%20fails%20with%3A%3CBR%20%2F%3E%3CSTRONG%3ECannot%20find%20type%20%5BSystem.Windows.Forms.DataVisualization.Charting.Chart%5D%3A%20verify%20that%20the%20assembly%20containing%20this%20type%20is%20loaded.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20read%20that%20the%20%3CSTRONG%3E%5BReflection.Assembly%5D%3C%2FSTRONG%3E%20methods%20of%20loading%20.Net%20assemblies%20are%20deprecated%20and%20have%20tried%20following%20the%20guidelines%20to%20use%20%3CSTRONG%3EAdd-Type%3C%2FSTRONG%3E%20instead.%20These%20also%20fail%20in%20very%20similar%20ways%20i.e%3A%3CBR%20%2F%3E%3CSTRONG%3ESystem.Windows.Forms%3C%2FSTRONG%3E%20-%20fails%20to%20load%20with%20an%20exception%3CBR%20%2F%3E%3CSTRONG%3ESystem.Windows.Forms.DataVisualization%3C%2FSTRONG%3E%20appears%20to%20load%20but%20does%20not%20export%20any%20types%3C%2FP%3E%3CP%3EThe%20methods%20I%20have%20tried%20so%20far%20are%3A%3CBR%20%2F%3E%3CSTRONG%3E%5BSystem.Reflection.Assembly%5D%3A%3ALoad%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%5BSystem.Reflection.Assembly%5D%3A%3ALoadFile%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%5BSystem.Reflection.Assembly%5D%3A%3ALoadFrom%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EAdd-Type%20-AssemblyName%3C%2FSTRONG%3E%20(with%20partial%20%26amp%3B%20strong%20names)%3CBR%20%2F%3E%3CSTRONG%3EAdd-Type%20-Path%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EIn%20every%20case%2C%20the%20code%20works%20fine%20on%20PS5%20and%20fails%20in%20Powershell%20Core.%3C%2FP%3E%3CP%3EAre%20these%20assemblies%20simply%20no%20longer%20supported%20in%20Powershell%20Core%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-355539%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-355806%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20Core%20-%20loading%20assemblies%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-355806%22%20slang%3D%22en-US%22%3E%3CP%3EPowerShell%20Core%20is%20not%20a%20%22replacement%22%20or%20%22upgrade%22%20for%20%22regular%22%20PowerShell%2C%20it's%20designed%20to%20solve%20different%20scenarios.%20None%20of%20which%20have%20understanding%20of%20%22UI%22%2C%20apart%20from%20the%20console.%20Now%2C%20afaik%20the%20.NET%20Core%20folks%20are%20actually%20working%20on%20some%20sort%20of%20%22port%22%20for%20the%20Forms%20library%2C%20but%20I%20wouldn't%20hold%20my%20breath%20(see%20the%20announcement%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fdevblogs.microsoft.com%2Fdotnet%2Fnet-core-3-and-support-for-windows-desktop-applications%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdevblogs.microsoft.com%2Fdotnet%2Fnet-core-3-and-support-for-windows-desktop-applications%2F%3C%2FA%3E).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20meantime%2C%20for%20anything%20that%20requires%20even%20a%20simple%20UI-based%20prompt%2C%20keep%20using%20good%20old%20Windows%20PowerShell.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1077545%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20Core%20-%20loading%20assemblies%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1077545%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20PowerShell%20Core%20is%20not%20a%20replacement%20for%20Windows%20PowerShell%2C%20then%20why%20is%20there%20no%20longer%20any%20ongoing%20development%20or%20maintenance%20on%20Windows%20PowerShell%2C%20with%20100%25%20of%20the%20PowerShell%20team%20working%20exclusively%20on%20PowerShell%20Core%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1077874%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20Core%20-%20loading%20assemblies%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1077874%22%20slang%3D%22en-US%22%3E%3CP%3EThat's%20a%20question%20you%20should%20be%20asking%20the%20relevant%20folks%20at%20Microsoft.%20It%20was%20a%20poor%20judgement%20on%20their%20end%2C%20one%20that%20will%20finally%20be%20addressed%20with%20the%20release%20of%20v7.%20You%20can%20read%20the%20announcement%20blog%20for%20more%20details%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fthe-next-release-of-powershell-powershell-7%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fthe-next-release-of-powershell-powershell-7%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Deleted
Not applicable

I'm currently engaged in updating Powershell Code to work with Powershell Core (I expect a lot of us are). I've been able to overcome all obstacles so far, except this one!

 

The existing script loads the assemblies System.Windows.Forms and System.Windows.Forms.DataVisualization (both using [Reflection.Assembly]::LoadWithPartialName()) and creates a System.Windows.Forms.DataVisualization.Charting.Chart object. The same code, run without changes in Powershell core (and on the same machine), fails.

 

System.Windows.Forms fails to load with:
Exception calling "LoadWithPartialName" with "1" argument(s): "Could not load file or assembly 'System.Windows.Forms, Culture=neutral, PublicKeyToken=null'. Operation is not supported. (Exception from HRESULT: 0x80131515)"

and System.Windows.Forms.DataVisualization appears to load (i.e doesn't throw an exception) BUT the console display starts like this:
GAC   Version
---      -------
False   v4.0.30319

and a subsequent call to create a charting object fails with:
Cannot find type [System.Windows.Forms.DataVisualization.Charting.Chart]: verify that the assembly containing this type is loaded.

 

I have read that the [Reflection.Assembly] methods of loading .Net assemblies are deprecated and have tried following the guidelines to use Add-Type instead. These also fail in very similar ways i.e:
System.Windows.Forms - fails to load with an exception
System.Windows.Forms.DataVisualization appears to load but does not export any types

The methods I have tried so far are:
[System.Reflection.Assembly]::Load
[System.Reflection.Assembly]::LoadFile
[System.Reflection.Assembly]::LoadFrom
[System.Reflection.Assembly]::LoadWithPartialName
Add-Type -AssemblyName (with partial & strong names)
Add-Type -Path

In every case, the code works fine on PS5 and fails in Powershell Core.

Are these assemblies simply no longer supported in Powershell Core?

 

3 Replies
Highlighted

PowerShell Core is not a "replacement" or "upgrade" for "regular" PowerShell, it's designed to solve different scenarios. None of which have understanding of "UI", apart from the console. Now, afaik the .NET Core folks are actually working on some sort of "port" for the Forms library, but I wouldn't hold my breath (see the announcement here: https://devblogs.microsoft.com/dotnet/net-core-3-and-support-for-windows-desktop-applications/).

 

In the meantime, for anything that requires even a simple UI-based prompt, keep using good old Windows PowerShell.

Highlighted

@Vasil Michev 

 

If PowerShell Core is not a replacement for Windows PowerShell, then why is there no longer any ongoing development or maintenance on Windows PowerShell, with 100% of the PowerShell team working exclusively on PowerShell Core?

Highlighted

That's a question you should be asking the relevant folks at Microsoft. It was a poor judgement on their end, one that will finally be addressed with the release of v7. You can read the announcement blog for more details: https://devblogs.microsoft.com/powershell/the-next-release-of-powershell-powershell-7/