Skip to content

System.Data.DataTable Becomes Null When Returned from Function on ARM64 and AMD64 Platforms (Windows, macOS & Linux) #25428

@cameronfairbairn

Description

@cameronfairbairn

Prerequisites

Steps to reproduce

When a System.Data.DataTable object is instantiated within a PowerShell function and then returned to the calling scope, the returned value incorrectly becomes $null. This issue has been observed across multiple platforms (Windows AMD64, macOS ARM64, Linux ARM64) and affects multiple recent PowerShell versions, including LTS (7.4) and the latest stable (7.5) and preview (7.6).

Directly creating and manipulating a System.Data.DataTable object outside of a function scope works correctly. Returning other object types (e.g., System.Collections.Hashtable) from functions also works correctly. This suggests an issue specific to how System.Data.DataTable objects are handled or marshalled across function boundaries in recent PowerShell versions, likely introduced sometime after PowerShell 7.3.

Run the following PowerShell code on an affected platform/version (e.g., Windows 10/11 with PS 7.4+, macOS ARM64 with PS 7.4+, Linux ARM64 with PS 7.4+):

Write-Host "Testing DataTable return issue on $($PSVersionTable.PSVersion) / $($PSVersionTable.OS)"

function New-EmptyDataTable {
    Write-Host "[Inside Function] Attempting to create DataTable..."
    $dt = New-Object System.Data.DataTable
    if ($dt -eq $null) {
        Write-Error "[Inside Function] FAILED to create DataTable object!"
        return "FUNCTION_FAILED_CREATION" # Should not happen based on tests
    } else {
        # Object creation inside the function appears successful
        Write-Host "[Inside Function] DataTable created. Type: $($dt.GetType().FullName). Returning..."
        return $dt # Attempt to return the created DataTable
    }
}

Write-Host "[Main Scope] Calling function..."
$resultTable = $null # Ensure it's null before call
try {
    $resultTable = New-EmptyDataTable
} catch {
    Write-Error "[Main Scope] Error calling function: $_"
}

Write-Host "[Main Scope] Function call complete."
Write-Host "[Main Scope] Checking returned value..."

if ($resultTable -eq $null) {
    Write-Error "[Main Scope] TEST FAILED: Returned DataTable is NULL." # This error occurs
} elseif ($resultTable -is [string] -and $resultTable -eq "FUNCTION_FAILED_CREATION") {
    Write-Error "[Main Scope] TEST FAILED: Function indicated internal creation failure."
} elseif ($resultTable -is [System.Data.DataTable]) {
    Write-Host "[Main Scope] TEST SUCCEEDED: Returned value is a DataTable." # This is expected
    Write-Host "[Main Scope] Returned Type: $($resultTable.GetType().FullName)"
} else {
    Write-Warning "[Main Scope] UNEXPECTED RESULT: Returned value is not null, but also not a DataTable."
    Write-Warning "[Main Scope] Returned Type: $($resultTable.GetType().FullName)"
    Write-Warning "[Main Scope] Returned Value: $resultTable"
}

Write-Host "--- Test Complete ---"

Expected behavior

[Main Scope] TEST SUCCEEDED: Returned value is a DataTable.
[Main Scope] Returned Type: System.Data.DataTable

Actual behavior

Write-Error: [Main Scope] TEST FAILED: Returned DataTable is NULL.

Error details

The bug causes the function to return $null. The following error occurs when attempting to use the $null variable that was incorrectly returned (e.g., trying to access a property or method):

Exception             :
    Type    : Microsoft.PowerShell.Commands.WriteErrorException
    Message : [Main Scope] TEST FAILED: Returned DataTable is NULL.
    HResult : -2146233087
CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
InvocationInfo        :
    MyCommand       : if ($resultTable -eq $null) {
                      Write-Error "[Main Scope] TEST FAILED: Returned DataTable is NULL."
                      } elseif ($resultTable -is [string] -and $resultTable -eq "FUNCTION_FAILED_CREATION") {
                      Write-Error "[Main Scope] TEST FAILED: Function indicated internal creation failure."
                      } elseif ($resultTable -is [System.Data.DataTable]) {
                      Write-Host "[Main Scope] TEST SUCCEEDED: Returned value is a DataTable."
                      Write-Host "[Main Scope] Returned Type: $($resultTable.GetType().FullName)"
                      } else {
                      Write-Warning "[Main Scope] UNEXPECTED RESULT: Returned value is not null, but also not a DataTable."
                      Write-Warning "[Main Scope] Returned Type: $($resultTable.GetType().FullName)"
                      Write-Warning "[Main Scope] Returned Value: $resultTable"
                      }
    HistoryId       : 4
    CommandOrigin   : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 2

Environment data

PowerShell 7.4.7 (from tag: lts-7.4-azurelinux-3.0-arm64)

    PowerShell 7.5.0 (from tag: 7.5-azurelinux-3.0-arm64)

    PowerShell 7.6.0-preview.3 (from tag: preview-7.6-azurelinux-3.0-arm64)

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions