alejandro-g
5/24/2017 - 1:53 PM

Invoke-SQL querys returns System.data.datatables

Invoke-SQL querys returns System.data.datatables

function Invoke-SQL {
    param(
        [string] $dataSource = "",
        [string] $database = "Master",
        [string] $sqlCommand = ""
      )

    $connectionString = "Data Source=$dataSource; " +
            "Integrated Security=SSPI; " +
            "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()
    $dataSet.Tables

}