如何阻止 Powershell 展平锯齿状数组的参数?

How to stop Powershell from flattening parameter of jagged array?

我创建了一个哈希表来表示我从

中提取数据的文件的结构
# schema of the data stored in the file
$RecordSchema = @{
    Header = @(
        @("EmployerName",       2, 30)
        @("ApplicableMonth",    32, 6)
        @("EmployerIDNumber",   38, 10)
    )
}

# function to extract data from the file based on the $schema
function Map-Field ($row, $schema) {
    $mappedRow = @{}
    foreach ($field in $schema) {
        $fieldName = $field[0]
        $fieldPosition = $field[1]
        $fieldSize = $field[2]

        $value = $row.Substring($fieldPosition, $fieldSize)
        $mappedRow[$fieldName] = $value
    }

    [PSCustomObject]$mappedRow
}

function Set-RecordHeader($record, $recordRaw) {
    $record["Header"] = Map-Field $recordRaw[0] $RecordSchema["Header"]
    $record
}

当我 运行 脚本时,$schema 得到了架构的扁平化版本 $RecordSchema.Header 我已经通过了。

我在参数 $RecordSchema["Header"] 之前添加了逗号,但我得到了一个单项数组,并且该项目包含我正在传递的架构的扁平化版本。

$record["Header"] = Map-Field $recordRaw[0] (,$RecordSchema["Header"]) 

我刚刚发现,出于某种原因,我需要在每个数组的末尾添加一个逗号

$RecordSchema = @{
    Header = @(
        @("EmployerName",       2, 30), # this
        @("ApplicableMonth",    32, 6), # and other comma matter
        @("EmployerIDNumber",   38, 10)
    )
}

我通过运行以下验证

$a = @(
    @(1, 2, 3)
    @(4, 5, 6)
)

$b = @(
    @(1, 2, 3),
    @(4, 5, 6)
)

$a.Length # returns 6
$b.Length # returns 2

我以为 PowerShell 会认为传递一个新行意味着另一个条目:-(