Executes numbered SQL scripts (001_…sql, 002_…sql, …) from a directory in ascending numeric order against a target database. Pre-flight checks detect USE DATABASE mismatches and nested BEGIN TRANSACTION statements. All scripts run inside one outer BEGIN/COMMIT/ROLLBACK transaction by default. Creates a full database backup before execution and writes a detailed .log and .csv to -LogPath.
Execution Flow
Synopsis
Script files must start with one or more digits (^\d+) to be included. Files without a numeric prefix are skipped with Status=Skipped. The numeric prefix is parsed as an integer for sorting, so 2_ sorts before 10_. Pre-checks scan every file for USE [database] pointing to a different database and for nested BEGIN TRANSACTION statements (only relevant in wrap-transaction mode). Both checks prompt the user interactively — in -WhatIf mode only the warning is logged. The backup is placed in a Sonderbackup subdirectory under the default SQL Server backup path. The summary object is the last element in the returned list and also copied to the clipboard via clip.exe.
Requires dbatools. The -JobNumber parameter embeds an order/ticket number into the log and CSV filename for traceability.
-SkipBackup requires ShouldProcess confirmation (ConfirmImpact=High). Declining the confirmation aborts the deployment. When the outer transaction is active, a script failure immediately issues ROLLBACK TRANSACTION, leaving the database unchanged.
Syntax
Invoke-sqmDeployScripts-SqlInstance <String> # required-Database <String> # required-ScriptPath <String> # required; directory with numbered .sql files-LogPath <String> # required; created if missing
[-JobNumber <String>] # embedded in log / csv filename
[-QueryTimeout <Int>] # default: 30 seconds per script
[-SkipBackup]
[-NoWrapTransaction] # each script manages its own transactions
[-SqlCredential <PSCredential>]
[-WhatIf] [-Confirm]
Parameters
Parameter
Type
Default
Description
-SqlInstance
String
—
Required. SQL Server instance (e.g. SQLSERVER01 or SQLSERVER01\INST1).