Skip to content

Instantly share code, notes, and snippets.

@mintsoft
Created June 14, 2013 10:03
Show Gist options
  • Save mintsoft/5780780 to your computer and use it in GitHub Desktop.
Save mintsoft/5780780 to your computer and use it in GitHub Desktop.
Execute TSQL against a SQL Server from Powershell without SQL Server Management Studio modules installed
$connectionString = "Data Source=cidbsql.cwserverfarm.local;Initial Catalog=workdb;Integrated Security=True;Application Name=PowerSheQL";
$SQL = "SELECT TOP 5 * FROM dbo.RE_Trace;";
$dbc = New-Object System.Data.SqlClient.SqlConnection($connectionString);
$dbc.Open();
$sqlcmd = $dbc.CreateCommand();
$sqlcmd.CommandTimeout = 1;
$sqlcmd.CommandText = $SQL;
$sqlcmd.Prepare();
$sqlReader = $sqlcmd.ExecuteReader();
$Datatable = New-Object System.Data.DataTable;
$Datatable.Load($SqlReader);
$dbc.Close();
$Datatable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment