Mahmoud ElMansy: knowledge meant to be free

Simple PowerShell Script To Retrieve Data From DataBase

As im continuing posts on PowerShell scripts .
This post to demonistrate how to connect to sqlserver and retrieve data from DataBase.

$connString = "data source=.;Initial catalog=Production;uid=sa;pwd=uer_password;"
$QueryText = "select * from TableName"
$SqlConnection = new-object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connString
$SqlCommand = $SqlConnection.CreateCommand()
$SqlCommand.CommandText = $QueryText
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCommand
$dataset = new-object System.Data.Dataset
$DataAdapter.Fill($dataset)
$dataset.Tables[0]
$dataset.Tables[0] | Export-CSV c:\path\Output.csv