SQL Requests & Powershell

It could be really hard to find something interesting on the web when we encountered a trouble in Powershell.

After a long search, I found it ! So, let’s share..

A request in SQL using powershell can be send using .Net Frameword.

You should have in mind that SQL request through powershell are exactly the same like standard SQL requests !

Here is an example with a simple Select :

$conec = “server=;database=;Integrated Security=sspi”
$cmd = “SELECT FROM WHERE ”
$da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $conec)
$dt = new-object System.Data.DataTable
$da.fill($dt)

The “Integrated Security” allow to use Windows Authentication, so the account used to launch the powershell script should have rights on the Database !

The SQL request’s results are contained into the “$dt” variable.

So… Let’s “play” now !

Leave a comment