Skip to content

Instantly share code, notes, and snippets.

@mrhockeymonkey
Last active June 30, 2018 15:37
Show Gist options
  • Save mrhockeymonkey/6579466589ebae7a0ecad83347ee725e to your computer and use it in GitHub Desktop.
Save mrhockeymonkey/6579466589ebae7a0ecad83347ee725e to your computer and use it in GitHub Desktop.
Invoke SQL queries the old way
# This is the old way I used to invoke sql queries.
# A better way now would be to use the SqlServer module and Invoke-SqlCommand
$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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment