powershellassembliesjea

How do I use assemblies with PowerShell JEA?


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.

Solution

  • 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'