2
SQLデータベースの問題を特定するために使用するクエリがいくつかありますが、これを自動的に行うために使用できるpowershellスクリプトで作成しようとしています。私が抱えている問題は、私がSQLスクリプトを呼び出すときに複数の結果セットがあり、スクリプトが最初のセットをキャプチャしているようだということです。私は、すべての結果を循環させるために何をする必要があるのだろうかと思っています。これは単にいくつかの簡単な選択powershellを使用して複数のSQL結果セットを処理する
$dataSource = 'Server'
$database = "DB"
$sqlcommand = @"
Select TOP 1000 * from tblA;
Select TOP 1000 * from tblB
"@
Function Convert-Dataset
{
Param
(
[Parameter(Mandatory=$true)]
$dataset
)
Begin
{
[email protected]()
For($r = 0; $r -lt $dataset.tables[0].rows.Count; $r++)
{
$table= new-object psobject
If($dataset.tables[0].columns.ColumnName.Count -le 1)
{
$colName = [String]$dataset.tables[0].columns.ColumnName
If($dataset.tables[0].rows.Count -eq 1)
{
$colValue = [string]$dataset.tables[0].rows.$colName
}
Else
{
$colValue = [string]$dataset.tables[0].rows[$r].$colName
}
$table | Add-Member -memberType noteproperty -Name $colName -Value $colValue
}
Else{
For($c = 0; $c -lt $dataset.tables[0].columns.ColumnName.Count; $c++)
{
$colName = [String]$dataset.tables[0].columns.ColumnName[$c]
$colValue = [string]$dataset.tables[0].rows[$r][$c]
$table | Add-Member -memberType noteproperty -Name $colName -Value $colValue
}
}
$return +=$table
}
}
End
{
Return $return
}
}
$connectionString = "Data Source=$dataSource; " +
"Integrated Security=True; " +
"Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$return=Convert-Dataset -dataset $dataset
$return | Out-GridView
それを考え出したのですか? –
$ result.ColumnNameを使用して返されたコンテンツを処理する方法としてconvert-datasetを書きました – Jewtus