如何从 PowerShell 函数即 DataTable 返回特定数据类型 [英] How to return specific data type from PowerShell function i.e. DataTable

查看:43
本文介绍了如何从 PowerShell 函数即 DataTable 返回特定数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个实用程序函数,该函数将返回初始化的 [System.Data.DataTable] 对象数据类型.

I've been trying to write a utility function that will return initialized [System.Data.DataTable] object data type.

我知道讨厌的 PowerShell 函数返回行为,它试图将所有内容展开"为 [System.Array] 返回类型.以前我总是设法解决这个问题.通常使用逗号技巧"返回您自己的数组 @(,$result) 工作 - 但是这一次这似乎没有什么区别,我已经用完了选项......

I am aware of the annoying PowerShell functions return behavior where it tries to "unroll" everything into a [System.Array] return type. Previously I have always managed to get around that problem. Usually using the "comma trick" to return your own array @(,$result) aways worked - however this time this does not seem to make a difference and I've run out of options...

我通常使用的另一个技巧是 Process 块中的 $null 分配(请参阅下面的代码)-这样我就愚弄了管道,在输出上没有什么可展开"...

Another trick I normally use is the $null assignments within the Process block (see my code bellow) - this way I fool the pipeline there is nothing to "unroll" on the output...

我不认为不可能的答案,到目前为止,根据我的经验,在 PowerShell 中没有什么是不可能的 :)

I am not taking impossible for an answer, so far based on my experience nothing is impossible in PowerShell :)

这是我的代码:

function Get-SourceDataTable 
{
    [OutputType([System.Data.DataTable])]
    [cmdletbinding()]
    param(
        [parameter(Mandatory=$true, Position=0)]
        [System.Data.SqlClient.SqlBulkCopy] $Destination,
        [parameter(Mandatory=$true, Position=1)]
        [System.Collections.Specialized.OrderedDictionary] $ColumnDefinition,
        [parameter(Mandatory=$false, Position=2)]
        [int]$ColumnStartIndex = 0
    )
    BEGIN{
        $datatable = New-Object System.Data.DataTable
        $colIndex = $ColumnStartIndex 
    }
    PROCESS{
        $ColumnDefinition.Keys | 
        foreach {
            $null = $datatable.Columns.Add($_, $ColumnDefinition[$_])   # define each column name and data type
            $null = $Destination.ColumnMappings.Add($_, $colIndex) # map column to destination table
            $colIndex++
        }
    }
    END{
        return ,$datatable
    }
}

我希望有人能让这段代码工作...

I hope someone can get this code working...

推荐答案

而不是 return 使用 Write-Output -NoEnumerate.例如:

Rather than return use Write-Output -NoEnumerate. For example:

function New-DataTable {
   $datatable = New-Object System.Data.DataTable
   $null = $datatable.Columns.Add("x",[int])
   $null = $datatable.Columns.Add("y",[int])
   $null = $datatable.Rows.Add(@(1,2))
   $null = $dataTable.Rows.Add(@(3,4))
   Write-Output -NoEnumerate $datatable
}

New-DataTable | Get-Member

但是请注意,如果您只键入 New-DataTable,它可能看起来像编号的行,但是 Get-Member 会告诉您实际的类型返回.

Note however that if you just type New-DataTable, it might look like enumberated rows, but Get-Member tells you the actual type returned.

这篇关于如何从 PowerShell 函数即 DataTable 返回特定数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆