Functions/GenXdev.Data.SqlServer/Invoke-SSMS.ps1
|
############################################################################### <# .SYNOPSIS Executes SQL Server database queries with support for parameters and transactions. .DESCRIPTION Provides a PowerShell interface for executing SQL Server queries with support for: - Connection via connection string or database name with server - Parameterized queries to prevent SQL injection - Transaction isolation level control - Multiple query execution in a single transaction - Pipeline input for queries and parameters .LICENSE Copyright (C) 2026 René Vaessen / GenXdev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. .PARAMETER ConnectionString The SQL Server connection string for connecting to the database. Format: "Server=servername;Database=databasename;Integrated Security=true" .PARAMETER DatabaseName The name of the SQL Server database. .PARAMETER Server The SQL Server instance name. Defaults to 'localhost'. .PARAMETER Queries One or more SQL queries to execute. Can be provided via pipeline. Each query can be parameterized using @parameter notation. .PARAMETER SqlParameters Hashtable of parameters to use in queries. Format: @{"param" = "value"} Multiple parameter sets can be provided for multiple queries. .PARAMETER IsolationLevel Controls the transaction isolation. Default is ReadCommitted. Available levels: ReadUncommitted, ReadCommitted, RepeatableRead, Serializable .EXAMPLE Invoke-SSMS ` -DatabaseName "users" -Server "localhost" ` -Queries "SELECT * FROM Users WHERE active = @status" ` -SqlParameters @{"status" = 1} .EXAMPLE "SELECT * FROM Users" | Invoke-SSMS -DatabaseName "users" #> function Invoke-SSMS { [CmdletBinding()] [Alias("ssms", "sqlservermanagementstudio")] param ( ) begin { EnsureSSMS } process { } end { $searchPath = GenXdev\Find-Item ` "${Env:ProgramFiles(x86)}\*SQL Server Management*\Ssms.exe", "${Env:ProgramFiles}\*SQL Server Management*\Ssms.exe" | Microsoft.PowerShell.Utility\Sort-Object LastWriteTime -Descending | Microsoft.PowerShell.Utility\Select-Object -First 1 if (-not $searchPath) { Throw "SSMS not found after installation attempt." } & $searchPath } } |