Invoke SQL Queries

Running SQL queries from powershell can be extremely powerfull. However some implementation I have see often result with off object types such as DBNull. This is my method that seems to work quite nicely for me.


$tsqlQuery = @"
Select AccountName
From SUSDB.dbo.tbDownstreamServerTarget
"@
Try {
   $conn.Open()
}
Catch  {
   $PSCmdlt.ThrowTerminatingError($_)
}

$command = $conn.CreateCommand()
$command.CommandText = $tsqlQuery
$table = new-object System.Data.DataTable
$data = $command.ExecuteReader()
$table.load($data)

$conn.Close()
$conn.Dispose()

Leave a Reply

Your email address will not be published. Required fields are marked *