How do I use assemblies with PowerShell JEA?

43 Views Asked by At

I need to use an assembly in a start script in JEA (Just Enough Administration) but I get an error message when trying to use it.

The capabilities files contains

AssembliesToLoad = 'Web.Script.Serialization.JavaScriptSerializer'

But when I try to use it in a script

$parser = New-Object Web.Script.Serialization.JavaScriptSerializer

I get the error

Enter-PsSession : Could not load file or assembly
'Web.Script.Serialization.JavaScriptSerializer' or one
of its dependencies. The system cannot find the file specified.
2

There are 2 best solutions below

4
Santiago Squarzon On BEST ANSWER

At least for Windows PowerShell 5.1, the JavaScriptSerializer Class is part of System.Web.Extensions.dll, there is no such Web.Script.Serialization.JavaScriptSerializer assembly, in fact:

Add-Type -AssemblyName Web.Script.Serialization.JavaScriptSerializer

Will throw an error, however:

PS ..\pwsh> Add-Type -AssemblyName System.Web.Extensions
PS ..\pwsh> [System.Web.Script.Serialization.JavaScriptSerializer]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    JavaScriptSerializer                     System.Object

So fixing your issue might be as simple as:

AssembliesToLoad = 'System.Web.Extensions'
0
Dennis On

In this case, dependencies wasn't an issue.

But to find dependencies using PowerShell for the class [Web.Script.Serialization.JavaScriptSerializer] you seem to be able to do

C:\> [Web.Script.Serialization.JavaScriptSerializer].Assembly.GetReferencedAssemblies()

Version        Name
-------        ----
4.0.0.0        mscorlib
4.0.0.0        System.Web
4.0.0.0        System.Drawing
4.0.0.0        System
4.0.0.0        System.Web.Services
4.0.0.0        System.Data.Services.Design
4.0.0.0        System.Configuration
4.0.0.0        System.ServiceModel
4.0.0.0        System.Xml
4.0.0.0        System.Core
4.0.0.0        System.Runtime.Serialization
4.0.0.0        System.Design
4.0.0.0        System.Data.Linq
4.0.0.0        System.Web.ApplicationServices
4.0.0.0        System.Data
4.0.0.0        System.ServiceModel.Activation
4.0.0.0        System.Data.Services.Client
4.0.0.0        System.Data.Entity
4.0.0.0        System.Windows.Forms