Created
March 16, 2020 15:54
-
-
Save TomasBouda/06c2c32f7c487fd5e8ecb96b52bcaa52 to your computer and use it in GitHub Desktop.
This file contains 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
function Invoke-SQL { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$DataSource, | |
[Parameter(Mandatory = $true)] | |
[string]$Database, | |
[Parameter(Mandatory = $true)] | |
[string]$SqlCommand, | |
[Parameter(Mandatory = $false)] | |
[switch]$Scalar | |
) | |
begin { | |
$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() | |
} | |
process { | |
if ($Scalar) { | |
$command.ExecuteScalar() | |
} | |
else { | |
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command | |
$dataset = New-Object System.Data.DataSet | |
$adapter.Fill($dataSet) | Out-Null | |
$dataSet | |
} | |
} | |
end { | |
$connection.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment