Skip to content

Can't get [ordered] hashtables with Import-PowerShellDataFile -SkipLimitCheck (Regression from 7.4.x?) #27444

Description

@sean-r-williams

Prerequisites

Context

Likely related to #27306. In #25090, there was some previously-discussed workarounds for lack of an in-box/intended functionality for order-preserving PSD1 import (like what ConvertFrom-Json and others do currently). [ordered] accelerators, with -SkipLimitCheck, worked "enough" for that need.

#19070 from long-ago highlighted the lack of ordered deserialization on Import-PowerShellDataFile, but that bug was closed due to no activity.

Now, that loophole is closed, meaning:

  • Existing [ordered]-tagged PSD1s no longer import.
  • There's no way to deserialze PSD1s in an order-preserving manner, which is important for any round-trip encoding or workflows that depend on deterministic enumeration (e.g. CI)

Steps to reproduce

Import a PSD1 like the following:

[ordered]@{
    A = [ordered]@{
        B = 1
        C = "2"
    }
}

Expected behavior

PS> Import-PowerShellDataFile mrtc.psd1 -SkipLimitCheck
A
-----------
@{B=1, C=2}

Actual behavior

PS> Import-PowerShellDataFile mrtc.psd1 -SkipLimitCheck
Import-PowerShellDataFile: Cannot generate a PowerShell object for a ScriptBlock evaluating dynamic expressions. Dynamic expression: @{
    A = [ordered]@{
        B = 1
        C = "2"
    }
}

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: @{
                 A = [ordered]@{
                 B = 1
                 C = "2"
                 }
                 }.
    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        : 9
    Line             : Import-PowerShellDataFile mrtc.psd1 -SkipLimitCheck
    Statement        : Import-PowerShellDataFile mrtc.psd1 -SkipLimitCheck
    PositionMessage  : At line:1 char:1
                       + Import-PowerShellDataFile mrtc.psd1 -SkipLimitCheck
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : import-PowerShellDataFile
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.6.0
PSEdition                      Core
GitCommitId                    7.6.0
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
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.

    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