PowerShell 函数不会返回 DataTable

标签 powershell datatable

我在 PowerShell v4.0(Windows 7 x64 SP1)上有一个 PowerShell 脚本,它创建了一个非常复杂的数据表。我希望能够很容易地将 DataTable 代码放在任何地方,所以我决定将它包装在一个简单的函数中,如下所示:

function Get-UserDataTable
{
    $DataTable = New-Object -TypeName System.Data.DataTable -ArgumentList 'User';

    $NewColumn = $DataTable.Columns.Add('Id',[System.Int32]);
    $NewColumn.AllowDBNull = $false;
    $NewColumn.Unique = $true;

    $NewColumn = $DataTable.Columns.Add('FirstName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('MiddleName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('LastName',[System.String]);
    $NewColumn.MaxLength = 64;

    return $DataTable;
}

但是,该代码始终返回空对象。我试过 Write-Output $DataTable , return $DataTable.Copy() , 和 $DataTable ,但该函数始终为空。

所以,我想我可以尝试添加一些行。我总是可以清除数据表,这样编码仍然会更少:
function Get-UserDataTable2
{
    $DataTable = New-Object -TypeName System.Data.DataTable -ArgumentList 'User';

    $NewColumn = $DataTable.Columns.Add('Id',[System.Int32]);
    $NewColumn.AllowDBNull = $false;
    $NewColumn.Unique = $true;

    $NewColumn = $DataTable.Columns.Add('FirstName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('MiddleName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('LastName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewRow = $DataTable.NewRow();
    $NewRow.Id = 1;
    $NewRow.FirstName = 'Test';
    $NewRow.MiddleName = '';
    $NewRow.LastName = 'User';

    $DataTable.Rows.Add($NewRow);

    $NewRow = $DataTable.NewRow();
    $NewRow.Id = 2;
    $NewRow.FirstName = 'Other';
    $NewRow.MiddleName = 'Test';
    $NewRow.LastName = 'User';

    $DataTable.Rows.Add($NewRow);

    return $DataTable;
}

不。此函数返回 [System.Object[]]包含个人 [System.Data.DataRow] . DataRows 的对象数组。

如何从 PowerShell 中的函数返回 DataTable?

最佳答案

正如 PerSerAl 评论中的链接所暗示的那样,该问题是由于 DataTable 不是可枚举的数据类型而引起的。要强制它可枚举,您可以使用一元逗号运算符将其作为单个元素放入数组中。数组是可枚举的。

function Get-UserDataTable
{
    $DataTable = New-Object -TypeName System.Data.DataTable -ArgumentList 'User';

    $NewColumn = $DataTable.Columns.Add('Id',[System.Int32]);
    $NewColumn.AllowDBNull = $false;
    $NewColumn.Unique = $true;

    $NewColumn = $DataTable.Columns.Add('FirstName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('MiddleName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('LastName',[System.String]);
    $NewColumn.MaxLength = 64;

    return ,$DataTable;
}

关于PowerShell 函数不会返回 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491390/

相关文章:

javascript - Vue.js - 事件不会触发子组件在表中显示请求的数据

javascript - JQuery dataTable删除id和class结果行需要在值中搜索

sharepoint - 在SharePoint Online中使用Powershell更改列表字段

powershell - 检查是否已配置 nuget 源

string - 在 PowerShell 字符串中保留换行符

jsf - filterMatchMode lte 和 gte 在 p :calendar in filter facet of p:dataTable 上不起作用

c# - 具有多个条件的数据表上的 LINQ

Powershell 错误在 Azure 上打开 Edx 部署

arrays - 如何在 Powershell 中按对象的一个​​属性值对对象数组进行排序?

javascript - 如何禁用在特定 DataTable 列中的搜索?