-
-
Save vunguyen-it/28fb3bac8bd4cad92eece3378270954c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Performs a SQL query and returns an array of PSObjects. | |
.NOTES | |
Author: Jourdan Templeton - [email protected] | |
.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