Sending queries to SQL Server
Querying is a typical task we do with SQL Server. Normally we would open SQL Server Management Studio (SSMS) and type and execute our queries from there. If we are using PowerShell, that routine needs to be slightly adjusted. The few ways we can send queries to SQL Server using PowerShell are as follows:
- SQL Server Management Objects (SMO)
Invoke-Sqlcmd
- ADO.NET
Invoke-Expression
SQL Server Management Objects
We have been using SQL Server Management Objects (SMO) for a few chapters now. Although it's indirect, when we create SMO objects, use properties, and invoke methods, we are technically sending queries to SQL Server. Let us take the following snippet, for example:
$servername = "ROGUE" # or localhost $server = New-Object ...
Get PowerShell for SQL Server Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.