我创建了一个查询给定服务器/数据库的函数,我尝试将数据输出到以后可以使用的持久数据集中。问题是数据集在输出时以某种方式转换为数组。正如您在脚本中看到的,我在函数外部创建了一个数据集,但是当我将函数结果传递给它时,它会变为数组
[CmdletBinding()]
param(
[parameter(mandatory = $true,helpmessage = "Enter envrionment name to create tdp files for. Eg 'LTS'")]
[string]$environment,
[parameter(mandatory = $true,helpmessage = "Enter a log file desination and name eg. \\torvdb954\dba\refresh_output.txt" )]
[string]$logFile
)
"" > $logFile
function Get-SQLdata
($SQlServerName, $SQlDBName,$SQlQuery,$FLogfile)
{
Try
{
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQlServerName; Database = $SQlDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$Dataset = New-Object System.Data.Dataset
$SqlAdapter.Fill($Dataset)
return $Dataset.tables[0]
}
Catch
{
"Error connecting to: " + $SQlServerName >> $logFile
$_.ToString() >> $logFile
$_.ToString()
break
}
finally
{
$SqlConnection.Close()
}
}
#control table data set
#declare your dataset outside the function so it is persisted
$Dataset = New-Object System.Data.Dataset
$Dataset.GetType()
$CtrlSqlQuery = “select distinct instanceName,databaseName from pega_refresh_control where environment = '" + $environment + "'"
$Dataset = Get-SQLdata -SQlServerName 'torvdb954' -SQlDBName 'DBA_pega_refresh' -FLogfile $logFile -SQlQuery $CtrlSqlQuery
$Dataset.GetType()
$Dataset.tables[0]