powershell function not returning datatable

Viewed 26
function Create-Container
(
    $SourceTable
)
{
    $datatable = [system.data.datatable]::new();
    $conn = [system.data.sqlclient.SqlConnection]::new("<connectionstring>");
    $da = [system.data.sqlclient.SqlDataAdapter]::new();
    $da.SelectCommand = [system.data.sqlclient.sqlcommand]::new(); 
    $da.SelectCommand.Connection = $conn;
    $da.selectcommand.CommandText = "<query>";
    $da.fill($datatable) | out-null;
    return $datatable;
}

Given the above PowerShell 7.2.5 function, I am unable to create an empty DataTable using the following operation:

$emptycontainer = Create-Container -SourceTable "sourcetable"

$emptycontainer is always null. However, placing the code in the main code path and not in a function works as expected. How do I get the function to return the empty DataTable?

0 Answers
Related