Skip to content

Import-PowerShellDataFile: [ordered] requires -SkipLimitCheck on nested properties #25090

Description

@sean-r-williams

Prerequisites

Steps to reproduce

  1. Create a PSD1 with the following contents:
    [ordered]@{
        TestCase = "foo"
        TestCase2 = "bar"
        TestCase3 = [ordered]@{
            "bar" = "baz"
            "baz" = "bar"
        }
    }
  2. Save the file to disk, e.g. name MRTC.psd1.
  3. Call import-powershelldatafile .\path\to\MRTC.psd1 inside another order-sensitive function, like ConvertTo-Json.

Expected behavior

PS> convertto-json (Import-PowerShellDataFile .\MRTC.psd1 -SkipLimitCheck)
{
  "TestCase": "foo",
  "TestCase3": {
    "baz": "bar",
    "bar": "baz"
  },
  "TestCase2": "bar"
}

Actual behavior

PS> Import-PowerShellDataFile .\MRTC.psd1
Import-PowerShellDataFile: Cannot generate a PowerShell object for a ScriptBlock evaluating dynamic expressions. Dynamic expression: @{
    TestCase  = "foo"
    TestCase2 = "bar"
    TestCase3 = [ordered]@{
        "bar" = "baz"
        "baz" = "bar"
    }
}.

Error details

Exception             : 
    Type       : System.InvalidOperationException
    TargetSite : 
        Name          : SafeGetValue
        DeclaringType : [System.Management.Automation.Language.Ast]
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message    : Cannot generate a PowerShell object for a ScriptBlock evaluating dynamic expressions. Dynamic expression: @{
    TestCase  = "foo"
    TestCase2 = "bar"
    TestCase3 = [ordered]@{
        "bar" = "baz"
        "baz" = "bar"
    }
}.
    Source     : System.Management.Automation
    HResult    : -2146233079
    StackTrace : 
   at System.Management.Automation.Language.Ast.SafeGetValue(Boolean skipHashtableSizeCheck)
   at Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Import-PowerShellDataFile], InvalidOperationException
FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand
InvocationInfo        : 
    MyCommand        : Import-PowerShellDataFile
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 26
    Line             : Import-PowerShellDataFile .\MRTC.psd1
    Statement        : Import-PowerShellDataFile .\MRTC.psd1
    PositionMessage  : At line:1 char:1
                       + Import-PowerShellDataFile .\MRTC.psd1
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Import-PowerShellDataFile
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.0
PSEdition                      Core
GitCommitId                    7.5.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.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.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions