Forum Discussion

Deleted's avatar
Deleted
Feb 22, 2019

Powershell Core - loading assemblies

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?

 

Resources