SAPIEN Script Packager: Include PowerShell Reference Assemblies

In the latest builds of PowerShell Studio (5.9.253) and PrimalScript (8.1.213), SAPIEN Script Packager added an option to include the PowerShell reference assemblies.

This option was added to address some errors when packaging scripts, particularly when using Add-Type. The error would look something like the following:

Despite having the type information in the associated files, Add-Type appears to fail in a packaged script when loading C# code. The issue appears to stem from Add-Type trying to load internal assemblies from a ‘ref’ folder that it cannot locate. This behavior is only present in PowerShell 7 when packaging a script as an executable.

Resolution

The remedy for this issue is to copy the ‘ref’ folder from the PowerShell install folder to the executable output folder:

When Include PowerShell reference assemblies (PowerShell 7.x only) is checked, it will copy this ref folder to the output folder when building your packaged executable. It is important to note that SAPIEN Script Packager will only copy the ref folder if the selected package version and the installed PowerShell version match. For example, it will not copy 7.4.7 ref files if a 7.5.0 engine is selected.

The option can be found on the Output Settings and is called Include PowerShell reference assemblies (PowerShell 7.x only):

Feedback

As always, any feedback is appreciated. If you have a particular type of blog article or product feature request you would like to see, please submit your suggestions on the Wish List and Feature Requests forum or the Feature Requests page.