Archive | SQL RSS for this section

Running .SQL scripts with Powershell

Today I came across a great script by Andy Mishechkin on MS Technet.


The PowerShell script for execution of T-SQL batch files

It’s a great script for .SQL file execution against a SQL server. It can also run .SQL files which contains GO commands. In short: works like a charm!

Creating an ODBC connection

With Powershell 4 on Windows 8 and 2012, they’ve improve the support for ODBC connections. You just need to import the Wdac module to be able to use the different ODBC functions.

Get-Command *ODBC* shows the following functions:

Add-OdbcDsn
Disable-OdbcPerfCounter
Enable-OdbcPerfCounter
Get-OdbcDriver
Get-OdbcDsn
Get-OdbcPerfCounter
Remove-OdbcDsn
Set-OdbcDriver
Set-OdbcDsn

For instance, if you were to create a System ODBC connection, with a 32 bit SQL driver, use the following code:

Import-Module Wdac
$OdbcDriver = Get-OdbcDriver -Name *SQL* -Platform 32-bit
If(!$OdbcDriver.Count) # Only continue if 1 SQL ODBC driver is installed
{ Add-OdbcDsn -Name "ODBC Connection Name" -DriverName $OdbcDriver.Name -Platform 32-bit -DsnType System -SetPropertyValue @("Server=SQL ServerName\SQL Server Instance", "Trusted_Connection=Yes","Database=SQL DatabaseName") }