Skip to content

Instantly share code, notes, and snippets.

@jourdant
Last active April 8, 2024 18:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jourdant/e9fa625fec54deb1a31f8e441157fc9f to your computer and use it in GitHub Desktop.
Save jourdant/e9fa625fec54deb1a31f8e441157fc9f to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Performs a SQL query and returns an array of PSObjects.
.NOTES
Author: Jourdan Templeton - hello@jourdant.me
.LINK
https://blog.jourdant.me/post/simple-sql-in-powershell
#>
function Invoke-SqlCommand() {
[cmdletbinding(DefaultParameterSetName="integrated")]Param (
[Parameter(Mandatory=$true)][Alias("Serverinstance")][string]$Server,
[Parameter(Mandatory=$true)][string]$Database,
[Parameter(Mandatory=$true, ParameterSetName="not_integrated")][string]$Username,
[Parameter(Mandatory=$true, ParameterSetName="not_integrated")][string]$Password,
[Parameter(Mandatory=$false, ParameterSetName="integrated")][switch]$UseWindowsAuthentication = $true,
[Parameter(Mandatory=$true)][string]$Query,
[Parameter(Mandatory=$false)][int]$CommandTimeout=0
)
#build connection string
$connstring = "Server=$Server; Database=$Database; "
If ($PSCmdlet.ParameterSetName -eq "not_integrated") { $connstring += "User ID=$username; Password=$password;" }
ElseIf ($PSCmdlet.ParameterSetName -eq "integrated") { $connstring += "Trusted_Connection=Yes; Integrated Security=SSPI;" }
#connect to database
$connection = New-Object System.Data.SqlClient.SqlConnection($connstring)
$connection.Open()
#build query object
$command = $connection.CreateCommand()
$command.CommandText = $Query
$command.CommandTimeout = $CommandTimeout
#run query
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataset) | out-null
#return the first collection of results or an empty array
If ($dataset.Tables[0] -ne $null) {$table = $dataset.Tables[0]}
ElseIf ($table.Rows.Count -eq 0) { $table = New-Object System.Collections.ArrayList }
$connection.Close()
return $table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment