Powershell 使用System.ComponentModel.MarshallByValueComponent

Powershell 使用System.ComponentModel.MarshallByValueComponent,powershell,Powershell,我使用System.data.SqlClient从SQL中提取数据,最终将数据集存储到变量中: $SqlQuery = "select * from my_Table" $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security =

我使用System.data.SqlClient从SQL中提取数据,最终将数据集存储到变量中:

$SqlQuery = "select * from my_Table"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlConnection.Open()

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$amManagedFolders = New-Object System.Data.DataSet
$SqlAdapter.Fill($dataset)
然后,我通过类型为:
System.ComponentModel.marshallbyvaluecomponent的
$dataset.Tables[0]
访问该表

我需要能够使用
ForEach
,以便在该表中循环并将结果存储到
CustomPSObject
中供以后使用,但是,我不确定要使用什么方法来实现这一点

我发现下面的代码很有用。我正在进行SQL查询并生成一个包含结果的Excel工作簿

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSetTable = $DataSet.Tables["Table"]
## - Build the Excel column heading:
[Array] $getColumnNames = $DataSetTable.Columns | Select ColumnName
## - Build column header:
[Int] $RowHeader = 1
foreach ($ColH in $getColumnNames)
....
foreach ($rec in $DataSetTable.Rows)
{
foreach ($Coln in $getColumnNames)
{
## - Next line convert cell to be text only:
$xlsSh.Cells.NumberFormat = "@"
## - Populating columns:
$xlsSh.Cells.Item($rowData, $colData) = $rec.$($Coln.ColumnName).ToString()
$ColData++
}
$rowData++; $ColData = 1
}

-编辑,看到你的评论。您可能能够适应这个问题

我需要直接从$dataset中的SQL表开始工作,并将其直接放入CustomObject中。这是可行的,但不可行的是查询自定义对象。这看起来很像我遇到的问题。正在尝试查询CustomObject。最后,我将数据作为CSV导入,然后将其管道化到Where对象并匹配我的条件。