Skip to content

About

The PowerShell SQL Client module aims to replace to the SQL Server utilities SQLCMD and BCP

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

410 Commits

Folders and files

Repository files navigation

PsSqlClient

.github/workflows/build-validation.yml

PsSqlClient is a compiled PowerShell module for SQL Server and Azure SQL workflows commonly handled by SQLCMD and BCP. It provides PowerShell commands for connecting to databases, running SQL commands and stored procedures, reading scalar values, and bulk-copying PowerShell objects into tables. It is part of PsSqlUtils.

For command details, visit the online documentation.

Installation

Install the module from the PowerShell Gallery:

Install-Module -Name PsSqlClient -Scope CurrentUser

Alternatively, build and install it from source:

  1. Clone this repository.
  2. Install InvokeBuild and the project dependencies.
  3. Run the installation task from the repository root:
Invoke-Build Install

Usage

See the command reference for command details and additional examples.

Azure authentication and module import order

When using Az.Accounts in the same PowerShell session, import it before PsSqlClient to avoid Azure assembly conflicts:

Import-Module Az.Accounts
Import-Module PsSqlClient

If PsSqlClient was loaded first and Azure authentication fails, start a new PowerShell session and import the modules in this order. Az.Accounts is not required for every Azure authentication method. See Connect-TSqlInstance for authentication options.

Copy from CSV to SQL database

# Connect to SQL Server LocalDB using the current Windows login.
Connect-TSqlInstance -DataSource '(LocalDb)\MSSQLLocalDB'

# Create a temporary table with columns matching the CSV file.
Invoke-TSqlCommand 'CREATE TABLE #Test (Id INT NULL, Name NVARCHAR(MAX))'

# Import the CSV rows into the table.
Import-Csv 'test.csv' | Export-TSqlTable '#Test'

This example requires SQL Server LocalDB and a test.csv file with Id and Name columns.

Get a single value

# Connect to SQL Server LocalDB using the current Windows login.
Connect-TSqlInstance -DataSource '(LocalDb)\MSSQLLocalDB'

# Get a scalar value from the database.
[string] $databaseName = Get-TSqlValue 'SELECT DB_NAME()'

Parameterize a query and process results in a pipeline

# Connect to SQL Server LocalDB using the current Windows login.
Connect-TSqlInstance -DataSource '(LocalDb)\MSSQLLocalDB'

# Get table metadata from master, then sort and select the first five rows.
Invoke-TSqlProcedure 'sp_tables' @{ 'table_qualifier' = 'master' } |
    Sort-Object TABLE_NAME |
    Select-Object -First 5

Commands

Command Description
Connect-TSqlInstance Open a SQL Server or Azure SQL connection.
Disconnect-TSqlInstance Close a connection.
Test-TSqlConnection Test a connection and its current database.
Invoke-TSqlCommand Run SQL text, a SQL file, or a stored procedure and return result rows.
Invoke-TSqlProcedure Run a stored procedure, SQL text, or a SQL file and return result rows.
Get-TSqlValue Return the first value from a SQL command or stored procedure.
Export-TSqlTable Bulk-copy pipeline objects into a SQL table.

Changelog

See the changelog file.

Development

.github/workflows/build-validation.yml

Build

The build scripts require InvokeBuild. If it is not installed, install it with the command Install-Module InvokeBuild -Scope CurrentUser.

You can build the module using the VS Code build task or with the command Invoke-Build Build.

Testing

The tests use Pester and PsSqlTestServer. Install the common test modules with:

Install-Module Pester -MinimumVersion 5.2.0 -SkipPublisherCheck -Force
Install-Module PsSqlTestServer -RequiredVersion 1.11.0 -Force

Choose at least one SQL test backend. Tests that use the shared backend selection try LocalDB, then Docker, then Azure SQL; backend-specific tests run when that backend is available.

LocalDB (Windows)

Install the PsSqlLocalDb PowerShell module and the SQL Server Express LocalDB engine:

Install-Module PsSqlLocalDb

Install LocalDB using the SQL Server Express LocalDB instructions. The module alone does not install the database engine. Check that the LocalDB utility is available by running sqllocaldb info.

Docker

Install Docker, start its engine, and make sure the current user can connect to it. Then install the PowerShell module used by the tests:

Install-Module PSDocker

Azure SQL

Azure tests create Azure SQL resources. Install the Az modules required by PsSqlTestServer, then authenticate and select the test subscription:

Install-Module Az.Accounts, Az.Resources, Az.Sql
Connect-AzAccount
Set-AzContext -Subscription '<subscription-name-or-id>'

Alternatively, set PSSQLTESTSERVER_AZURE_SUBSCRIPTION or create the ignored settings file from the repository root:

Import-Module PsSqlTestServer
New-SqlTestSettings -AzureSubscription '<subscription-name-or-id>' -Directory .\test

Azure also requires the existing PsSqlTestServer resource group, permission and network access to create and connect to Azure SQL resources, and explicit consent to use billable resources. Set AllowBillableResources to the JSON boolean true in test/.PsSqlTestServer.settings.json, or set PSSQLTESTSERVER_ALLOW_BILLABLE_RESOURCES=true. A subscription alone does not grant this consent.

Azure test resources may remain after a failure and continue to incur charges. Configure an Azure Cost Management budget with alerts before running these tests, and inspect and remove leftover resources afterward. Budget alerts do not automatically stop spending.

Run Azure tests in a fresh PowerShell session with Import-Module Az.Accounts before importing PsSqlClient or invoking build and test tasks.

The Azure ActiveDirectoryIntegrated test runs only on AD-domain-joined Windows machines. Domain membership alone does not guarantee success: the process also needs an identity configured for Entra integrated authentication and authorized for the test database. Entra-only and other non-domain-joined configurations are skipped.

The interactive Azure authentication test runs by default. To exclude it for unattended runs, use Invoke-Pester -Path .\test\Connect-Instance.Tests.ps1 -ExcludeTagFilter Interactive.

For local testing use the VSCode test tasks or execute the test scripts directly or with Invoke-Pester. The InvokeBuild test tasks are for CI and do not generate console output.

Release

  1. Create a release branch using git-flow.
  2. Update the version number in the module manifest.
  3. Update the changelog in CHANGELOG.md.
  4. If you want to create a pre-release.
    1. Push the release branch to github, to publish the pre-release to PsGallery.
  5. Finish release using git-flow.
  6. Check if tags are not pushed to github.
  7. Check if the release branch is deleted on github.
  8. Create the release on github.

About

The PowerShell SQL Client module aims to replace to the SQL Server utilities SQLCMD and BCP

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Contributors

Languages