Invoke SQL commands from Powershell script
Hi everyone,
I've been trying to find a good script to invoke SQL commands without installing the SQLPS module but without success.
So I managed to create one:
function InvokeSQL{
param([string] $DataSource, $Database,$Username, $Password, $Query)
$connectionString = “Server=$DataSource;uid=$Username; pwd=$Password;Database=$Database;Integrated Security=False;”
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
$table
$connection.Close()
}
Hope someone finds this helpful.