Execution Flow
The backup path must end with
\User-Db, this is enforced to prevent accidental writes to incorrect directories. Availability Group databases are backed up only and always on the current primary: on a secondary they are skipped with Status=SkippedAgSecondary, regardless of the AG backup preference. Databases outside an AG are not affected (since 1.9.166.0).Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Target SQL Server instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL authentication credentials. |
| -Database | String[] | Optional | Specific database names. Ignored when -All is set. |
| -All | Switch | Optional | Back up all accessible user databases. |
| -BackupPath | String | Optional | Override backup directory. Must end with \User-Db. Defaults to server's BackupDirectory property. |
| -UseExcludeTable | Switch | Optional | Read master.dbo.sqm_BackupExclude and skip databases where IsActive=1 and IsOrphaned=0. |
| -CheckPreferredReplica | Switch | Optional | Obsolete since 1.9.166.0, no effect (logs a warning). AG databases are always backed up on the primary only. |
| -MailTo | String | Optional | Email address for the backup report via Database Mail. Sent on failure by default. |
| -MailProfile | String | Optional | Database Mail profile name. Default: Default. |
| -MailOnSuccess | Switch | Optional | Also send mail on full success (not only on errors). |
| -EnableException | Switch | Optional | Throw exceptions immediately. |
Return Value
Returns PSCustomObject[], one entry per database: SqlInstance, DatabaseName, BackupFile, Status (Success / Failed / WhatIfSkipped / SkippedAgSecondary / NoDatabasesFound), Message.
Examples
Example 1, All user databases, local instance
Invoke-sqmUserDatabaseBackup -AllExample 2, Specific databases on remote server
Invoke-sqmUserDatabaseBackup -SqlInstance "SQL01" -Database "SalesDB", "InventoryDB"Example 3, Backup with exclude table and mail report
Invoke-sqmUserDatabaseBackup -SqlInstance "SQL01" -All -UseExcludeTable `
-MailTo "dba@example.com" -MailOnSuccessExample 4, Custom path
Invoke-sqmUserDatabaseBackup -All -BackupPath "D:\Backup\User-Db"