Skip to content

ConstrainedLanguage mode: [pscustomobject] literals cannot be used, even though the type is white-listed, and an equivalent New-Object call succeeds #20767

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

# From a regular session (with language mode 'FullLanguage')
# CAVEAT: You won't be able to undo this in the same session.
$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'
[pscustomobject] @{ foo = 1 }

Perhaps I'm missing something, but the fact that hashtable literals alone (without the [pscustomobject] "cast") work fine, and that [pscustomobject] is among the whitelisted types, as well as the fact that the equivalent New-Object pscustomobject -Property @{ foo = 1 } does work, leads me to believe that this is a bug (it isn't specific to 7.4.0 - I see it in 7.3.8 too, and even in Windows PowerShell).

Expected behavior

A [pscustomobject] instance should be constructed and output.

Actual behavior

An error occurs (see below).

Error details

Curiously, the error message depends on whether at least one such literal was defined earlier, while the session was still in FullLanguage mode:

  • In the latter case - note the confusing reference to restricted mode:
InvalidArgument: Cannot convert value "System.Collections.Specialized.OrderedDictionary" to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". 
Error: "Cannot convert hashtable to an object of the following type: System.Management.Automation.LanguagePrimitives+InternalPSCustomObject. 
Hashtable-to-Object conversion is not supported in restricted language mode or a Data section."
  • If no [pscustomobject] literal was defined before entering constrained mode:
InvalidArgument: Cannot convert value to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". 
Only core types are supported in this language mode.

Environment data

PowerShell 7.4.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions