I created a hashtable representing the structure of the file I'm extracting data from
# 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
}
When I run the script, $schema gets the flattened version of the schema $RecordSchema.Header I've passed.
I've added comma before the parameter $RecordSchema["Header"] yet I got an array of single-item array and that item contains the flattened version of the schema I'm passing.
$record["Header"] = Map-Field $recordRaw[0] (,$RecordSchema["Header"])