Hello there,
Just started learning about sending Sql Server command through PowerShell.
I understand that we can use .Net objects like SQLConnection, SqlCommand (just like we do in C#) to send the CRUD request to Sql Server database (as $sqlcmd = New-Object System.Data.SqlClient.SqlCommand...)
However, I was reading about Provider and SnapIns and have two doubts. Below is the snippet from my PS v2.0 window:
PS C:\> Get-PSSnapin -Registered returns:--------------------------------------------------------------------- Name : SqlServerCmdletSnapin100 Name : SqlServerProviderSnapin100 #If I see what commands are available in above SnapIns, I get following: PS C:\> get-command -module SqlServerProviderSnapin100 #returns:--------------------------------------------------------------------- CommandType Name Definition ----------- ---- ---------- Cmdlet Convert-UrnToPath Convert-UrnToPath [-Urn] <String> [-Verbose. Cmdlet Decode-SqlName Decode-SqlName [-SqlName] <String> [-Verbos. Cmdlet Encode-SqlName Encode-SqlName [-SqlName] <String> [-Verbos. PS C:\> get-command -module SqlServerCmdletSnapin100 #returns:--------------------------------------------------------------------- CommandType Name Definition ----------- ---- ---------- Cmdlet Invoke-PolicyEvaluation Invoke-PolicyEvaluation [-Policy] <PSObject. Cmdlet Invoke-Sqlcmd Invoke-Sqlcmd [[-Query] <String>] [-ServerI.
Two questions:
1. Do I need to Add-PSSnapin only when I need to run above commands?
So, I don't need to import any snap-ins if I want my PowerShell to use .net framework objects to communicate with SQL Server.
2. I can send any CRUD request to database using SqlServerCmdletSnapin100 command 'Invoke-Sqlcmd'.
Which one is preferred method of sending CRUD requests - 'Invoke-Sqlcmd' or .Net Framework objects?
Can anyone please clarify.
Thank you!