Th real work starts when you decided to work with Data from a different source mostly with SQL DBs
I was recently involved in to the subject matter for a project, figured out that everyday is a school day that i learned about how much stuff Powershell can do when it comes in contact with a DB source like SQL. Less talk and let me present you the script to connect to the DB
Few things to notice before running the script. You should have below details ready to make a proper connection.
Server name = SQL server name.
Database = Database name that you are going to connect.
SQl Query = SQL query that you wanna test against the DB
Userid = Username with appropriate access to the DB
Password = Password for the above username.
function Start-SQLQuery
{
[CmdletBinding()]
param
(
[string]$Server,
[string]$Database,
[string]$SQLQuery
)
$Datatable = New-Object System.Data.DataTable
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';User Id=username;Password=Password;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
$Datatable.Load($Reader)
$Connection.Close()
return $Datatable
}
